gooderp18绿色标准版
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

105 líneas
3.4KB

  1. from __future__ import annotations
  2. import re
  3. from typing import TYPE_CHECKING, Literal, Optional, Sequence
  4. from babel import lists
  5. from odoo.tools.misc import babel_locale_parse, get_lang
  6. if TYPE_CHECKING:
  7. import odoo.api
  8. XPG_LOCALE_RE = re.compile(
  9. r"""^
  10. ([a-z]+) # language
  11. (_[A-Z\d]+)? # maybe _territory
  12. # no support for .codeset (we don't use that in Odoo)
  13. (@.+)? # maybe @modifier
  14. $""",
  15. re.VERBOSE,
  16. )
  17. def format_list(
  18. env: odoo.api.Environment,
  19. lst: Sequence[str],
  20. style: Literal["standard", "standard-short", "or", "or-short", "unit", "unit-short", "unit-narrow"] = "standard",
  21. lang_code: Optional[str] = None,
  22. ) -> str:
  23. """
  24. Format the items in `lst` as a list in a locale-dependent manner with the chosen style.
  25. The available styles are defined by babel according to the Unicode TR35-49 spec:
  26. * standard:
  27. A typical 'and' list for arbitrary placeholders.
  28. e.g. "January, February, and March"
  29. * standard-short:
  30. A short version of an 'and' list, suitable for use with short or abbreviated placeholder values.
  31. e.g. "Jan., Feb., and Mar."
  32. * or:
  33. A typical 'or' list for arbitrary placeholders.
  34. e.g. "January, February, or March"
  35. * or-short:
  36. A short version of an 'or' list.
  37. e.g. "Jan., Feb., or Mar."
  38. * unit:
  39. A list suitable for wide units.
  40. e.g. "3 feet, 7 inches"
  41. * unit-short:
  42. A list suitable for short units
  43. e.g. "3 ft, 7 in"
  44. * unit-narrow:
  45. A list suitable for narrow units, where space on the screen is very limited.
  46. e.g. "3′ 7″"
  47. See https://www.unicode.org/reports/tr35/tr35-49/tr35-general.html#ListPatterns for more details.
  48. :param env: the current environment.
  49. :param lst: the sequence of items to format into a list.
  50. :param style: the style to format the list with.
  51. :param lang_code: the locale (i.e. en_US).
  52. :return: the formatted list.
  53. """
  54. locale = babel_locale_parse(lang_code or get_lang(env).code)
  55. # Some styles could be unavailable for the chosen locale
  56. if style not in locale.list_patterns:
  57. style = "standard"
  58. return lists.format_list(lst, style, locale)
  59. def py_to_js_locale(locale: str) -> str:
  60. """
  61. Converts a locale from Python to JavaScript format.
  62. Most of the time the conversion is simply to replace _ with -.
  63. Example: fr_BE → fr-BE
  64. Exception: Serbian can be written in both Latin and Cyrillic scripts
  65. interchangeably, therefore its locale includes a special modifier
  66. to indicate which script to use.
  67. Example: sr@latin → sr-Latn
  68. BCP 47 (JS):
  69. language[-extlang][-script][-region][-variant][-extension][-privateuse]
  70. https://www.ietf.org/rfc/rfc5646.txt
  71. XPG syntax (Python):
  72. language[_territory][.codeset][@modifier]
  73. https://www.gnu.org/software/libc/manual/html_node/Locale-Names.html
  74. :param locale: The locale formatted for use on the Python-side.
  75. :return: The locale formatted for use on the JavaScript-side.
  76. """
  77. match_ = XPG_LOCALE_RE.match(locale)
  78. if not match_:
  79. return locale
  80. language, territory, modifier = match_.groups()
  81. subtags = [language]
  82. if modifier == "@Cyrl":
  83. subtags.append("Cyrl")
  84. elif modifier == "@latin":
  85. subtags.append("Latn")
  86. if territory:
  87. subtags.append(territory.removeprefix("_"))
  88. return "-".join(subtags)
上海开阖软件有限公司 沪ICP备12045867号-1