Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .sampo/changesets/centralize-default-locale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
pypi/generaltranslation: patch
pypi/gt-fastapi: patch
pypi/gt-flask: patch
pypi/generaltranslation-intl-messageformat: patch
---

Centralize hardcoded `"en"` locale defaults into a `LIBRARY_DEFAULT_LOCALE` constant. No behavioral change.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from babel.numbers import format_decimal
from generaltranslation_icu_messageformat_parser import Parser

LIBRARY_DEFAULT_LOCALE = "en"

_parser = Parser()


Expand All @@ -27,13 +29,13 @@ class IntlMessageFormat:
mf.format({"count": 5}) # "5 items"
"""

def __init__(self, pattern: str, locale: str = "en") -> None:
def __init__(self, pattern: str, locale: str = LIBRARY_DEFAULT_LOCALE) -> None:
self._pattern = pattern
self._ast = _parser.parse(pattern)
try:
self._locale = Locale.parse(locale, sep="-")
except (UnknownLocaleError, ValueError):
self._locale = Locale("en")
self._locale = Locale(LIBRARY_DEFAULT_LOCALE)

@property
def pattern(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from babel import Locale
from babel.core import UnknownLocaleError

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE


def _resolve_babel_locale(locales: str | list[str] | None = None) -> Locale:
"""Convert BCP 47 locale(s) to a Babel :class:`Locale` instance.
Expand All @@ -22,15 +24,15 @@ def _resolve_babel_locale(locales: str | list[str] | None = None) -> Locale:
A :class:`babel.Locale` instance.
"""
if locales is None:
return Locale("en")
return Locale(LIBRARY_DEFAULT_LOCALE)
if isinstance(locales, str):
locales = [locales]
for tag in locales:
try:
return Locale.parse(tag, sep="-")
except (UnknownLocaleError, ValueError):
continue
return Locale("en")
return Locale(LIBRARY_DEFAULT_LOCALE)


def _get_language_code(locales: str | list[str] | None = None) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@

from babel import Locale

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE
from generaltranslation.locales._types import CustomMapping


def get_locale_name(
locale: str,
default_locale: str | None = "en",
default_locale: str | None = LIBRARY_DEFAULT_LOCALE,
custom_mapping: CustomMapping | None = None,
) -> str:
"""Return the display name of *locale* rendered in *default_locale*.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from babel import Locale
from babel.core import get_global

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE
from generaltranslation.locales._types import CustomMapping, LocaleProperties

_likely_subtags: dict[str, str] = dict(get_global("likely_subtags"))
Expand Down Expand Up @@ -105,7 +106,7 @@ def _build_component_name(

def get_locale_properties(
locale: str,
default_locale: str | None = "en",
default_locale: str | None = LIBRARY_DEFAULT_LOCALE,
custom_mapping: CustomMapping | None = None,
) -> LocaleProperties:
"""Return a :class:`LocaleProperties` for *locale*."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from babel import Locale

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE
from generaltranslation.locales._types import PLURAL_FORMS, PluralType

# Aliases between our custom form names and CLDR form names
Expand Down Expand Up @@ -54,7 +55,7 @@ def get_plural_form(
if forms is None:
forms = list(PLURAL_FORMS)
if locales is None:
locales = ["en"]
locales = [LIBRARY_DEFAULT_LOCALE]

forms_set = set(forms)

Expand Down Expand Up @@ -95,7 +96,7 @@ def _get_cldr_category(n: int | float, locales: list[str]) -> str:
continue
# Default to English rules
try:
rule = Locale("en").plural_form
rule = Locale(LIBRARY_DEFAULT_LOCALE).plural_form
return rule(abs(n))
except Exception:
return "other"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

from babel import Locale

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE
from generaltranslation.locales._get_locale_emoji import DEFAULT_EMOJI, EMOJIS
from generaltranslation.locales._types import CustomRegionMapping


def get_region_properties(
region: str,
default_locale: str | None = "en",
default_locale: str | None = LIBRARY_DEFAULT_LOCALE,
custom_mapping: CustomRegionMapping | None = None,
) -> dict[str, str]:
"""Return metadata for a region code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from babel import Locale
from babel.core import UnknownLocaleError

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE
from generaltranslation.locales._types import CustomMapping

# Scripts that are valid but may not be recognised by all display-name APIs.
Expand Down Expand Up @@ -109,7 +110,7 @@ def is_valid_locale(
# For region codes, verify they're known
if parsed.territory:
try:
display_locale = Locale("en")
display_locale = Locale(LIBRARY_DEFAULT_LOCALE)
if parsed.territory not in display_locale.territories:
return False
except Exception:
Expand All @@ -119,7 +120,7 @@ def is_valid_locale(
if parsed.script:
if parsed.script not in SCRIPT_EXCEPTIONS:
try:
display_locale = Locale("en")
display_locale = Locale(LIBRARY_DEFAULT_LOCALE)
if parsed.script not in display_locale.scripts:
return False
except Exception:
Expand Down
3 changes: 2 additions & 1 deletion packages/gt-fastapi/src/gt_fastapi/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from contextlib import asynccontextmanager
from typing import Any

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE
from generaltranslation.locales import determine_locale
from gt_i18n import I18nManager, set_i18n_manager, t # noqa: F401

Expand Down Expand Up @@ -45,7 +46,7 @@ def _detect_from_accept_language(request: Any, manager: I18nManager) -> str:
def initialize_gt(
app: Any,
*,
default_locale: str = "en",
default_locale: str = LIBRARY_DEFAULT_LOCALE,
locales: list[str] | None = None,
project_id: str | None = None,
cache_url: str | None = None,
Expand Down
3 changes: 2 additions & 1 deletion packages/gt-flask/src/gt_flask/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from collections.abc import Callable
from typing import Any

from generaltranslation._settings import LIBRARY_DEFAULT_LOCALE
from generaltranslation.locales import determine_locale
from gt_i18n import I18nManager, set_i18n_manager, t # noqa: F401

Expand Down Expand Up @@ -47,7 +48,7 @@ def _detect_from_accept_language(request: Any, manager: I18nManager) -> str:
def initialize_gt(
app: Any,
*,
default_locale: str = "en",
default_locale: str = LIBRARY_DEFAULT_LOCALE,
locales: list[str] | None = None,
project_id: str | None = None,
cache_url: str | None = None,
Expand Down