【问题标题】:How i18n translation works in Django/GAE - strange behavior for Polish?i18n 翻译如何在 Django/GAE 中工作 - 波兰语的奇怪行为?
【发布时间】:2013-02-14 16:04:20
【问题描述】:

我使用 Django 1.3/Google App Engine 编写程序,发现缺乏支持和奇怪的行为。

Prapration - 波兰语翻译(可能很重要)

首先,ungettext 不直接支持波兰语,但 django 1.3 支持 PO 文件标签 Plural-Forms(许多非英语语言有多个复数形式) - 我对波兰语这样做 - 也许同样会是俄语:

复数形式:nplurals=3;复数=(n==1 ? 0 : n%10>=2 && n%10=20) ? 1 : 2);

我之前说过一些例子 ungettext 没用,因为波兰语的复数形式比英语多,我用它来提取 msgId 而已

# should be 0 kóz
ungettext(u'%s kóz', u'%s kozy', 0) % 0
# should be 1 koza
ungettext(u'%s kóz', u'%s kozy', 1) % 1
# should be 2 kozy
ungettext(u'%s kóz', u'%s kozy', 2) % 2
# should be 5 kóz
ungettext(u'%s kóz', u'%s kozy', 5) % 5

我使用 makemessages.py 并用 trick 翻译它以获得 3 个复数形式,而不是像英语中的 2 个(波兰语需要 2-3 个形式):

msgid "%s kóz"
msgid_plural "%s kozy"
msgstr[0] "%s 1 koza"
msgstr[1] "%s 2 kozy"
msgstr[2] "%s 5 kóz"

Django 的奇怪行为(主要问题)

现在我会做更多但不了解行为的技巧:

我为 DJANGO 正确设置了所有变量,包括(希望如此)和:

LANGUAGE_CODE = 'pl' - 所有翻译作品,但不是波兰语我有两种形式而不是 3。 msgstr "%s kóz" msgstr_plural "%s 好笑" msgstr[0] "%s 1 koza" msgstr[1] "%s 2 舒适" msgstr[2] "%s 5 kóz"

LANGUAGE_CODE = 'en' - 所有翻译作品(pl, en-us, en-gb, de-de, ...)

LANGUAGE_CODE = 'xx' - 所有翻译作品(pl, en-us, en-gb, de-de, ...)

LANGUAGE_CODE = 'en-us' - 所有翻译作品(pl, en-us, en-gb, de-de, ...)但不是 en-us 翻译

为什么我应该将我的语言设置为 INVALID 'xx' 或其他不存在的翻译以使其在 django 中工作这对我来说很奇怪?你能帮我正确地做吗?

【问题讨论】:

    标签: django google-app-engine internationalization globalization django-i18n


    【解决方案1】:

    这是一个小错误:

    # Language code for this installation. All choices can be found here:
    # http://www.i18nguy.com/unicode/language-identifiers.html
    LANGUAGE_CODE = 'pl'
    
    SITE_ID = 1
    
    # If you set this to False, Django will make some optimizations so as not
    # to load the internationalization machinery.
    USE_I18N = True
    
    # Valid languages
    LANGUAGES = (
      (u'pl', _('Polski')),
      (u'en-us', _('angielski - Stany Zjednoczone')),
      (u'en-gb', _('angielski - Wielka Brytania')),
      (u'de-de', _('niemiecki - Niemcy')),
    )
    
    LOCALE_PATHS = (
      os.path.join(__ROOT_PATH, 'conf', 'locale'),
    )
    

    路径是在首次使用后定义的,因此 django 无法构建默认翻译 - 这里的修复非常简单。

    # Language code for this installation. All choices can be found here:
    # http://www.i18nguy.com/unicode/language-identifiers.html
    LANGUAGE_CODE = 'pl'
    
    LOCALE_PATHS = (
      os.path.join(__ROOT_PATH, 'conf', 'locale'),
    )
    
    SITE_ID = 1
    
    # If you set this to False, Django will make some optimizations so as not
    # to load the internationalization machinery.
    USE_I18N = True
    
    # Valid languages
    LANGUAGES = (
      (u'pl', _('Polski')),
      (u'en-us', _('angielski - Stany Zjednoczone')),
      (u'en-gb', _('angielski - Wielka Brytania')),
      (u'de-de', _('niemiecki - Niemcy')),
    )
    

    LANGUAGE_CODE = 'xx' 正在创建永远不会使用的无效翻译容器,并且第一次使用其他语言(如 pl)的有效设置会产生很好的副作用:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-26
      • 2019-04-02
      • 2015-05-14
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多