lang.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. from gtts.langs import _main_langs
  3. from warnings import warn
  4. import logging
  5. __all__ = ["tts_langs"]
  6. # Logger
  7. log = logging.getLogger(__name__)
  8. log.addHandler(logging.NullHandler())
  9. def tts_langs():
  10. """Languages Google Text-to-Speech supports.
  11. Returns:
  12. dict: A dictionary of the type `{ '<lang>': '<name>'}`
  13. Where `<lang>` is an IETF language tag such as `en` or `zh-TW`,
  14. and `<name>` is the full English name of the language, such as
  15. `English` or `Chinese (Mandarin/Taiwan)`.
  16. The dictionary returned combines languages from two origins:
  17. - Languages fetched from Google Translate (pre-generated in :mod:`gtts.langs`)
  18. - Languages that are undocumented variations that were observed to work and
  19. present different dialects or accents.
  20. """
  21. langs = dict()
  22. langs.update(_main_langs())
  23. langs.update(_extra_langs())
  24. log.debug("langs: {}".format(langs))
  25. return langs
  26. def _extra_langs():
  27. """Define extra languages.
  28. Returns:
  29. dict: A dictionary of extra languages manually defined.
  30. Variations of the ones generated in `_main_langs`,
  31. observed to provide different dialects or accents or
  32. just simply accepted by the Google Translate Text-to-Speech API.
  33. """
  34. return {
  35. # Chinese
  36. "zh-TW": "Chinese (Mandarin/Taiwan)",
  37. "zh": "Chinese (Mandarin)",
  38. }
  39. def _fallback_deprecated_lang(lang):
  40. """Languages Google Text-to-Speech used to support.
  41. Language tags that don't work anymore, but that can
  42. fallback to a more general language code to maintain
  43. compatibility.
  44. Args:
  45. lang (string): The language tag.
  46. Returns:
  47. string: The language tag, as-is if not deprecated,
  48. or a fallback if it exits.
  49. Example:
  50. ``en-GB`` returns ``en``.
  51. ``en-gb`` returns ``en``.
  52. """
  53. deprecated = {
  54. # '<fallback>': [<list of deprecated langs>]
  55. "en": [
  56. "en-us",
  57. "en-ca",
  58. "en-uk",
  59. "en-gb",
  60. "en-au",
  61. "en-gh",
  62. "en-in",
  63. "en-ie",
  64. "en-nz",
  65. "en-ng",
  66. "en-ph",
  67. "en-za",
  68. "en-tz",
  69. ],
  70. "fr": ["fr-ca", "fr-fr"],
  71. "pt": ["pt-br", "pt-pt"],
  72. "es": ["es-es", "es-us"],
  73. "zh-CN": ["zh-cn"],
  74. "zh-TW": ["zh-tw"],
  75. }
  76. for fallback_lang, deprecated_langs in deprecated.items():
  77. if lang.lower() in deprecated_langs:
  78. msg = (
  79. "'{}' has been deprecated, falling back to '{}'. "
  80. "This fallback will be removed in a future version."
  81. ).format(lang, fallback_lang)
  82. warn(msg, DeprecationWarning)
  83. log.warning(msg)
  84. return fallback_lang
  85. return lang