Replace cidr-localenames-full with Intl.DisplayNames
This commit is contained in:
parent
90b1e75ccb
commit
737c62bde5
|
|
@ -155,7 +155,6 @@ export class I18n {
|
||||||
// The language data itself
|
// The language data itself
|
||||||
private langs : Map<string, LanguageMetadata> = new Map<string, Language>();
|
private langs : Map<string, LanguageMetadata> = new Map<string, Language>();
|
||||||
private lang: Language = fallbackLanguage;
|
private lang: Language = fallbackLanguage;
|
||||||
private countryNames: {[key: string]: string} | null = null;
|
|
||||||
private languageDropdown: HTMLSpanElement = document.getElementById('languageDropdown') as HTMLSpanElement;
|
private languageDropdown: HTMLSpanElement = document.getElementById('languageDropdown') as HTMLSpanElement;
|
||||||
private emitter: Emitter<I18nEvents> = createNanoEvents();
|
private emitter: Emitter<I18nEvents> = createNanoEvents();
|
||||||
|
|
||||||
|
|
@ -164,6 +163,8 @@ export class I18n {
|
||||||
// the ID of the language
|
// the ID of the language
|
||||||
private langId: string = fallbackId;
|
private langId: string = fallbackId;
|
||||||
|
|
||||||
|
private regionNameRenderer = new Intl.DisplayNames(['en-US'], {type: 'region'});
|
||||||
|
|
||||||
async Init() {
|
async Init() {
|
||||||
// Load language list
|
// Load language list
|
||||||
var res = await fetch("lang/languages.json");
|
var res = await fetch("lang/languages.json");
|
||||||
|
|
@ -212,20 +213,8 @@ export class I18n {
|
||||||
this.ReplaceStaticStrings();
|
this.ReplaceStaticStrings();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getCountryNames(lang: string) : Promise<{[key: string]: string} | null> {
|
|
||||||
lang = lang.split('-')[0].toLowerCase();
|
|
||||||
let res = await fetch(`https://www.unpkg.com/cldr-localenames-full@45.0.0/main/${lang}/territories.json`);
|
|
||||||
if (!res.ok) {
|
|
||||||
console.error(`Failed to load territories.json for ${lang}: ${res.statusText}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
let data = await res.json();
|
|
||||||
return data.main[lang].localeDisplayNames.territories;
|
|
||||||
}
|
|
||||||
|
|
||||||
getCountryName(code: string) : string {
|
getCountryName(code: string) : string {
|
||||||
if (this.countryNames === null) return code;
|
return this.regionNameRenderer.of(code) || code;
|
||||||
return this.countryNames[code] || code;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async SetLanguage(id: string) {
|
private async SetLanguage(id: string) {
|
||||||
|
|
@ -247,10 +236,13 @@ export class I18n {
|
||||||
|
|
||||||
this.lang = lang;
|
this.lang = lang;
|
||||||
|
|
||||||
this.countryNames = await this.getCountryNames(id);
|
if (this.langId != lastId) {
|
||||||
|
// Replace static strings
|
||||||
|
this.ReplaceStaticStrings();
|
||||||
|
|
||||||
// Only replace static strings
|
// Update region name renderer target language
|
||||||
if (this.langId != lastId) this.ReplaceStaticStrings();
|
this.regionNameRenderer = new Intl.DisplayNames([this.langId], {type: 'region'});
|
||||||
|
};
|
||||||
|
|
||||||
// Set the language ID localstorage entry
|
// Set the language ID localstorage entry
|
||||||
if (this.langId !== fallbackId) {
|
if (this.langId !== fallbackId) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user