Debian Codegraph @

babel/languages.py

raw
1 from __future__ import annotations
2
3 from babel.core import get_global
4
5
6 def get_official_languages(
7 territory: str,
8 regional: bool = False,
9 de_facto: bool = False,
10 ) -> tuple[str, ...]:
11 """
12 Get the official language(s) for the given territory.
13
14 The language codes, if any are known, are returned in order of descending popularity.
15
16 If the `regional` flag is set, then languages which are regionally official are also returned.
17
18 If the `de_facto` flag is set, then languages which are "de facto" official are also returned.
19
20 .. warning:: Note that the data is as up to date as the current version of the CLDR used
21 by Babel. If you need scientifically accurate information, use another source!
22
23 :param territory: Territory code
24 :param regional: Whether to return regionally official languages too
25 :param de_facto: Whether to return de-facto official languages too
26 :return: Tuple of language codes
27 """
28
29 territory = str(territory).upper()
30 allowed_stati = {"official"}
31 if regional:
32 allowed_stati.add("official_regional")
33 if de_facto:
34 allowed_stati.add("de_facto_official")
35
36 languages = get_global("territory_languages").get(territory, {})
37 pairs = [
38 (info['population_percent'], language)
39 for language, info in languages.items()
40 if info.get('official_status') in allowed_stati
41 ]
42 pairs.sort(reverse=True)
43 return tuple(lang for _, lang in pairs)
44
45
46 def get_territory_language_info(
47 territory: str,
48 ) -> dict[str, dict[str, float | str | None]]:
49 """
50 Get a dictionary of language information for a territory.
51
52 The dictionary is keyed by language code; the values are dicts with more information.
53
54 The following keys are currently known for the values:
55
56 * `population_percent`: The percentage of the territory's population speaking the
57 language.
58 * `official_status`: An optional string describing the officiality status of the language.
59 Known values are "official", "official_regional" and "de_facto_official".
60
61 .. warning:: Note that the data is as up to date as the current version of the CLDR used
62 by Babel. If you need scientifically accurate information, use another source!
63
64 .. note:: Note that the format of the dict returned may change between Babel versions.
65
66 See https://www.unicode.org/cldr/charts/latest/supplemental/territory_language_information.html
67
68 :param territory: Territory code
69 :return: Language information dictionary
70 """
71 territory = str(territory).upper()
72 return get_global("territory_languages").get(territory, {}).copy()