pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #726 from movie-web/language-code-fixes

Language code fixes

authored by

William Oldham and committed by
GitHub
88bc1aa6 04906f1f

+46 -40
+3 -3
src/assets/README.md
··· 3 3 Locales are difficult, here is some guidance. 4 4 5 5 ## Process on adding new languages 6 - 1. Use weblate to add translations, see contributing guidelines. 6 + 1. Use Weblate to add translations, see contributing guidelines. 7 7 2. Add your language to `@/assets/languages.ts`. Must be in ISO format (ISO-639 for language and ISO-3166 for country/region). For joke languages, use any format. 8 - 3. If your language doesn't have a region specified (Such as in `pt-BR`, `BR` being the region). Add a default region in `@/utils/language.ts` at `defaultLanguageCodes` 9 - 4. If the flag in the language dropdown doesn't match the correct one. Add a default country in `@/utils/language.ts` at `countryPriority`. 8 + 3. If the language code doesn't have a region specified (Such as in `pt-BR`, `BR` being the region), add a default region in `@/utils/language.ts` at `defaultLanguageCodes` 9 + 4. If the language code doesn't contain a region (Such as in `zh-Hant`), add a default country in `@/utils/language.ts` at `countryPriority`.
+43 -37
src/utils/language.ts
··· 5 5 6 6 // mapping of language code to country code. 7 7 // multiple mappings can exist, since languages are spoken in multiple countries. 8 - // This mapping purely exists to prioritize a country over another in languages. 8 + // This mapping purely exists to prioritize a country over another in languages where the base language code does 9 + // 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 9 10 // iso639_1 -> iso3166 Alpha-2 10 11 const countryPriority: Record<string, string> = { 11 - en: "us", 12 - nl: "nl", 13 - fr: "fr", 14 - de: "de", 15 - pt: "pt", 16 - ar: "sa", 17 - es: "es", 18 12 zh: "cn", 19 - ko: "kr", 20 - ta: "lk", 21 13 }; 22 14 23 15 // list of iso639_1 Alpha-2 codes used as default languages 24 16 const defaultLanguageCodes: string[] = [ 25 - "en-US", 26 - "cs-CZ", 27 - "de-DE", 28 - "fr-FR", 29 - "pt-BR", 30 - "it-IT", 31 - "nl-NL", 32 - "pl-PL", 33 - "tr-TR", 34 - "vi-VN", 35 - "zh-CN", 36 - "he-IL", 37 - "sv-SE", 38 - "lv-LV", 39 - "th-TH", 40 - "ne-NP", 41 17 "ar-SA", 42 - "es-ES", 43 - "et-EE", 44 18 "bg-BG", 45 19 "bn-BD", 20 + "cs-CZ", 21 + "de-DE", 46 22 "el-GR", 23 + "en-US", 24 + "es-ES", 25 + "et-EE", 47 26 "fa-IR", 27 + "fr-FR", 28 + "gl-ES", 48 29 "gu-IN", 30 + "he-IL", 49 31 "id-ID", 32 + "it-IT", 50 33 "ja-JP", 51 34 "ko-KR", 35 + "lv-LV", 36 + "ne-NP", 37 + "nl-NL", 38 + "pl-PL", 39 + "pt-BR", 40 + "ru-RU", 52 41 "sl-SI", 42 + "sv-SE", 53 43 "ta-LK", 54 - "ru-RU", 55 - "gl-ES", 44 + "th-TH", 45 + "tr-TR", 46 + "vi-VN", 47 + "zh-CN", 56 48 ]; 57 49 58 50 export interface LocaleInfo { ··· 89 81 } 90 82 91 83 /** 92 - * @param locale idk what kinda code this takes, anytihhng in ietf format I guess 84 + * @param locale idk what kinda code this takes, anything in ietf format I guess 93 85 * @returns pretty format for language, null if it no info can be found for language 94 86 */ 95 87 export function getPrettyLanguageNameFromLocale(locale: string): string | null { ··· 105 97 } 106 98 107 99 /** 108 - * Sort locale codes by occurance, rest on alphabetical order 100 + * Sort locale codes by occurrence, rest on alphabetical order 109 101 * @param langCodes list language codes to sort 110 102 * @returns sorted version of inputted list 111 103 */ 112 104 export function sortLangCodes(langCodes: string[]) { 113 - const languagesOrder = [...languageOrder].reverse(); // Reverse is neccesary, not sure why 105 + const languagesOrder = [...languageOrder].reverse(); // Reverse is necessary, not sure why 114 106 115 107 const results = langCodes.sort((a, b) => { 116 108 const langOrderA = languagesOrder.findIndex( ··· 134 126 */ 135 127 export function getCountryCodeForLocale(locale: string): string | null { 136 128 let output: LanguageObj | null = null as any as LanguageObj; 137 - const tag = getTag(locale, true); 129 + const tag = getTag(populateLanguageCode(locale), true); 138 130 139 131 if (!tag?.language?.Subtag) return null; 140 - // this function isnt async, so its garuanteed to work like this 132 + // this function isn't async, so its guaranteed to work like this 141 133 countryLanguages.getLanguage(tag.language.Subtag, (_err, lang) => { 142 134 if (lang) output = lang; 143 135 }); 136 + 144 137 if (!output) return null; 145 138 const priority = countryPriority[output.iso639_1.toLowerCase()]; 146 139 if (output.countries.length === 0) { 147 140 return priority ?? null; 148 141 } 142 + 149 143 if (priority) { 150 - const priotizedCountry = output.countries.find( 144 + const prioritizedCountry = output.countries.find( 151 145 (v) => v.code_2.toLowerCase() === priority, 152 146 ); 153 - if (priotizedCountry) return priotizedCountry.code_2.toLowerCase(); 147 + if (prioritizedCountry) return prioritizedCountry.code_2.toLowerCase(); 148 + } 149 + 150 + // If the language contains a region, check that against the countries and 151 + // return the region if it matches 152 + const regionSubtag = tag?.region?.Subtag.toLowerCase(); 153 + if (regionSubtag) { 154 + const regionCode = output.countries.find( 155 + (c) => 156 + c.code_2.toLowerCase() === regionSubtag || 157 + c.code_3.toLowerCase() === regionSubtag, 158 + ); 159 + if (regionCode) return regionCode.code_2.toLowerCase(); 154 160 } 155 161 return output.countries[0].code_2.toLowerCase(); 156 162 }