@ -5,54 +5,46 @@ const languageOrder = ["en", "hi", "fr", "de", "nl", "pt"];
// mapping of language code to country code.
// mapping of language code to country code.
// multiple mappings can exist, since languages are spoken in multiple countries.
// multiple mappings can exist, since languages are spoken in multiple countries.
// This mapping purely exists to prioritize a country over another in languages.
// This mapping purely exists to prioritize a country over another in languages where the base language code does
// not contain a region (i.e. if the language code is zh-Hant where Hant is a script) or if the region in the language code is incorrect
// iso639_1 -> iso3166 Alpha-2
// iso639_1 -> iso3166 Alpha-2
const countryPriority : Record < string , string > = {
const countryPriority : Record < string , string > = {
en : "us" ,
nl : "nl" ,
fr : "fr" ,
de : "de" ,
pt : "pt" ,
ar : "sa" ,
es : "es" ,
zh : "cn" ,
zh : "cn" ,
ko : "kr" ,
ta : "lk" ,
} ;
} ;
// list of iso639_1 Alpha-2 codes used as default languages
// list of iso639_1 Alpha-2 codes used as default languages
const defaultLanguageCodes : string [ ] = [
const defaultLanguageCodes : string [ ] = [
"en-US" ,
"cs-CZ" ,
"de-DE" ,
"fr-FR" ,
"pt-BR" ,
"it-IT" ,
"nl-NL" ,
"pl-PL" ,
"tr-TR" ,
"vi-VN" ,
"zh-CN" ,
"he-IL" ,
"sv-SE" ,
"lv-LV" ,
"th-TH" ,
"ne-NP" ,
"ar-SA" ,
"ar-SA" ,
"es-ES" ,
"et-EE" ,
"bg-BG" ,
"bg-BG" ,
"bn-BD" ,
"bn-BD" ,
"cs-CZ" ,
"de-DE" ,
"el-GR" ,
"el-GR" ,
"en-US" ,
"es-ES" ,
"et-EE" ,
"fa-IR" ,
"fa-IR" ,
"fr-FR" ,
"gl-ES" ,
"gu-IN" ,
"gu-IN" ,
"he-IL" ,
"id-ID" ,
"id-ID" ,
"it-IT" ,
"ja-JP" ,
"ja-JP" ,
"ko-KR" ,
"ko-KR" ,
"lv-LV" ,
"ne-NP" ,
"nl-NL" ,
"pl-PL" ,
"pt-BR" ,
"ru-RU" ,
"sl-SI" ,
"sl-SI" ,
"sv-SE" ,
"ta-LK" ,
"ta-LK" ,
"ru-RU" ,
"th-TH" ,
"gl-ES" ,
"tr-TR" ,
"vi-VN" ,
"zh-CN" ,
] ;
] ;
export interface LocaleInfo {
export interface LocaleInfo {
@ -89,7 +81,7 @@ function populateLanguageCode(language: string): string {
}
}
/ * *
/ * *
* @param locale idk what kinda code this takes , anytihh ng in ietf format I guess
* @param locale idk what kinda code this takes , anythi ng in ietf format I guess
* @returns pretty format for language , null if it no info can be found for language
* @returns pretty format for language , null if it no info can be found for language
* /
* /
export function getPrettyLanguageNameFromLocale ( locale : string ) : string | null {
export function getPrettyLanguageNameFromLocale ( locale : string ) : string | null {
@ -105,12 +97,12 @@ export function getPrettyLanguageNameFromLocale(locale: string): string | null {
}
}
/ * *
/ * *
* Sort locale codes by occura nce , rest on alphabetical order
* Sort locale codes by occurre nce , rest on alphabetical order
* @param langCodes list language codes to sort
* @param langCodes list language codes to sort
* @returns sorted version of inputted list
* @returns sorted version of inputted list
* /
* /
export function sortLangCodes ( langCodes : string [ ] ) {
export function sortLangCodes ( langCodes : string [ ] ) {
const languagesOrder = [ . . . languageOrder ] . reverse ( ) ; // Reverse is necc esary, not sure why
const languagesOrder = [ . . . languageOrder ] . reverse ( ) ; // Reverse is neces sary, not sure why
const results = langCodes . sort ( ( a , b ) = > {
const results = langCodes . sort ( ( a , b ) = > {
const langOrderA = languagesOrder . findIndex (
const langOrderA = languagesOrder . findIndex (
@ -134,23 +126,37 @@ export function sortLangCodes(langCodes: string[]) {
* /
* /
export function getCountryCodeForLocale ( locale : string ) : string | null {
export function getCountryCodeForLocale ( locale : string ) : string | null {
let output : LanguageObj | null = null as any as LanguageObj ;
let output : LanguageObj | null = null as any as LanguageObj ;
const tag = getTag ( locale , true ) ;
const tag = getTag ( populateLanguageCode ( locale ) , true ) ;
if ( ! tag ? . language ? . Subtag ) return null ;
if ( ! tag ? . language ? . Subtag ) return null ;
// this function isnt async, so its garu anteed to work like this
// this function isn' t async, so its gu aranteed to work like this
countryLanguages . getLanguage ( tag . language . Subtag , ( _err , lang ) = > {
countryLanguages . getLanguage ( tag . language . Subtag , ( _err , lang ) = > {
if ( lang ) output = lang ;
if ( lang ) output = lang ;
} ) ;
} ) ;
if ( ! output ) return null ;
if ( ! output ) return null ;
const priority = countryPriority [ output . iso639_1 . toLowerCase ( ) ] ;
const priority = countryPriority [ output . iso639_1 . toLowerCase ( ) ] ;
if ( output . countries . length === 0 ) {
if ( output . countries . length === 0 ) {
return priority ? ? null ;
return priority ? ? null ;
}
}
if ( priority ) {
if ( priority ) {
const priotizedCountry = output . countries . find (
const priori tizedCountry = output . countries . find (
( v ) = > v . code_2 . toLowerCase ( ) === priority ,
( v ) = > v . code_2 . toLowerCase ( ) === priority ,
) ;
) ;
if ( priotizedCountry ) return priotizedCountry . code_2 . toLowerCase ( ) ;
if ( prioritizedCountry ) return prioritizedCountry . code_2 . toLowerCase ( ) ;
}
// If the language contains a region, check that against the countries and
// return the region if it matches
const regionSubtag = tag ? . region ? . Subtag . toLowerCase ( ) ;
if ( regionSubtag ) {
const regionCode = output . countries . find (
( c ) = >
c . code_2 . toLowerCase ( ) === regionSubtag ||
c . code_3 . toLowerCase ( ) === regionSubtag ,
) ;
if ( regionCode ) return regionCode . code_2 . toLowerCase ( ) ;
}
}
return output . countries [ 0 ] . code_2 . toLowerCase ( ) ;
return output . countries [ 0 ] . code_2 . toLowerCase ( ) ;
}
}