languages.py 2.8 KB

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