Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #2406 (closed defect: fixed)

Opened 10 years ago

Last modified 9 years ago

Workaround for i18n bug

Reported by: victorlin Owned by:
Priority: normal Milestone: 2.x
Component: TurboGears Version: 2.0b7
Severity: normal Keywords: i18n
Cc:

Description

I notice that TurboGears2 can't pick correct language automatically by languages given by browser. For example, the acceptable languages setting of my browser is [zh-tw, en-us, en]. I do provide zh_TW in my i18n folder, it works fine if I set the language to zh_TW. But most of browser write the language in zh-tw form, gettext can't recognize that, so it just fallback to en. I wrote a workaround to fix problem:

    # XXX work around for languages in dash form problem
    # gettext can't recognize that form, for example, it can recognize
    # 'zh_tw' rather than zh-tw, so we need to replace all dash to 
    # underscore to fix this problem 
    import tg.i18n
    oldFunc = tg.i18n.set_temporary_lang 
    def newSetTemporaryLang(languages):
        languages = map(lambda lang: lang.replace('-', '_'), languages)
        return oldFunc(languages)
    tg.i18n.set_temporary_lang  = newSetTemporaryLang

Attachments

firefox_lang.PNG Download (30.7 KB) - added by victorlin 10 years ago.
i18n_lang.patch Download (676 bytes) - added by victorlin 10 years ago.
Patch for i18n language name format problem

Change History

comment:1 Changed 10 years ago by jorge.vargas

I see this as a valid bug but the patch is invalid.

Are you suggesting this is a browser bug we need to fix? if so which browsers send this wrong lang?

That said a proper fix should go into pylons or even python's gettext module if that's the case.

comment:2 Changed 10 years ago by jorge.vargas

  • Milestone changed from __unclassified__ to 2.x

Changed 10 years ago by victorlin

comment:3 Changed 10 years ago by victorlin

I don't know should this problem be considered as a bug of browser. I think it depends on how the http standard documents said about the form of language name. As I know, most of browsers use dash to split lang and location, like "en-us". As the picture shows in attached file. But the gettext of python can only recognize language name like "en_us", the underscore form. Is that a feature or bug, I don't know, too. My solution is, replace the "-" to "_" in lang argument of tg.i18n.set_temporary_lang. To make the path work, you have to insert the code in where we insert middlewares, before everything setup.

Changed 10 years ago by victorlin

Patch for i18n language name format problem

comment:4 Changed 9 years ago by chrisz

see also #2512

comment:5 Changed 9 years ago by chrisz

  • Keywords i18n added
  • Status changed from new to closed
  • Resolution set to fixed

Should be fixed in  changeset 5508bd96904c. Please check if it works for you, it not, reopen.

Note: See TracTickets for help on using tickets.