English 中文(简体)
Django国际化问题与fr-ca, es-us
原标题:Django internationalization problems with fr-ca, es-us

除了加拿大法语(fr-ca)和美国西班牙语(es-us)之外,

我用的是决哥1.4

sets.py django.middware.middware.locale. localeMiddleware 作为安装的中间软件

我的语言清单是:

ugettext = lambda s: s
LANGUAGES = (
    ( en ,    ugettext( English )),
    ( en-gb , ugettext( English United Kingdom )),
    ( es ,    ugettext( Spanish )),
    ( es-us , ugettext( Spanish United States )),
    ( fr ,    ugettext( French )),
    ( fr-ca , ugettext( French Canada )),
)  

urls.py :

from django.conf.urls.defaults import patterns, include
from django.conf.urls.i18n import i18n_patterns

urlpatterns = patterns( myapp ,
    (r ^example/?$ ,             main.views.example ),
    (r ^$ ,                      main.views.index ),

    (r  ,                       include( myapp.main.urls )),
)

urlpatterns += i18n_patterns( myapp.main.views ,  
    (r ^example/?$ ,             example ),
    (r ^example_1/?$ ,           example1 ),
    (r ^example_2/?$ ,           example2 ),
    (r ^$ ,                      index ),
)

我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑,我跑

# django-admin.py makemessages -l en
# django-admin.py makemessages -l en-GB
# django-admin.py makemessages -l fr
# django-admin.py makemessages -l fr_CA
# django-admin.py makemessages -l es
# django-admin.py makemessages -l es_US
#
# django-admin.py compilemessages

这就是我得到的:

|  Accept-Language Header              | Response Language  | Expected Language  |
|--------------------------------------|--------------------|--------------------|
|  fr-ca;q=0.9, fr;q=0.8               |       fr           |       fr-ca        |
|  fr-ca;q=0.9                         |       fr           |       fr-ca        |
|                                      |                    |                    |
|  es-us;q=0.9, es-mx;q=0.8, es;q=0.7  |       es           |       es-us        |
|  es-us;q=0.9                         |       es           |       es-us        |
|                                      |                    |                    |
|  en-gb;q=0.8, en-us;q=0.7, en;q=0.6  |       en-gb        |       en-gb        |
|  en-us;q=0.9                         |       en           |       en           |
|                                      |                    |                    |
|  fr-ca;q=0.8, en-gb;q=0.7            |       fr           |       fr-ca        |
|  fr-ca;q=0.7, en-gb;q=0.8            |       en-gb        |       en-gb        |
|--------------------------------------|--------------------|--------------------|

如果我在 URL 中输入本地代码, 那么我会得到正确的本地响应 :

http://localhost:8000/fr-ca/       I get fr-ca text back
http://localhost:8000/fr/          I get fr text back
http://localhost:8000/en/          I get en text back
http://localhost:8000/en-gb/       I get en-gb text back
http://localhost:8000/es-us/       I get es-us text back
http://localhost:8000/es/          I get es text back
http://localhost:8000/             I get en text back _(Default in settings.py)_

en-gb 工作与预期相同,在最后一项请求中,将退回,而不是上位的fr-ca。

我是不是漏了点什么, 或者fr -ca/es -us只是不和决哥合作?


(** 我还发送了默认的 django_ language cookie, 一切都如预期的那样有效,所以看来我只是在使用 Accept-Language 信头时才遇到问题。 **)


@ilvar 已经提供了简短和甜蜜的回答。

好,我终于挖到Django 源<他们><(因为它是开放源头,所有)

文件[django/utils/translation/trans_real.py][1] 是我找到、核实和回答的地方。

这里的函数 get_language_from_request, 第350行的片段 :

...
for path in all_locale_paths():
    if os.path.exists(os.path.join(path, dirname,  LC_MESSAGES ,  django.mo )):
        _accepted[normalized] = lang
        return lang  
...  

path 设置为Django安装 (在站地包装中) dirname 目录,这是请求中的地方名称,在我看来, fr-ca 。因此,如果Django安装不包含请求中的地方名称,而它没有 fr-ca ,它将恢复为 fr/code>。

仅在片段上方, 在 URL 中先检查语言, 如果您是使用 i18n 路径, 然后在语言 cookie 中检查。 如果它存在其中之一, 那么语言代码会立即返回, 所以在 Django 安装时该语言是否不存在并不重要 。

对于2/3的场景, 不同的处理方式 使我失望, 但有些意义 因为浏览器可以发送任何东西, 但如果你已经设置了一个曲奇url, 那么在这一点上,它就是你的问题。

Thanks @ilvar for the answer, even though I questioned it ;)
All I did was just copy the fr to fr_CA, within Django s locales, and voila...

最佳回答
问题回答

暂无回答




相关问题
How does gettext handle dynamic content?

In php (or maybe gettext in general), what does gettext do when it sees a variable to dynamic content? I have 2 cases in mind. 1) Let s say I have <?=$user1?> poked John <?=$user2?>. ...

Explain the Need for Mutexes in Locales, Please

Reading the question Why doesn’t C++ STL support atoi(const string& ) like functions?, I encountered a comment which warned that GCC (at least) has a bug that can slow down multi-threaded ...

How does Vistalizer work

How does Vistalizer manage to override the language limit in Windows Vista Home edition. Which api s does it use to allow installation of Multiple language packages.

Localized exceptions (within a Struts2 app)

I am developing a Struts 2 application with support for multiple languages. If one of the domain objects needs to throw an exception, how can it do so in such a way that the error message is no ...

Rails Globalize plugin help

Has anyone gotten the Globalize plugin to work Rails 2.3.2 or later? If so, could you direct me to some useful info?