Update to Tornado 3.0

This commit is contained in:
Ruud committed 2013-04-19 14:49:27 +02:00
1 parent 4f6b31d14a
commit 0e90739786
29 files changed
+1932 -1166

No files matched your search

+42 -38
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env python
#
# -*- coding: utf-8 -*-
# Copyright 2009 Facebook
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
@@ -18,25 +18,25 @@
To load a locale and generate a translated string::
user_locale = locale.get("es_LA")
user_locale = tornado.locale.get("es_LA")
print user_locale.translate("Sign out")
locale.get() returns the closest matching locale, not necessarily the
`tornado.locale.get()` returns the closest matching locale, not necessarily the
specific locale you requested. You can support pluralization with
additional arguments to translate(), e.g.::
additional arguments to `~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 second
The first string is chosen if ``len(people) == 1``, otherwise the second
string is chosen.
Applications should call one of load_translations (which uses a simple
CSV format) or load_gettext_translations (which uses the .mo format
supported by gettext and related tools). If neither method is called,
the locale.translate method will simply return the original string.
Applications should call one of `load_translations` (which uses a simple
CSV format) or `load_gettext_translations` (which uses the ``.mo`` format
supported by `gettext` and related tools). If neither method is called,
the `Locale.translate` method will simply return the original string.
"""
from __future__ import absolute_import, division, print_function, with_statement
@@ -63,15 +63,15 @@ def get(*locale_codes):
or a loose match for the code (e.g., "en" for "en_US"), we return
the locale. Otherwise we move to the next code in the list.
By default we return en_US if no translations are found for any of
By default we return ``en_US`` if no translations are found for any of
the specified locales. You can change the default locale with
set_default_locale() below.
`set_default_locale()`.
"""
return Locale.get_closest(*locale_codes)
def set_default_locale(code):
"""Sets the default locale, used in get_closest_locale().
"""Sets the default locale.
The default locale is assumed to be the language used for all strings
in the system. The translations loaded from disk are mappings from
@@ -85,32 +85,32 @@ def set_default_locale(code):
def load_translations(directory):
u("""Loads translations from CSV files in a directory.
"""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.
(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,
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 should
be one of "plural" or "singular". A given string can have both singular
and plural forms. For example "%(name)s liked this" may have a
and plural forms. For example ``%(name)s liked this`` may have a
different verb conjugation depending on whether %(name)s is one
name or a list of names. There should be two rows in the CSV file for
that string, one with plural indicator "singular", and one "plural".
For strings with no verbs that would change on translation, simply
use "unknown" or the empty string (or don't include the column at all).
The file is read using the csv module in the default "excel" dialect.
The file is read using the `csv` module in the default "excel" dialect.
In this format there should not be spaces after the commas.
Example translation es_LA.csv:
Example translation ``es_LA.csv``::
"I love you","Te amo"
"%(name)s liked this","A %(name)s les gust\u00f3 esto","plural"
"%(name)s liked this","A %(name)s le gust\u00f3 esto","singular"
"%(name)s liked this","A %(name)s les gustó esto","plural"
"%(name)s liked this","A %(name)s le gustó esto","singular"
""")
"""
global _translations
global _supported_locales
_translations = {}
@@ -151,22 +151,25 @@ def load_translations(directory):
def load_gettext_translations(directory, domain):
"""Loads translations from gettext's locale tree
"""Loads translations from `gettext`'s locale tree
Locale tree is similar to system's /usr/share/locale, like:
Locale tree is similar to system's ``/usr/share/locale``, like::
{directory}/{lang}/LC_MESSAGES/{domain}.mo
{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 cyclone file1.py file2.html etc
1. Generate POT translation file::
2. Merge against existing POT file:
msgmerge old.po cyclone.po > new.po
xgettext --language=Python --keyword=_:1,2 -d mydomain file1.py file2.html etc
3. Compile:
msgfmt cyclone.po -o {directory}/pt_BR/LC_MESSAGES/cyclone.mo
2. Merge against existing POT file::
msgmerge old.po mydomain.po > new.po
3. Compile::
msgfmt mydomain.po -o {directory}/pt_BR/LC_MESSAGES/mydomain.mo
"""
import gettext
global _translations
@@ -262,9 +265,10 @@ class Locale(object):
def translate(self, message, plural_message=None, count=None):
"""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.
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``.
"""
raise NotImplementedError()
@@ -273,10 +277,10 @@ class Locale(object):
"""Formats the given date (which should be GMT).
By default, we return a relative time (e.g., "2 minutes ago"). You
can return an absolute date string with relative=False.
can return an absolute date string with ``relative=False``.
You can force a full format date ("July 10, 1980") with
full_format=True.
``full_format=True``.
This method is primarily intended for dates in the past.
For dates in the future, we fall back to full format.
@@ -360,7 +364,7 @@ class Locale(object):
"""Formats the given date as a day of week.
Example: "Monday, January 22". You can remove the day of week with
dow=False.
``dow=False``.
"""
local_date = date - datetime.timedelta(minutes=gmt_offset)
_ = self.translate
@@ -421,7 +425,7 @@ class CSVLocale(Locale):
class GettextLocale(Locale):
"""Locale implementation using the gettext module."""
"""Locale implementation using the `gettext` module."""
def __init__(self, code, translations):
try:
# python 2