Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DI84 opened this issue Oct 24, 2023 · 4 comments
Assignees
Labels

Comments

@DI84
Copy link
Contributor

DI84 commented Oct 24, 2023

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.

@inabout
Copy link

inabout commented Oct 24, 2023

@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?

@DI84
Copy link
Contributor Author

DI84 commented Oct 24, 2023

This PR would fix it: #1038

@akx akx self-assigned this Oct 24, 2023
@akx akx added the bug label Oct 24, 2023
@akx
Copy link
Member

akx commented Oct 24, 2023

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

@akx
Copy link
Member

akx commented Oct 24, 2023

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.

@akx akx closed this as completed Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants