| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
from __future__ import annotations |
| 16 |
|
| 17 |
from re import fullmatch |
| 18 |
|
| 19 |
ANDROID_LOCALES = {"he": "iw", "id": "in", "yi": "ji"} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
def get_android_locale(locale: str) -> str: |
| 24 |
lc, *rest = locale.split("-") |
| 25 |
if lc in ANDROID_LOCALES: |
| 26 |
lc = ANDROID_LOCALES[lc] |
| 27 |
if not rest: |
| 28 |
return lc |
| 29 |
if len(rest) == 1 and fullmatch(r"[A-Z]{2}", rest[0]): |
| 30 |
return f"{lc}-r{rest[0]}" |
| 31 |
return f"b+{lc}+{'+'.join(rest)}" |
| 32 |
|
| 33 |
|
| 34 |
def parse_android_locale(alocale: str) -> str | None: |
| 35 |
if alocale.startswith("b+"): |
| 36 |
lc, *rest = alocale[2:].split("+") |
| 37 |
else: |
| 38 |
m = fullmatch(r"([a-z]{2,3})(?:-r([A-Z]{2}))?", alocale) |
| 39 |
if m is None: |
| 40 |
return None |
| 41 |
lc = m[1] |
| 42 |
rest = [m[2]] if m[2] else [] |
| 43 |
for current, legacy in ANDROID_LOCALES.items(): |
| 44 |
if lc == legacy: |
| 45 |
lc = current |
| 46 |
break |
| 47 |
return "-".join((lc, *rest)) |