### Navigation
- [index](# "General Index")
- [modules](# "Python Module Index") |
- [next](# "tornado.websocket — Bidirectional communication to the browser") |
- [previous](# "tornado.escape — Escaping and string manipulation") |
- [Tornado 4.4.dev1 documentation](#) »
- [Web framework](#) »
# `tornado.locale` — Internationalization support
Translation methods for generating localized strings.
To load a locale and generate a translated string:
~~~
user_locale = tornado.locale.get("es_LA")
print user_locale.translate("Sign out")
~~~
[`tornado.locale.get()`](# "tornado.locale.get") returns the closest matching locale, not necessarily thespecific locale you requested. You can support pluralization withadditional arguments to [`translate()`](# "tornado.locale.Locale.translate"), e.g.:
~~~
people = [...]
message = user_locale.translate(
"%(list)s is online", "%(list)s are online", len(people))
print message % {"list": user_locale.list(people)}
~~~
The first string is chosen if `len(people) == 1`, otherwise the secondstring is chosen.
Applications should call one of [`load_translations`](# "tornado.locale.load_translations") (which uses a simpleCSV format) or [`load_gettext_translations`](# "tornado.locale.load_gettext_translations") (which uses the `.mo` formatsupported by [`gettext`](https://docs.python.org/3.4/library/gettext.html#module-gettext "(in Python v3.4)") [https://docs.python.org/3.4/library/gettext.html#module-gettext] and related tools). If neither method is called,the [`Locale.translate`](# "tornado.locale.Locale.translate") method will simply return the original string.
`tornado.locale.``get`(**locale_codes*)[[source]](#)
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 “en_US”), we returnthe locale. Otherwise we move to the next code in the list.
By default we return `en_US` if no translations are found for any ofthe specified locales. You can change the default locale with[`set_default_locale()`](# "tornado.locale.set_default_locale").
`tornado.locale.``set_default_locale`(*code*)[[source]](#)
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.``load_translations`(*directory*, *encoding=None*)[[source]](#)
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 form `LOCALE.csv`,e.g. `es_GT.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 `%(name)s liked this` 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 [`csv`](https://docs.python.org/3.4/library/csv.html#module-csv "(in Python v3.4)") [https://docs.python.org/3.4/library/csv.html#module-csv] module in the default “excel” dialect.In this format there should not be spaces after the commas.
If no `encoding` 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 translation `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"
~~~
Changed in version 4.3: Added `encoding` parameter. Added support for BOM-based encodingdetection, UTF-16, and UTF-8-with-BOM.
`tornado.locale.``load_gettext_translations`(*directory*, *domain*)[[source]](#)
Loads translations from [`gettext`](https://docs.python.org/3.4/library/gettext.html#module-gettext "(in Python v3.4)") [https://docs.python.org/3.4/library/gettext.html#module-gettext]‘s locale tree
Locale tree is similar to system's `/usr/share/locale`, like:
~~~
{directory}/{lang}/LC_MESSAGES/{domain}.mo
~~~
Three steps are required to have you app translated:
1.
Generate POT translation file:
~~~
xgettext --language=Python --keyword=_:1,2 -d mydomain file1.py file2.html etc
~~~
1.
Merge against existing POT file:
~~~
msgmerge old.po mydomain.po > new.po
~~~
1.
Compile:
~~~
msgfmt mydomain.po -o {directory}/pt_BR/LC_MESSAGES/mydomain.mo
~~~
`tornado.locale.``get_supported_locales`()[[source]](#)
Returns a list of all the supported locale codes.
*class *`tornado.locale.``Locale`(*code*, *translations*)[[source]](#)
Object representing a locale.
After calling one of [`load_translations`](# "tornado.locale.load_translations") or [`load_gettext_translations`](# "tornado.locale.load_gettext_translations"),call [`get`](# "tornado.locale.get") or [`get_closest`](# "tornado.locale.Locale.get_closest") to get a Locale object.
*classmethod *`get_closest`(**locale_codes*)[[source]](#)
Returns the closest match for the given locale code.
*classmethod *`get`(*code*)[[source]](#)
Returns the Locale for the given locale code.
If it is not supported, we raise an exception.
`translate`(*message*, *plural_message=None*, *count=None*)[[source]](#)
Returns the translation for the given message for this locale.
If `plural_message` is given, you must also provide`count`. We return `plural_message` when `count != 1`,and we return the singular form for the given message when`count == 1`.
`format_date`(*date*, *gmt_offset=0*, *relative=True*, *shorter=False*, *full_format=False*)[[source]](#)
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 with `relative=False`.
You can force a full format date (“July 10, 1980”) with`full_format=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*)[[source]](#)
Formats the given date as a day of week.
Example: “Monday, January 22”. You can remove the day of week with`dow=False`.
`list`(*parts*)[[source]](#)
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.
`friendly_number`(*value*)[[source]](#)
Returns a comma-separated number for the given integer.
*class *`tornado.locale.``CSVLocale`(*code*, *translations*)[[source]](#)
Locale implementation using tornado's CSV translation format.
*class *`tornado.locale.``GettextLocale`(*code*, *translations*)[[source]](#)
Locale implementation using the [`gettext`](https://docs.python.org/3.4/library/gettext.html#module-gettext "(in Python v3.4)") [https://docs.python.org/3.4/library/gettext.html#module-gettext] module.
`pgettext`(*context*, *message*, *plural_message=None*, *count=None*)[[source]](#)
Allows to set context for translation, accepts plural forms.
Usage example:
~~~
pgettext("law", "right")
pgettext("good", "right")
~~~
Plural message example:
~~~
pgettext("organization", "club", "clubs", len(clubs))
pgettext("stick", "club", "clubs", len(clubs))
~~~
To generate POT file with context, add following options to step 1of [`load_gettext_translations`](# "tornado.locale.load_gettext_translations") sequence:
~~~
xgettext [basic options] --keyword=pgettext:1c,2 --keyword=pgettext:1c,2,3
~~~
New in version 4.2.
© Copyright 2009-2016, The Tornado Authors. Created using [Sphinx](http://sphinx-doc.org/) 1.3.5.
- User's guide
- Introduction
- Asynchronous and non-Blocking I/O
- Coroutines
- Queue example - a concurrent web spider
- Structure of a Tornado web application
- Templates and UI
- Authentication and security
- Running and deploying
- Web framework
- tornado.web — RequestHandler and Application classes
- tornado.template — Flexible output generation
- tornado.escape — Escaping and string manipulation
- tornado.locale — Internationalization support
- tornado.websocket — Bidirectional communication to the browser
- HTTP servers and clients
- tornado.httpserver — Non-blocking HTTP server
- tornado.httpclient — Asynchronous HTTP client
- tornado.httputil — Manipulate HTTP headers and URLs
- tornado.http1connection – HTTP/1.x client/server implementation
- Asynchronous networking
- tornado.ioloop — Main event loop
- tornado.iostream — Convenient wrappers for non-blocking sockets
- tornado.netutil — Miscellaneous network utilities
- tornado.tcpclient — IOStream connection factory
- tornado.tcpserver — Basic IOStream-based TCP server
- Coroutines and concurrency
- tornado.gen — Simplify asynchronous code
- tornado.concurrent — Work with threads and futures
- tornado.locks – Synchronization primitives
- tornado.queues – Queues for coroutines
- tornado.process — Utilities for multiple processes
- Integration with other services
- tornado.auth — Third-party login with OpenID and OAuth
- tornado.wsgi — Interoperability with other Python frameworks and servers
- tornado.platform.asyncio — Bridge between asyncio and Tornado
- tornado.platform.caresresolver — Asynchronous DNS Resolver using C-Ares
- tornado.platform.twisted — Bridges between Twisted and Tornado
- Utilities
- tornado.autoreload — Automatically detect code changes in development
- tornado.log — Logging support
- tornado.options — Command-line parsing
- tornado.stack_context — Exception handling across asynchronous callbacks
- tornado.testing — Unit testing support for asynchronous code
- tornado.util — General-purpose utilities
- Frequently Asked Questions
- Release notes
- What's new in Tornado 4.3
- What's new in Tornado 4.2.1
- What's new in Tornado 4.2
- What's new in Tornado 4.1
- What's new in Tornado 4.0.2
- What's new in Tornado 4.0.1
- What's new in Tornado 4.0
- What's new in Tornado 3.2.2
- What's new in Tornado 3.2.1
- What's new in Tornado 3.2
- What's new in Tornado 3.1.1
- What's new in Tornado 3.1
- What's new in Tornado 3.0.2
- What's new in Tornado 3.0.1
- What's new in Tornado 3.0
- What's new in Tornado 2.4.1
- What's new in Tornado 2.4
- What's new in Tornado 2.3
- What's new in Tornado 2.2.1
- What's new in Tornado 2.2
- What's new in Tornado 2.1.1
- What's new in Tornado 2.1
- What's new in Tornado 2.0
- What's new in Tornado 1.2.1
- What's new in Tornado 1.2
- What's new in Tornado 1.1.1
- What's new in Tornado 1.1
- What's new in Tornado 1.0.1
- What's new in Tornado 1.0