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

choose wrong translation #263

Closed
jiuka opened this Issue Sep 6, 2011 · 3 comments

Comments

Projects
None yet
2 participants
@jiuka

jiuka commented Sep 6, 2011

For locale de_CH the function make_localizer in pyramid.i18n try to add de_CH and de. However the order in which thous two locales are added depends on the output of os.path.listdir which is in arbitrary order.

@jiuka

This comment has been minimized.

Show comment
Hide comment
@jiuka

jiuka Sep 6, 2011

My local fixed version.

def make_localizer(current_locale_name, translation_directories):
    """ Create a :class:`pyramid.i18n.Localizer` object
    corresponding to the provided locale name from the 
    translations found in the list of translation directories."""
    translations = Translations()
    translations._catalog = {}

    locales_to_try = [current_locale_name]
    if '_' in current_locale_name:
        locales_to_try.insert(0, current_locale_name.split('_')[0])

    for tdir in translation_directories:
        locale_dirs = [ (lname, os.path.join(tdir, lname)) for lname in
                        locales_to_try if os.path.isdir(os.path.join(tdir, lname)) ]
        for locale_name, locale_dir in locale_dirs:
            messages_dir = os.path.join(locale_dir, 'LC_MESSAGES')
            if not os.path.isdir(os.path.realpath(messages_dir)):
                continue
            for mofile in os.listdir(messages_dir):
                mopath = os.path.realpath(os.path.join(messages_dir,
                                                       mofile))
                if mofile.endswith('.mo') and os.path.isfile(mopath):
                    mofp = open(mopath, 'rb')
                    domain = mofile[:-3]
                    dtrans = Translations(mofp, domain)
                    translations.add(dtrans)

    return Localizer(locale_name=current_locale_name,
                          translations=translations)

jiuka commented Sep 6, 2011

My local fixed version.

def make_localizer(current_locale_name, translation_directories):
    """ Create a :class:`pyramid.i18n.Localizer` object
    corresponding to the provided locale name from the 
    translations found in the list of translation directories."""
    translations = Translations()
    translations._catalog = {}

    locales_to_try = [current_locale_name]
    if '_' in current_locale_name:
        locales_to_try.insert(0, current_locale_name.split('_')[0])

    for tdir in translation_directories:
        locale_dirs = [ (lname, os.path.join(tdir, lname)) for lname in
                        locales_to_try if os.path.isdir(os.path.join(tdir, lname)) ]
        for locale_name, locale_dir in locale_dirs:
            messages_dir = os.path.join(locale_dir, 'LC_MESSAGES')
            if not os.path.isdir(os.path.realpath(messages_dir)):
                continue
            for mofile in os.listdir(messages_dir):
                mopath = os.path.realpath(os.path.join(messages_dir,
                                                       mofile))
                if mofile.endswith('.mo') and os.path.isfile(mopath):
                    mofp = open(mopath, 'rb')
                    domain = mofile[:-3]
                    dtrans = Translations(mofp, domain)
                    translations.add(dtrans)

    return Localizer(locale_name=current_locale_name,
                          translations=translations)
@mcdonc

This comment has been minimized.

Show comment
Hide comment
@mcdonc

mcdonc Sep 7, 2011

Member

@wichert can you weigh in here?

Member

mcdonc commented Sep 7, 2011

@wichert can you weigh in here?

@mcdonc mcdonc closed this in 58ca44e Sep 7, 2011

@mcdonc

This comment has been minimized.

Show comment
Hide comment
@mcdonc

mcdonc Sep 7, 2011

Member

Thank you. I added slightly different code to solve this but your analysis is correct.

Member

mcdonc commented Sep 7, 2011

Thank you. I added slightly different code to solve this but your analysis is correct.

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