Skip to content

Commit 2ec84d4

Browse files
committed
fix: update validation rules to support bcp47 lines
1 parent 7bdddd8 commit 2ec84d4

3 files changed

Lines changed: 64 additions & 8 deletions

File tree

index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import fs from 'fs-extra';
88
import chokidar from 'chokidar';
99
import { AsyncQueue } from '@sapphire/async-queue';
1010
import getFlagEmoji from 'country-flag-svg';
11-
11+
import { parse } from 'bcp-47'
1212

1313
const processFrontendMessagesQueue = new AsyncQueue();
1414

@@ -52,15 +52,20 @@ function getPrimaryLanguageCode(langCode: SupportedLanguage): string {
5252
}
5353

5454
function isValidSupportedLanguageTag(langCode: SupportedLanguage): boolean {
55-
const [primary, region] = String(langCode).split('-');
56-
if (!iso6391.validate(primary as any)) {
55+
try {
56+
const schema = parse(String(langCode),
57+
{
58+
normalize: true,
59+
warning: (reason, code, offset) => {
60+
console.warn(`Warning in validating language tag ${langCode}: reason=${reason}, code=${code}, offset=${offset}`);
61+
}
62+
}
63+
);
64+
const isValid = schema && schema.language && schema.language.length === 2;
65+
return !!isValid;
66+
} catch (e) {
5767
return false;
5868
}
59-
if (!region) {
60-
return true;
61-
}
62-
const regionUpper = region.toUpperCase();
63-
return /^[A-Z]{2}$/.test(regionUpper) && (regionUpper in iso31661Alpha2ToAlpha3);
6469
}
6570

6671

package-lock.json

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"dependencies": {
3131
"@aws-sdk/client-ses": "^3.654.0",
3232
"@sapphire/async-queue": "^1.5.5",
33+
"bcp-47": "^2.1.0",
3334
"chokidar": "^4.0.1",
3435
"country-flag-svg": "^1.0.19",
3536
"fs-extra": "^11.3.2",

0 commit comments

Comments
 (0)