tornado.locale
——国际化支撑¶
生成本地化字符串的转换方法。
要加载区域设置并生成转换后的字符串,请执行以下操作:
user_locale = tornado.locale.get("es_LA")
print(user_locale.translate("Sign out"))
tornado.locale.get()
返回最接近的匹配区域设置,不一定是您请求的特定区域设置。您可以支持复数形式,并为 translate()
,例如:
people = [...]
message = user_locale.translate(
"%(list)s is online", "%(list)s are online", len(people))
print(message % {"list": user_locale.list(people)})
如果 len(people) == 1
,否则选择第二个字符串。
应用程序应调用 load_translations
(使用简单的csv格式)或 load_gettext_translations
(使用 .mo
支持的格式 gettext
以及相关工具)。如果两个方法都没有调用,则 Locale.translate
方法只返回原始字符串。
- tornado.locale.get(*locale_codes: str) tornado.locale.Locale [源代码]¶
返回给定区域设置代码的最近匹配项。
我们按顺序迭代所有给定的区域设置代码。如果我们对代码有严格或松散的匹配(例如,“en”代表“en-us”),我们将返回区域设置。否则,我们将转到列表中的下一个代码。
我们默认返回
en_US
如果找不到任何指定区域设置的翻译。可以使用更改默认区域设置set_default_locale()
.
- tornado.locale.set_default_locale(code: str) None [源代码]¶
设置默认区域设置。
默认区域设置假定为系统中所有字符串使用的语言。从磁盘加载的翻译是从默认区域设置到目标区域设置的映射。因此,您不需要为默认区域设置创建翻译文件。
- tornado.locale.load_translations(directory: str, encoding: Optional[str] = None) None [源代码]¶
从目录中的csv文件加载翻译。
翻译是带有可选的python样式名为placeholders的字符串(例如,
My name is %(name)s
)以及相关的翻译。目录应该有表单的翻译文件
LOCALE.csv
,例如es_GT.csv
. csv文件应该有两列或三列:字符串、翻译和可选的复数指示符。复数指标应为“复数”或“单数”之一。给定的字符串可以同时具有单数和复数形式。例如%(name)s liked this
根据%(name)s是一个名称还是一组名称,可能有不同的动词连词。在csv文件中,该字符串应该有两行,一行带有复数指示符“单数”,一行带有复数。对于没有动词在转换时会改变的字符串,只需使用“unknown”或空字符串(或者根本不包括列)。文件是使用
csv
使用默认“Excel”方言的模块。在这种格式中,逗号后面不应该有空格。如果没有
encoding
参数给定,如果文件包含字节顺序标记(bom),则自动检测编码(在utf-8和utf-16之间),如果不存在bom,则默认为utf-8。示例翻译
es_LA.csv
::"I love you","Te amo" "%(name)s liked this","A %(name)s les gustó esto","plural" "%(name)s liked this","A %(name)s le gustó esto","singular"
在 4.3 版更改: 补充
encoding
参数。增加了对基于bom的编码检测、utf-16和utf-8-with-bom的支持。
- tornado.locale.load_gettext_translations(directory: str, domain: str) None [源代码]¶
从加载翻译
gettext
的区域树区域设置树类似于系统的
/usr/share/locale
,比如:{directory}/{lang}/LC_MESSAGES/{domain}.mo
翻译应用程序需要三个步骤:
生成pot转换文件:
xgettext --language=Python --keyword=_:1,2 -d mydomain file1.py file2.html etc
与现有pot文件合并::
msgmerge old.po mydomain.po > new.po
编译:
msgfmt mydomain.po -o {directory}/pt_BR/LC_MESSAGES/mydomain.mo
- class tornado.locale.Locale(code: str)[源代码]¶
表示区域设置的对象。
打了一个电话
load_translations
或load_gettext_translations
,呼叫get
或get_closest
获取区域设置对象。- classmethod get_closest(*locale_codes: str) tornado.locale.Locale [源代码]¶
返回给定区域设置代码的最近匹配项。
- classmethod get(code: str) tornado.locale.Locale [源代码]¶
返回给定区域设置代码的区域设置。
如果不支持,我们将引发异常。
- translate(message: str, plural_message: Optional[str] = None, count: Optional[int] = None) str [源代码]¶
返回此区域设置的给定消息的翻译。
如果
plural_message
如果提供,您还必须提供count
. 我们回来plural_message
什么时候?count != 1
,我们返回给定消息的单数形式,当count == 1
.
- format_date(date: Union[int, float, datetime.datetime], gmt_offset: int = 0, relative: bool = True, shorter: bool = False, full_format: bool = False) str [源代码]¶
设置给定日期的格式(应为GMT)。
默认情况下,我们返回相对时间(例如,“2分钟前”)。您可以返回绝对日期字符串
relative=False
.您可以强制使用完整格式的日期(“1980年7月10日”)。
full_format=True
.此方法主要用于过去的日期。对于未来的日期,我们将回到完整的格式。
- class tornado.locale.CSVLocale(code: str, translations: Dict[str, Dict[str, str]])[源代码]¶
使用Tornado的csv翻译格式实现区域设置。
- class tornado.locale.GettextLocale(code: str, translations: gettext.NullTranslations)[源代码]¶
使用
gettext
模块。- pgettext(context: str, message: str, plural_message: Optional[str] = None, count: Optional[int] = None) str [源代码]¶
允许设置翻译上下文,接受复数形式。
使用实例:
pgettext("law", "right") pgettext("good", "right")
复数信息示例:
pgettext("organization", "club", "clubs", len(clubs)) pgettext("stick", "club", "clubs", len(clubs))
要使用上下文生成pot文件,请将以下选项添加到步骤1
load_gettext_translations
序列:xgettext [basic options] --keyword=pgettext:1c,2 --keyword=pgettext:1c,2,3
4.2 新版功能.