Repository navigation
Expand file tree
/
Copy pathi18n.py
More file actions
105 lines (89 loc) · 3.41 KB
/
Copy pathi18n.py
File metadata and controls
105 lines (89 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from __future__ import annotations
import re
from typing import TYPE_CHECKING, Literal
from babel import lists
from odoo.tools.misc import babel_locale_parse, get_lang
if TYPE_CHECKING:
from collections.abc import Iterable
import odoo.api
XPG_LOCALE_RE = re.compile(
r"""^
([a-z]+) # language
(_[A-Z\d]+)? # maybe _territory
# no support for .codeset (we don't use that in Odoo)
(@.+)? # maybe @modifier
$""",
re.VERBOSE,
)
def format_list(
env: odoo.api.Environment,
lst: Iterable,
style: Literal["standard", "standard-short", "or", "or-short", "unit", "unit-short", "unit-narrow"] = "standard",
lang_code: str | None = None,
) -> str:
"""
Format the items in `lst` as a list in a locale-dependent manner with the chosen style.
The available styles are defined by babel according to the Unicode TR35-49 spec:
* standard:
A typical 'and' list for arbitrary placeholders.
e.g. "January, February, and March"
* standard-short:
A short version of an 'and' list, suitable for use with short or abbreviated placeholder values.
e.g. "Jan., Feb., and Mar."
* or:
A typical 'or' list for arbitrary placeholders.
e.g. "January, February, or March"
* or-short:
A short version of an 'or' list.
e.g. "Jan., Feb., or Mar."
* unit:
A list suitable for wide units.
e.g. "3 feet, 7 inches"
* unit-short:
A list suitable for short units
e.g. "3 ft, 7 in"
* unit-narrow:
A list suitable for narrow units, where space on the screen is very limited.
e.g. "3′ 7″"
See https://www.unicode.org/reports/tr35/tr35-49/tr35-general.html#ListPatterns for more details.
:param env: the current environment.
:param lst: the iterable of items to format into a list.
:param style: the style to format the list with.
:param lang_code: the locale (i.e. en_US).
:return: the formatted list.
"""
locale = babel_locale_parse(lang_code or get_lang(env).code)
# Some styles could be unavailable for the chosen locale
if style not in locale.list_patterns:
style = "standard"
return lists.format_list([str(el) for el in lst], style, locale)
def py_to_js_locale(locale: str) -> str:
"""
Converts a locale from Python to JavaScript format.
Most of the time the conversion is simply to replace _ with -.
Example: fr_BE → fr-BE
Exception: Serbian can be written in both Latin and Cyrillic scripts
interchangeably, therefore its locale includes a special modifier
to indicate which script to use.
Example: sr@latin → sr-Latn
BCP 47 (JS):
language[-extlang][-script][-region][-variant][-extension][-privateuse]
https://www.ietf.org/rfc/rfc5646.txt
XPG syntax (Python):
language[_territory][.codeset][@modifier]
https://www.gnu.org/software/libc/manual/html_node/Locale-Names.html
:param locale: The locale formatted for use on the Python-side.
:return: The locale formatted for use on the JavaScript-side.
"""
match_ = XPG_LOCALE_RE.match(locale)
if not match_:
return locale
language, territory, modifier = match_.groups()
subtags = [language]
if modifier == "@Cyrl":
subtags.append("Cyrl")
elif modifier == "@latin":
subtags.append("Latn")
if territory:
subtags.append(territory.removeprefix("_"))
return "-".join(subtags)