Skip to content

Commit 8991a17

Browse files
authored
Remove circular dependency in fluent-langneg (#545)
The circular dependency caused fluent-langneg to not be usable in Vite and likely other bundlers. It also caused a warning to show up in Rollup. fixes #459
1 parent 572cbbc commit 8991a17

File tree

2 files changed

+64
-66
lines changed

2 files changed

+64
-66
lines changed

fluent-langneg/src/locale.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* eslint no-magic-numbers: 0 */
22

3-
import {getLikelySubtagsMin} from "./subtags";
4-
53
const languageCodeRe = "([a-z]{2,3}|\\*)";
64
const scriptCodeRe = "(?:-([a-z]{4}|\\*))";
75
const regionCodeRe = "(?:-([a-z]{2}|\\*))";
@@ -108,3 +106,67 @@ export class Locale {
108106
return false;
109107
}
110108
}
109+
110+
111+
/**
112+
* Below is a manually a list of likely subtags corresponding to Unicode
113+
* CLDR likelySubtags list.
114+
* This list is curated by the maintainers of Project Fluent and is
115+
* intended to be used in place of the full likelySubtags list in use cases
116+
* where full list cannot be (for example, due to the size).
117+
*
118+
* This version of the list is based on CLDR 30.0.3.
119+
*/
120+
const likelySubtagsMin: Record<string, string> = {
121+
"ar": "ar-arab-eg",
122+
"az-arab": "az-arab-ir",
123+
"az-ir": "az-arab-ir",
124+
"be": "be-cyrl-by",
125+
"da": "da-latn-dk",
126+
"el": "el-grek-gr",
127+
"en": "en-latn-us",
128+
"fa": "fa-arab-ir",
129+
"ja": "ja-jpan-jp",
130+
"ko": "ko-kore-kr",
131+
"pt": "pt-latn-br",
132+
"sr": "sr-cyrl-rs",
133+
"sr-ru": "sr-latn-ru",
134+
"sv": "sv-latn-se",
135+
"ta": "ta-taml-in",
136+
"uk": "uk-cyrl-ua",
137+
"zh": "zh-hans-cn",
138+
"zh-hant": "zh-hant-tw",
139+
"zh-hk": "zh-hant-hk",
140+
"zh-gb": "zh-hant-gb",
141+
"zh-us": "zh-hant-us",
142+
};
143+
144+
const regionMatchingLangs = [
145+
"az",
146+
"bg",
147+
"cs",
148+
"de",
149+
"es",
150+
"fi",
151+
"fr",
152+
"hu",
153+
"it",
154+
"lt",
155+
"lv",
156+
"nl",
157+
"pl",
158+
"ro",
159+
"ru",
160+
];
161+
162+
function getLikelySubtagsMin(loc: string): Locale | null {
163+
if (Object.prototype.hasOwnProperty.call(likelySubtagsMin, loc)) {
164+
return new Locale(likelySubtagsMin[loc]);
165+
}
166+
const locale = new Locale(loc);
167+
if (locale.language && regionMatchingLangs.includes(locale.language)) {
168+
locale.region = locale.language.toUpperCase();
169+
return locale;
170+
}
171+
return null;
172+
}

fluent-langneg/src/subtags.ts

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)