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