tornado.locale — Internationalization support¶
To load a locale and generate a translated string:
- people = [...]
- "%(list)s is online", "%(list)s are online", len(people))
The first string is chosen if len(people) == 1
, otherwise the secondstring is chosen.
tornado.locale.
get
(*locale_codes)[源代码]
Returns the closest match for the given locale codes.
We iterate over all given locale codes in order. If we have a tightor a loose match for the code (e.g., “en” for “enUS”), we returnthe locale. Otherwise we move to the next code in the list.
By default we returnen_US
if no translations are found for any ofthe specified locales. You can change the default locale withset_default_locale()
.
tornado.locale.
set_default_locale
(_code)¶
Sets the default locale.
The default locale is assumed to be the language used for all stringsin the system. The translations loaded from disk are mappings fromthe default locale to the destination locale. Consequently, you don’tneed to create a translation file for the default locale.
tornado.locale.
loadtranslations
(_directory, encoding=None)¶
Loads translations from CSV files in a directory.
Translations are strings with optional Python-style named placeholders(e.g.,My name is %(name)s
) and their associated translations.
The directory should have translation files of the formLOCALE.csv
,e.g.esGT.csv
. The CSV files should have two or three columns: string,translation, and an optional plural indicator. Plural indicators shouldbe one of “plural” or “singular”. A given string can have both singularand plural forms. For example may have adifferent verb conjugation depending on whether %(name)s is onename or a list of names. There should be two rows in the CSV file forthat string, one with plural indicator “singular”, and one “plural”.For strings with no verbs that would change on translation, simplyuse “unknown” or the empty string (or don’t include the column at all).
The file is read using the module in the default “excel” dialect.In this format there should not be spaces after the commas.
If noencoding
parameter is given, the encoding will bedetected automatically (among UTF-8 and UTF-16) if the filecontains a byte-order marker (BOM), defaulting to UTF-8 if no BOMis present.
Example translationes_LA.csv
:
在 4.3 版更改: Addedencoding
parameter. Added support for BOM-based encodingdetection, UTF-16, and UTF-8-with-BOM.
tornado.locale.
load_gettext_translations
(_directory, domain)[源代码]
Loads translations fromgettext
‘s locale tree
Locale tree is similar to system’s/usr/share/locale
, like:- {directory}/{lang}/LCMESSAGES/{domain}.mo
Three steps are required to have you app translated:
-
Generate POT translation file:
xgettext —language=Python —keyword=:1,2 -d mydomain file1.py file2.html etc
-
Merge against existing POT file:
msgmerge old.po mydomain.po > new.po
-
Compile:
msgfmt mydomain.po -o {directory}/ptBR/LC_MESSAGES/mydomain.mo- {directory}/{lang}/LCMESSAGES/{domain}.mo
tornado.locale.
get_supported_locales
()¶
Returns a list of all the supported locale codes.
- _class
tornado.locale.
Locale
(code, translations)¶
Object representing a locale.
After calling one of orload_gettext_translations
,call orget_closest
to get a Locale object.- classmethod
getclosest
(*localecodes)¶
Returns the closest match for the given locale code.
- classmethod
get
(code)¶
Returns the Locale for the given locale code.
If it is not supported, we raise an exception.
- (message, plural_message=None, count=None)¶
Returns the translation for the given message for this locale.
Ifpluralmessage
is given, you must also providecount
. We returnplural_message
whencount != 1
,and we return the singular form for the given message whencount == 1
.
format_date
(_date, gmt_offset=0, relative=True, shorter=False, full_format=False)¶
Formats the given date (which should be GMT).
By default, we return a relative time (e.g., “2 minutes ago”). Youcan return an absolute date string withrelative=False
.
You can force a full format date (“July 10, 1980”) withfullformat=True
.
This method is primarily intended for dates in the past.For dates in the future, we fall back to full format.
format_day
(_date, gmt_offset=0, dow=True)¶
Formats the given date as a day of week.
Example: “Monday, January 22”. You can remove the day of week withdow=False
.
list
(parts)¶
Returns a comma-separated list for the given list of parts.
The format is, e.g., “A, B and C”, “A and B” or just “A” for listsof size 1.
friendlynumber
(_value)¶
Returns a comma-separated number for the given integer.
- classmethod
- class
tornado.locale.
CSVLocale
(code, translations)¶
Locale implementation using tornado’s CSV translation format.
- class
tornado.locale.
GettextLocale
(code, translations)¶
Locale implementation using the module.pgettext
(context, message, plural_message=None, count=None)[源代码]
Allows to set context for translation, accepts plural forms.
Usage example:
Plural message example:
To generate POT file with context, add following options to step 1of sequence:
4.2 新版功能.
原文: