【问题标题】:Activate Translation in Pyramids在金字塔中激活翻译
【发布时间】:2015-07-23 12:50:26
【问题描述】:

我想在金字塔框架中激活我的翻译。因此,我添加了翻译目录并在https://stackoverflow.com/a/11289565/2648872 中设置了一个本地谈判者,并在http://docs.pylonsproject.org/docs/pyramid/en/latest/narr/i18n.html#default-locale-negotiator 中进行了描述。此外,我的 ini 文件中的默认和可用语言已设置,但金字塔不会接受我的翻译。我错过了激活翻译的内容吗?

问候

托比亚斯

编辑

我的 production.ini 的片段

[app:main]
use = egg:dbas

pyramid.reload_templates = true
pyramid.debug_authorization = false
pyramid.debug_notfound = false
pyramid.debug_routematch = false
pyramid.default_locale_name = de

available_languages = de en

从我的 init.py:

def main(global_config, **settings):
[...]
config = Configurator(settings=settings,root_factory='dbas.database.RootFactory') 
config.add_translation_dirs('locale') 
[...]
config.set_locale_negotiator(my_locale_negotiator)

此外,设置会被记录下来,并且 default_locale_name 和 available_languages 都是可见的。不幸的是,在 my_locale_negotiator 中,它们不可读:(

我的文件夹结构是这样的:

dbas
|- setup.py
|- development.ini
|- [...]
|-dbas
  |- __init__.py
  |- views.py
  |- [...]
  |- locale
    |- dbas.pot
      |- de
        |- LC_MESSAGES
        |- dbas.mo
        |- dbas.po
    |- en
      |- LC_MESSAGES
        |- dbas.mo
        |- dbas.po

【问题讨论】:

  • 至少为 my_locale_negotiator 添加代码。更清楚地说明您正在做什么来切换语言,应该发生什么以及发生什么而不是预期的行为。如果可用,添加异常或日志记录。我在金字塔 1.5 中运行了这个。你可能还记得我。
  • 我的本地谈判代表就像食谱中描述的那样:docs.pylonsproject.org/docs/pyramid/en/latest/narr/…谈判代表。我的期望是一个网页,它被翻译成德语,因为这应该是默认语言。相反,我得到了英文页面的翻译版本 :( 是的,我记得你和你的有用提示 :)

标签: python internationalization translation pyramid


【解决方案1】:

Pyramid 实际上没有找到您的翻译目录。 According to docs you need to pass absolute directory paths or asset specifications。我更喜欢资产规范,因为我也习惯于为视图配置执行此操作。

config.add_translation_dirs('dbas:locale') 

因为you are applying localization-related deployment settings,您的用例不需要自定义区域设置协商器。在金字塔配置中禁用自定义区域设置协商器。 Rely on the pyramid implementation of the default locale negotiator.

你只是need two things to activate translations。只能先应用这两个概念。

出于调试目的,您可以use one these concepts to set locale manually。我真的很喜欢在查询字符串中传递 LOCALE 值。例如,访问

http://my.application?_LOCALE_=de

【讨论】:

  • 为了调试,我目前有这一行:config.add_translation_dirs('locale', 'locale/', 'dbas:locale'),但这并不能解决问题。
  • 前两个既不是绝对路径也不是资产规范。这可能会破坏规范序列上的迭代
  • 这对序列没有影响。起初我只有 'dbas:locale' 并且由于没有翻译,我添加了其他的:\ 不幸的是,当我在 my_locale_negotiator 中询问时,设置 [#available_languages'] 是 'None'。
【解决方案2】:

解决了这个问题,因为我的变色龙模板的宏观结构是恶意的。

【讨论】:

  • 我故意没有要求这个,但对我的先决条件列表中的模板有疑问。有很多陷阱。我建议有一个较小的项目,只测试 i18n/l10n 上的金字塔框架功能。达尼洛·德拉奎拉(Danilo Dellaquila)前段时间就这样做了——从那以后事情发生了一些变化(github.com/ddellaquila/pyramid_i18n_howto)。如果这些概念在较小的(隔离的)环境中工作,您可以在较大的应用程序中实现类似的概念。这使您的调试变得更加容易 - 对 SO 上的人来说也是如此:-)
  • 我会记住这一点。特别是作为横向进入者有很多陷阱:)
猜你喜欢
  • 2018-05-14
  • 1970-01-01
  • 2017-09-08
  • 1970-01-01
  • 2018-06-10
  • 2014-06-05
  • 1970-01-01
  • 2018-02-05
  • 2019-09-14
相关资源
最近更新 更多