update_locales.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python
  2. # lint: pylint
  3. # SPDX-License-Identifier: AGPL-3.0-or-later
  4. """Update locale names in :origin:`searx/data/locales.json` used by
  5. :ref:`searx.locales`
  6. - :py:obj:`searx.locales.RTL_LOCALES`
  7. - :py:obj:`searx.locales.LOCALE_NAMES`
  8. """
  9. # pylint: disable=invalid-name
  10. from __future__ import annotations
  11. from typing import Set
  12. import json
  13. from pathlib import Path
  14. import babel
  15. import babel.languages
  16. import babel.core
  17. from searx import searx_dir
  18. from searx.locales import (
  19. ADDITIONAL_TRANSLATIONS,
  20. LOCALE_BEST_MATCH,
  21. get_translation_locales,
  22. )
  23. LOCALE_DATA_FILE = Path(searx_dir) / 'data' / 'locales.json'
  24. TRANSLATOINS_FOLDER = Path(searx_dir) / 'translations'
  25. def main():
  26. LOCALE_NAMES = {}
  27. RTL_LOCALES: Set[str] = set()
  28. for tag, descr in ADDITIONAL_TRANSLATIONS.items():
  29. locale = babel.Locale.parse(LOCALE_BEST_MATCH[tag], sep='-')
  30. LOCALE_NAMES[tag] = descr
  31. if locale.text_direction == 'rtl':
  32. RTL_LOCALES.add(tag)
  33. for tag in LOCALE_BEST_MATCH:
  34. descr = LOCALE_NAMES.get(tag)
  35. if not descr:
  36. locale = babel.Locale.parse(tag, sep='-')
  37. LOCALE_NAMES[tag] = get_locale_descr(locale, tag.replace('-', '_'))
  38. if locale.text_direction == 'rtl':
  39. RTL_LOCALES.add(tag)
  40. for tr_locale in get_translation_locales():
  41. sxng_tag = tr_locale.replace('_', '-')
  42. descr = LOCALE_NAMES.get(sxng_tag)
  43. if not descr:
  44. locale = babel.Locale.parse(tr_locale)
  45. LOCALE_NAMES[sxng_tag] = get_locale_descr(locale, tr_locale)
  46. if locale.text_direction == 'rtl':
  47. RTL_LOCALES.add(sxng_tag)
  48. content = {
  49. "LOCALE_NAMES": LOCALE_NAMES,
  50. "RTL_LOCALES": sorted(RTL_LOCALES),
  51. }
  52. with LOCALE_DATA_FILE.open('w', encoding='utf-8') as f:
  53. json.dump(content, f, indent=2, sort_keys=True, ensure_ascii=False)
  54. def get_locale_descr(locale: babel.Locale, tr_locale):
  55. """Get locale name e.g. 'Français - fr' or 'Português (Brasil) - pt-BR'
  56. :param locale: instance of :py:class:`Locale`
  57. :param tr_locale: name e.g. 'fr' or 'pt_BR' (delimiter is *underscore*)
  58. """
  59. native_language, native_territory = _get_locale_descr(locale, tr_locale)
  60. english_language, english_territory = _get_locale_descr(locale, 'en')
  61. if native_territory == english_territory:
  62. english_territory = None
  63. if not native_territory and not english_territory:
  64. # none territory name
  65. if native_language == english_language:
  66. return native_language
  67. return native_language + ' (' + english_language + ')'
  68. result = native_language + ', ' + native_territory + ' (' + english_language
  69. if english_territory:
  70. return result + ', ' + english_territory + ')'
  71. return result + ')'
  72. def _get_locale_descr(locale: babel.Locale, tr_locale: str) -> tuple[str, str]:
  73. language_name = locale.get_language_name(tr_locale).capitalize() # type: ignore
  74. if language_name and ('a' <= language_name[0] <= 'z'):
  75. language_name = language_name.capitalize()
  76. territory_name: str = locale.get_territory_name(tr_locale) # type: ignore
  77. return language_name, territory_name
  78. if __name__ == "__main__":
  79. main()