【问题标题】:Deactivate language fallback when translating (Python Django i18n)翻译时停用语言回退(Python Django i18n)
【发布时间】:2017-07-19 15:02:47
【问题描述】:

我有一个 i18nized Python Django 应用程序。它目前使用两种语言;德语 (DE) 和法语 (FR)。

我已将所有密钥(.po-/.mo-files)翻译成德语并准备好,但对于法语,有些则丢失了。

在 Django 设置中,我将“de”指定为 LANGUAGE_CODE

我可以毫无问题地从一种语言切换到另一种语言。路由工作正常,我需要的所有其他功能都由 Django 中间件处理。

但是,在当前情况下,当我从德语切换到法语时,法语中缺少的所有键都回退到德语值。但我希望他们只默认使用他们的键。

例如
当前情景
Sortiment(提供法语版本)-> Assortiment
Gratis Lieferung(法语不可用)-> Gratis Lieferung

预期场景
Sortiment(提供法语版本)-> Assortiment
Gratis Lieferung(法语不可用)-> free.shipping.info

解决这个问题的干净解决方案是什么?我在 Django 文档中找不到任何内容。我想在不使用其他插件的情况下解决这个问题。

我可以想出的一种解决方案是,在法语翻译中添加所有缺失的键,并将它们的值也作为它们的键,但这感觉不对。

例如在django.po

msgid "searchsuggest.placeholder"
msgstr "searchsuggest.placeholder"

另一种可能的解决方案是不在settings.py 中设置LANGUAGE_CODE,这可以像我希望的那样用于法语,例如我转到mypage.com/fr/,所有翻译后的键都显示为正确的对应值,而未翻译的键仅显示为键(请参阅“预期场景”)。但是当我这样做时,德语版本只显示键,没有值。例如。我去mypage.com/(德语应该是隐含的),这就是我所看到的:

assortment.menu.title
free.shipping.info

更多信息

我的urls.py

urlpatterns = i18n_patterns(
    # app endpoints
    url(r'^$', home, name='home'),
    url(r'^cart', include('app.cart.urls')),
    url(r'^', include('app.infra.url.urls')),
    prefix_default_language=False,
)

我的settings.py

TIME_ZONE = 'UTC'
LANGUAGE_CODE = 'de'
LANGUAGES = [
    ('de', _('German')),
    ('fr', _('French')),
]
LOCALE_PATHS = [
    ../a/dir
]
USE_I18N = True
USE_L10N = True
USE_TZ = True

# And somewhere I use this
'django.middleware.locale.LocaleMiddleware',

我的神社模板全局翻译功能:

from django.utils.translation import ugettext as _
from jinja2.ext import Extension

class ViewExtension(Extension):
    def __init__(self, environment):
        super(ViewExtension, self).__init__(environment)
        environment.globals['trans'] = trans

# defaults back to german if not found
def trans(translation_key, **kwargs):
    translation = _(translation_key) % kwargs

    if translation == translation_key:
        # this only happens if my LANGUAGE_CODE is not set
        translation_logger.warning(f'Missing translation key "{translation_key}".')

    return translation

【问题讨论】:

    标签: python django internationalization translation


    【解决方案1】:

    如果有人对此有“正确”的解决方案,我仍然很高兴,但我最终用 shell 脚本解决了它:

    #!/bin/bash
    
    # A simple script to make sure that all translation files contain all the msgids.
    # If a msgid previously didn't exist in a file, it will be created with the msgstr set to the same as the msgid.
    
    SCRIPTPATH=`dirname $0`
    MSGIDS=`find $SCRIPTPATH -name "*.po" -type f -print0 | xargs grep -h msgid | sort | uniq | awk '{print $2}'`
    
    find $SCRIPTPATH -name "*.po" -type f | while read FILE; do
        current_msgids=`grep -h msgid $FILE | awk '{print $2}'`
        for msg in $MSGIDS; do
            [[ $current_msgids =~ (^|[[:space:]])"$msg"($|[[:space:]]) ]] || printf "\nmsgid $msg\nmsgstr $msg\n" >> $FILE
        done
    done
    

    我刚刚在我们的 Makefile 中运行 compilemessages 之前包含了这个脚本。

    【讨论】:

    • 您找到了“合适”的解决方案吗?有的话可以分享一下吗。谢谢!
    • @fedosov 不幸的是我从来没有这样做过。然而,这个脚本的解决方案对我们的团队来说效果很好。
    猜你喜欢
    • 1970-01-01
    • 2012-03-25
    • 2014-07-05
    • 1970-01-01
    • 2023-01-19
    • 2021-04-21
    • 2011-03-15
    • 2021-08-15
    • 2016-12-23
    相关资源
    最近更新 更多