Skip to content

Unable to load .mo files due to incorrect conversion of Locale to a List of language names #1037

Description

@DI84

Description

The method "_locales_to_names()" from the "Translations" class (babel/support.py) does not correctly convert a given Locale to a List of language names.

Instead of calling str() on the parameter variable locales (note the -s), it is called on the module object locale (without -s) if the parameter variable locales is an instance of Locale (see code below) .

def _locales_to_names(
    locales: Iterable[str | Locale] | str | Locale | None,
) -> list[str] | None:
    """Normalize a `locales` argument to a list of locale names.

    :param locales: the list of locales in order of preference (items in
                    this list can be either `Locale` objects or locale
                    strings)
    """
    if locales is None:
        return None
    if isinstance(locales, Locale):
        return [str(locale)]
    if isinstance(locales, str):
        return [locales]
    return [str(locale) for locale in locales]

The 4th lowest line should read return [str(locales)] instead of return [str(locale)] (as in locale the module object).

As a result, the gettext.find() call in the load method of the Translations class does not return a valid .mo file path and therefore the translations cannot be loaded.

Activity

  1. inabout commented on Oct 24, 2023

    @inabout

    @akx: This is an important bug for us in the current version 2.13.0. Would it be possible to release a 2.13.1 as soon as a fix for this is available?

  2. DI84 commented on Oct 24, 2023

    @DI84
    ContributorAuthor

    This PR would fix it: #1038

  3. self-assigned this
    on Oct 24, 2023
  4. akx commented on Oct 24, 2023

    @akx
    Member

    Oops, great catch. Will release a patch version soon...

  5. akx commented on Oct 24, 2023

    @akx
    Member

    Fixed in https://github.com/python-babel/babel/releases/tag/v2.13.1. Sorry for the inconvenience and thanks for spotting and fixing the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions