返回 Plone 4 中语言的国际化名称列表

发布于 2024-11-19 03:39:42 字数 547 浏览 1 评论 0原文

通过查询 Portal_languages 工具,我可以获得语言名称列表:

>>> from Products.CMFPlone.utils import getToolByName
>>> ltool = getToolByName(context, 'portal_languages')
>>> language_names = [name for code, name in ltool.listAvailableLanguages()]
[u'Abkhazian', u'Afar', u'Afrikaans', u'Albanian', u'Amharic', (...)

但是如何返回本地化语言名称列表?

[编辑] 我想要的是当前用户语言中的语言名称列表,如 @@language-controlpanel 所示 请参阅: https://i.sstatic.net/aEapn.png

By querying the portal_languages tool I can get a list of language names:

>>> from Products.CMFPlone.utils import getToolByName
>>> ltool = getToolByName(context, 'portal_languages')
>>> language_names = [name for code, name in ltool.listAvailableLanguages()]
[u'Abkhazian', u'Afar', u'Afrikaans', u'Albanian', u'Amharic', (...)

But how can I return a list of localized language names?

[EDIT] What I want is the list of language names in the language of the current user, as shown in @@language-controlpanel See: https://i.sstatic.net/aEapn.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

别闹i 2024-11-26 03:39:42

请改用 listAvailableLanguageInformation() 方法:

>>> from Products.CMFPlone.utils import getToolByName
>>> ltool = getToolByName(context, 'portal_languages')
>>> native_language_names = [entry[u'native'] 
...      for entry in ltool.listAvailableLanguageInformation()]
[u'Afrikaans', u'Aymara', u'Az\u0259ri T\xfcrk\xe7\u0259si', u'Bahasa Indonesia', ...]

请注意,@@language-controlpanel 视图使用 zope.i18n.locales 模块来提供翻译的语言;但该列表非常不完整,以至于大多数 UI 语言的语言列表都没有翻译。显然意大利语是其中一种翻译的语言。

您可以通过请求或通过 @@plone_state 视图访问语言环境结构。 locales.displayNames.languages 字典将语言代码(2 个字母)映射到本地语言名称:

>>> from Products.CMFPlone.utils import getToolByName
>>> ltool = getToolByName(context, 'portal_languages')
>>> languages = request.locales.displayNames.languages
>>> language_names = [languages.get(code, name) for code, name in ltool.listAvailableLanguages()]
[u'abkhazian', u'afar', u'afrikaans', u'albanese', u'amarico', ...]

如您所见,语言名称是小写的,而不是正确大写的。此外,解析数据的成本很高(该包包含首次访问时解析的 XML 文件),因此在首次访问时可能需要一些时间才能使用该数据。

正如 Hanno 所说,你最好的选择是使用 Babel,因为它实际上拥有更多可用的最新信息,而不仅仅是针对少数语言。

Use the listAvailableLanguageInformation() method instead:

>>> from Products.CMFPlone.utils import getToolByName
>>> ltool = getToolByName(context, 'portal_languages')
>>> native_language_names = [entry[u'native'] 
...      for entry in ltool.listAvailableLanguageInformation()]
[u'Afrikaans', u'Aymara', u'Az\u0259ri T\xfcrk\xe7\u0259si', u'Bahasa Indonesia', ...]

Note that the @@language-controlpanel view uses the zope.i18n.locales module to provide translated languages; but that list is so incomplete that the languages list is not translated for most of UI languages. Apparently italian is one language where this is translated.

You can reach the locales structure via the request, or via the @@plone_state view. The locales.displayNames.languages dictionary maps language code (2 letters) to local language name:

>>> from Products.CMFPlone.utils import getToolByName
>>> ltool = getToolByName(context, 'portal_languages')
>>> languages = request.locales.displayNames.languages
>>> language_names = [languages.get(code, name) for code, name in ltool.listAvailableLanguages()]
[u'abkhazian', u'afar', u'afrikaans', u'albanese', u'amarico', ...]

As you can see, the language names are lowercased, not properly capitalized. Also, the data is expensive to parse (the package contains XML files parsed on first access) so it can take several moments before this data is available to you on first access.

Your best bet would be to use Babel, as Hanno states, as it actually has far more current information available, and not just for a handful of languages.

海未深 2024-11-26 03:39:42

如果您想要多种不同语言的翻译语言名称,请安装 Babel (http://pypi.python.org/pypi/Babel)。关于它有很好的文档,例如 http://packages.python.org/Babel/display.html

>>> from babel import Locale
>>> locale = Locale('de', 'DE').languages['ja']
u'Japanisch'

Plone 仅包含母语和英语名称。 zope.i18n 包有一些这样的数据,但它确实不完整并且过时,所以 Babel 是你最好的选择。

If you want translated language names in many different languages, install Babel (http://pypi.python.org/pypi/Babel). There's good documentation on it, for example http://packages.python.org/Babel/display.html:

>>> from babel import Locale
>>> locale = Locale('de', 'DE').languages['ja']
u'Japanisch'

Plone only includes native and English language names. The zope.i18n package has some of this data, but it's really incomplete and outdated, so Babel is your best bet.

落花随流水 2024-11-26 03:39:42

感谢 Martijn 的帮助,我能够解决这个问题。这是将生成翻译语言名称词典的最终工作代码。如果您想要进行本地化选择字段(例如语言控制面板中的选择字段),则非常有用。

from Products.CMFCore.interfaces import ISiteRoot
from zope.component import getMultiAdapter
from zope.site.hooks import getSite
from zope.globalrequest import getRequest

@grok.provider(IContextSourceBinder)
def languages(context):
    """
    Return a vocabulary of language codes and
    translated language names.
    """

    # z3c.form KSS inline validation hack
    if not ISiteRoot.providedBy(context):
        for item in getSite().aq_chain:
            if ISiteRoot.providedBy(item):
                context = item

    # retrieve the localized language names.
    request = getRequest()
    portal_state = getMultiAdapter((context, request), name=u'plone_portal_state')
    lang_items = portal_state.locale().displayNames.languages.items()

    # build the dictionary
    return SimpleVocabulary(
        [SimpleTerm(value=lcode, token=lcode, title=lname)\
          for lcode, lname in lang_items]
    )

Thanks to Martijn's help I was able to solve the issue. This is the final working code that will generate the dictionary of translated language names. Very useful if you want to make a localized selection field such as the one found in the language control-panel.

from Products.CMFCore.interfaces import ISiteRoot
from zope.component import getMultiAdapter
from zope.site.hooks import getSite
from zope.globalrequest import getRequest

@grok.provider(IContextSourceBinder)
def languages(context):
    """
    Return a vocabulary of language codes and
    translated language names.
    """

    # z3c.form KSS inline validation hack
    if not ISiteRoot.providedBy(context):
        for item in getSite().aq_chain:
            if ISiteRoot.providedBy(item):
                context = item

    # retrieve the localized language names.
    request = getRequest()
    portal_state = getMultiAdapter((context, request), name=u'plone_portal_state')
    lang_items = portal_state.locale().displayNames.languages.items()

    # build the dictionary
    return SimpleVocabulary(
        [SimpleTerm(value=lcode, token=lcode, title=lname)\
          for lcode, lname in lang_items]
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文