| 1 |
/** |
| 2 |
* Converts a locale from JavaScript to Python format. |
| 3 |
* |
| 4 |
* Most of the time the conversion is simply to replace - with _. |
| 5 |
* Example: fr-BE → fr_BE |
| 6 |
* |
| 7 |
* Exceptions: |
| 8 |
* - Serbian can be written in both Latin and Cyrillic scripts interchangeably, |
| 9 |
* therefore its locale includes a special modifier to indicate which script to |
| 10 |
* use. Example: sr-Latn → sr@latin |
| 11 |
* - Tagalog/Filipino: The "fil" locale is replaced by "tl" for compatibility |
| 12 |
* with the Python side (where the "fil" locale doesn't exist). |
| 13 |
* |
| 14 |
* BCP 47 (JS): |
| 15 |
* language[-extlang][-script][-region][-variant][-extension][-privateuse] |
| 16 |
* https://www.ietf.org/rfc/rfc5646.txt |
| 17 |
* XPG syntax (Python): |
| 18 |
* language[_territory][.codeset][@modifier] |
| 19 |
* https://www.gnu.org/software/libc/manual/html_node/Locale-Names.html |
| 20 |
* |
| 21 |
* @param {string} locale The locale formatted for use on the JavaScript-side. |
| 22 |
* @returns {string} The locale formatted for use on the Python-side. |
| 23 |
*/ |
| 24 |
export function jsToPyLocale(locale) { |
| 25 |
if (!locale) { |
| 26 |
return ""; |
| 27 |
} |
| 28 |
try { |
| 29 |
var { language, script, region } = new Intl.Locale(locale); |
| 30 |
// new Intl.Locale("tl-PH") produces fil-PH, which one might not expect |
| 31 |
if (language === "fil") { |
| 32 |
language = "tl"; |
| 33 |
} |
| 34 |
} catch { |
| 35 |
return locale; |
| 36 |
} |
| 37 |
let xpgLocale = language; |
| 38 |
if (region) { |
| 39 |
xpgLocale += `_${region}`; |
| 40 |
} |
| 41 |
switch (script) { |
| 42 |
case "Cyrl": |
| 43 |
xpgLocale += "@Cyrl"; |
| 44 |
break; |
| 45 |
case "Latn": |
| 46 |
xpgLocale += "@latin"; |
| 47 |
break; |
| 48 |
} |
| 49 |
return xpgLocale; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Converts a locale from Python to JavaScript format. |
| 54 |
* |
| 55 |
* Most of the time the conversion is simply to replace _ with -. |
| 56 |
* Example: fr_BE → fr-BE |
| 57 |
* |
| 58 |
* Exception: Serbian can be written in both Latin and Cyrillic scripts |
| 59 |
* interchangeably, therefore its locale includes a special modifier |
| 60 |
* to indicate which script to use. |
| 61 |
* Example: sr@latin → sr-Latn |
| 62 |
* |
| 63 |
* BCP 47 (JS): |
| 64 |
* language[-extlang][-script][-region][-variant][-extension][-privateuse] |
| 65 |
* https://www.ietf.org/rfc/rfc5646.txt |
| 66 |
* XPG syntax (Python): |
| 67 |
* language[_territory][.codeset][@modifier] |
| 68 |
* https://www.gnu.org/software/libc/manual/html_node/Locale-Names.html |
| 69 |
* |
| 70 |
* @param {string} locale The locale formatted for use on the Python-side. |
| 71 |
* @returns {string} The locale formatted for use on the JavaScript-side. |
| 72 |
*/ |
| 73 |
export function pyToJsLocale(locale) { |
| 74 |
if (!locale) { |
| 75 |
return ""; |
| 76 |
} |
| 77 |
const regex = /^([a-z]+)(_[A-Z\d]+)?(@.+)?$/; |
| 78 |
const match = locale.match(regex); |
| 79 |
if (!match) { |
| 80 |
return locale; |
| 81 |
} |
| 82 |
const [, language, territory, modifier] = match; |
| 83 |
const subtags = [language]; |
| 84 |
switch (modifier) { |
| 85 |
case "@Cyrl": |
| 86 |
subtags.push("Cyrl"); |
| 87 |
break; |
| 88 |
case "@latin": |
| 89 |
subtags.push("Latn"); |
| 90 |
break; |
| 91 |
} |
| 92 |
if (territory) { |
| 93 |
subtags.push(territory.slice(1)); |
| 94 |
} |
| 95 |
return subtags.join("-"); |
| 96 |
} |