2005-11-04 12:59:46 +08:00
|
|
|
"translation helper functions"
|
|
|
|
|
2005-11-07 05:55:39 +08:00
|
|
|
import os, re, sys
|
2005-11-04 12:59:46 +08:00
|
|
|
import gettext as gettext_module
|
|
|
|
from cStringIO import StringIO
|
|
|
|
from django.utils.functional import lazy
|
|
|
|
|
|
|
|
try:
|
|
|
|
import threading
|
|
|
|
hasThreads = True
|
|
|
|
except ImportError:
|
|
|
|
hasThreads = False
|
|
|
|
|
|
|
|
if hasThreads:
|
|
|
|
currentThread = threading.currentThread
|
|
|
|
else:
|
|
|
|
def currentThread():
|
|
|
|
return 'no threading'
|
|
|
|
|
2005-11-07 05:55:39 +08:00
|
|
|
# Translations are cached in a dictionary for every language+app tuple.
|
|
|
|
# The active translations are stored by threadid to make them thread local.
|
2005-11-04 12:59:46 +08:00
|
|
|
_translations = {}
|
|
|
|
_active = {}
|
|
|
|
|
2005-11-07 05:55:39 +08:00
|
|
|
# The default translation is based on the settings file.
|
2005-11-04 12:59:46 +08:00
|
|
|
_default = None
|
|
|
|
|
2005-11-07 05:55:39 +08:00
|
|
|
# This is a cache for accept-header to translation object mappings to prevent
|
|
|
|
# the accept parser to run multiple times for one user.
|
2005-11-04 12:59:46 +08:00
|
|
|
_accepted = {}
|
|
|
|
|
|
|
|
def to_locale(language):
|
2005-11-07 05:55:39 +08:00
|
|
|
"Turns a language name (en-us) into a locale name (en_US)."
|
2005-11-04 12:59:46 +08:00
|
|
|
p = language.find('-')
|
|
|
|
if p >= 0:
|
|
|
|
return language[:p].lower()+'_'+language[p+1:].upper()
|
|
|
|
else:
|
|
|
|
return language.lower()
|
|
|
|
|
|
|
|
def to_language(locale):
|
2005-11-07 05:55:39 +08:00
|
|
|
"Turns a locale name (en_US) into a language name (en-us)."
|
2005-11-04 12:59:46 +08:00
|
|
|
p = locale.find('_')
|
|
|
|
if p >= 0:
|
|
|
|
return locale[:p].lower()+'-'+locale[p+1:].lower()
|
|
|
|
else:
|
|
|
|
return locale.lower()
|
|
|
|
|
|
|
|
class DjangoTranslation(gettext_module.GNUTranslations):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
This class sets up the GNUTranslations context with regard to output
|
|
|
|
charset. Django uses a defined DEFAULT_CHARSET as the output charset on
|
|
|
|
Python 2.4. With Python 2.3, use DjangoTranslation23.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
from django.conf import settings
|
|
|
|
gettext_module.GNUTranslations.__init__(self, *args, **kw)
|
2005-11-07 05:55:39 +08:00
|
|
|
# Starting with Python 2.4, there's a function to define
|
2005-11-04 12:59:46 +08:00
|
|
|
# the output charset. Before 2.4, the output charset is
|
|
|
|
# identical with the translation file charset.
|
|
|
|
try:
|
|
|
|
self.set_output_charset(settings.DEFAULT_CHARSET)
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
self.django_output_charset = settings.DEFAULT_CHARSET
|
|
|
|
self.__language = '??'
|
|
|
|
|
|
|
|
def merge(self, other):
|
|
|
|
self._catalog.update(other._catalog)
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def set_language(self, language):
|
|
|
|
self.__language = language
|
|
|
|
|
|
|
|
def language(self):
|
|
|
|
return self.__language
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def __repr__(self):
|
|
|
|
return "<DjangoTranslation lang:%s>" % self.__language
|
|
|
|
|
|
|
|
class DjangoTranslation23(DjangoTranslation):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Compatibility class that is only used with Python 2.3.
|
|
|
|
Python 2.3 doesn't support set_output_charset on translation objects and
|
|
|
|
needs this wrapper class to make sure input charsets from translation files
|
|
|
|
are correctly translated to output charsets.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
With a full switch to Python 2.4, this can be removed from the source.
|
|
|
|
"""
|
|
|
|
def gettext(self, msgid):
|
|
|
|
res = self.ugettext(msgid)
|
|
|
|
return res.encode(self.django_output_charset)
|
|
|
|
|
|
|
|
def ngettext(self, msgid1, msgid2, n):
|
|
|
|
res = self.ungettext(msgid1, msgid2, n)
|
|
|
|
return res.encode(self.django_output_charset)
|
|
|
|
|
|
|
|
def translation(language):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Returns a translation object.
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
This translation object will be constructed out of multiple GNUTranslations
|
2005-11-07 05:55:39 +08:00
|
|
|
objects by merging their catalogs. It will construct a object for the
|
|
|
|
requested language and add a fallback to the default language, if it's
|
|
|
|
different from the requested language.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
global _translations
|
|
|
|
|
|
|
|
t = _translations.get(language, None)
|
|
|
|
if t is not None:
|
|
|
|
return t
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
# set up the right translation class
|
|
|
|
klass = DjangoTranslation
|
|
|
|
if sys.version_info < (2, 4):
|
|
|
|
klass = DjangoTranslation23
|
|
|
|
|
|
|
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
|
|
|
|
|
|
|
parts = os.environ['DJANGO_SETTINGS_MODULE'].split('.')
|
|
|
|
project = __import__(parts[0], {}, {}, [])
|
|
|
|
projectpath = os.path.join(os.path.dirname(project.__file__), 'locale')
|
|
|
|
|
|
|
|
def _fetch(lang, fallback=None):
|
|
|
|
|
|
|
|
global _translations
|
|
|
|
|
|
|
|
loc = to_locale(lang)
|
|
|
|
|
|
|
|
res = _translations.get(lang, None)
|
|
|
|
if res is not None:
|
|
|
|
return res
|
|
|
|
|
|
|
|
def _translation(path):
|
|
|
|
try:
|
|
|
|
t = gettext_module.translation('django', path, [loc], klass)
|
|
|
|
t.set_language(lang)
|
|
|
|
return t
|
|
|
|
except IOError, e:
|
|
|
|
return None
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
res = _translation(globalpath)
|
|
|
|
|
|
|
|
def _merge(path):
|
|
|
|
t = _translation(path)
|
|
|
|
if t is not None:
|
|
|
|
if res is None:
|
|
|
|
return t
|
|
|
|
else:
|
|
|
|
res.merge(t)
|
|
|
|
return res
|
|
|
|
|
|
|
|
if hasattr(settings, 'LOCALE_PATHS'):
|
|
|
|
for localepath in settings.LOCALE_PATHS:
|
|
|
|
if os.path.isdir(localepath):
|
|
|
|
res = _merge(localepath)
|
|
|
|
|
|
|
|
if os.path.isdir(projectpath):
|
|
|
|
res = _merge(projectpath)
|
|
|
|
|
|
|
|
for appname in settings.INSTALLED_APPS:
|
|
|
|
p = appname.rfind('.')
|
|
|
|
if p >= 0:
|
|
|
|
app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
|
|
|
|
else:
|
|
|
|
app = __import__(appname, {}, {}, [])
|
|
|
|
|
|
|
|
apppath = os.path.join(os.path.dirname(app.__file__), 'locale')
|
|
|
|
|
|
|
|
if os.path.isdir(apppath):
|
|
|
|
res = _merge(apppath)
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
if res is None:
|
|
|
|
if fallback is not None:
|
|
|
|
res = fallback
|
|
|
|
else:
|
|
|
|
return gettext_module.NullTranslations()
|
|
|
|
_translations[lang] = res
|
|
|
|
return res
|
|
|
|
|
|
|
|
default_translation = _fetch(settings.LANGUAGE_CODE)
|
|
|
|
current_translation = _fetch(language, fallback=default_translation)
|
|
|
|
|
|
|
|
return current_translation
|
|
|
|
|
|
|
|
def activate(language):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Fetches the translation object for a given tuple of application name and
|
|
|
|
language and installs it as the current translation object for the current
|
|
|
|
thread.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
_active[currentThread()] = translation(language)
|
|
|
|
|
|
|
|
def deactivate():
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Deinstalls the currently active translation object so that further _ calls
|
|
|
|
will resolve against the default translation object, again.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
global _active
|
|
|
|
if _active.has_key(currentThread()):
|
|
|
|
del _active[currentThread()]
|
|
|
|
|
|
|
|
def get_language():
|
2005-11-07 05:55:39 +08:00
|
|
|
"Returns the currently selected language."
|
2005-11-04 12:59:46 +08:00
|
|
|
t = _active.get(currentThread(), None)
|
|
|
|
if t is not None:
|
|
|
|
try:
|
|
|
|
return to_language(t.language())
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2005-11-07 05:55:39 +08:00
|
|
|
# If we don't have a real translation object, assume it's the default language.
|
2005-11-04 12:59:46 +08:00
|
|
|
from django.conf.settings import LANGUAGE_CODE
|
|
|
|
return LANGUAGE_CODE
|
|
|
|
|
|
|
|
def gettext(message):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
This function will be patched into the builtins module to provide the _
|
|
|
|
helper function. It will use the current thread as a discriminator to find
|
|
|
|
the translation object to use. If no current translation is activated, the
|
|
|
|
message will be run through the default translation object.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
global _default, _active
|
|
|
|
t = _active.get(currentThread(), None)
|
|
|
|
if t is not None:
|
|
|
|
return t.gettext(message)
|
|
|
|
if _default is None:
|
|
|
|
from django.conf import settings
|
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
|
|
|
return _default.gettext(message)
|
|
|
|
|
|
|
|
def gettext_noop(message):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Marks strings for translation but doesn't translate them now. This can be
|
|
|
|
used to store strings in global variables that should stay in the base
|
|
|
|
language (because they might be used externally) and will be translated later.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
return message
|
|
|
|
|
|
|
|
def ngettext(singular, plural, number):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Returns the translation of either the singular or plural, based on the number.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
global _default, _active
|
|
|
|
|
|
|
|
t = _active.get(currentThread(), None)
|
|
|
|
if t is not None:
|
|
|
|
return t.ngettext(singular, plural, number)
|
|
|
|
if _default is None:
|
|
|
|
from django.conf import settings
|
|
|
|
_default = translation('*', settings.LANGUAGE_CODE)
|
|
|
|
return _default.ngettext(singular, plural, number)
|
|
|
|
|
|
|
|
gettext_lazy = lazy(gettext, str)
|
|
|
|
ngettext_lazy = lazy(ngettext, str)
|
|
|
|
|
|
|
|
def check_for_language(lang_code):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Checks whether there is a global language file for the given language code.
|
2005-11-13 08:19:16 +08:00
|
|
|
This is used to decide whether a user-provided language is available. This is
|
2005-11-13 05:27:46 +08:00
|
|
|
only used for language codes from either the cookies or session.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
from django.conf import settings
|
|
|
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
|
|
|
if gettext_module.find('django', globalpath, [to_locale(lang_code)]) is not None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_language_from_request(request):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Analyzes the request to find what language the user wants the system to show.
|
2005-11-13 05:27:46 +08:00
|
|
|
Only languages listed in settings.LANGUAGES are taken into account. If the user
|
|
|
|
requests a sublanguage where we have a main language, we send out the main language.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
global _accepted
|
|
|
|
from django.conf import settings
|
|
|
|
globalpath = os.path.join(os.path.dirname(settings.__file__), 'locale')
|
2005-11-13 05:27:46 +08:00
|
|
|
supported = dict(settings.LANGUAGES)
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
if hasattr(request, 'session'):
|
|
|
|
lang_code = request.session.get('django_language', None)
|
2005-11-13 05:27:46 +08:00
|
|
|
if lang_code in supported and lang_code is not None and check_for_language(lang_code):
|
2005-11-04 12:59:46 +08:00
|
|
|
return lang_code
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
lang_code = request.COOKIES.get('django_language', None)
|
2005-11-13 05:27:46 +08:00
|
|
|
if lang_code in supported and lang_code is not None and check_for_language(lang_code):
|
2005-11-04 12:59:46 +08:00
|
|
|
return lang_code
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
|
|
|
|
if accept is not None:
|
|
|
|
|
|
|
|
t = _accepted.get(accept, None)
|
|
|
|
if t is not None:
|
|
|
|
return t
|
|
|
|
|
|
|
|
def _parsed(el):
|
|
|
|
p = el.find(';q=')
|
|
|
|
if p >= 0:
|
|
|
|
lang = el[:p].strip()
|
|
|
|
order = int(float(el[p+3:].strip())*100)
|
|
|
|
else:
|
|
|
|
lang = el
|
|
|
|
order = 100
|
2005-11-13 05:27:46 +08:00
|
|
|
p = lang.find('-')
|
|
|
|
if p >= 0:
|
|
|
|
mainlang = lang[:p]
|
|
|
|
else:
|
|
|
|
mainlang = lang
|
|
|
|
return (lang, mainlang, order)
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
langs = [_parsed(el) for el in accept.split(',')]
|
2005-11-13 05:27:46 +08:00
|
|
|
langs.sort(lambda a,b: -1*cmp(a[2], b[2]))
|
|
|
|
|
|
|
|
for lang, mainlang, order in langs:
|
|
|
|
if lang in supported or mainlang in supported:
|
|
|
|
langfile = gettext_module.find('django', globalpath, [to_locale(lang)])
|
|
|
|
if langfile:
|
|
|
|
# reconstruct the actual language from the language
|
|
|
|
# filename, because otherwise we might incorrectly
|
|
|
|
# report de_DE if we only have de available, but
|
|
|
|
# did find de_DE because of language normalization
|
|
|
|
lang = langfile[len(globalpath):].split(os.path.sep)[1]
|
|
|
|
_accepted[accept] = lang
|
|
|
|
return lang
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
return settings.LANGUAGE_CODE
|
|
|
|
|
2005-11-13 06:08:54 +08:00
|
|
|
def get_date_formats():
|
|
|
|
"""
|
2005-11-13 08:19:16 +08:00
|
|
|
This function checks whether translation files provide a translation for some
|
2005-11-13 06:08:54 +08:00
|
|
|
technical message ID to store date and time formats. If it doesn't contain
|
|
|
|
one, the formats provided in the settings will be used.
|
|
|
|
"""
|
|
|
|
from django.conf.settings import DATE_FORMAT, DATETIME_FORMAT, TIME_FORMAT
|
|
|
|
date_format = _('DATE_FORMAT')
|
|
|
|
datetime_format = _('DATETIME_FORMAT')
|
|
|
|
time_format = _('TIME_FORMAT')
|
|
|
|
if date_format == 'DATE_FORMAT':
|
|
|
|
date_format = DATE_FORMAT
|
|
|
|
if datetime_format == 'DATETIME_FORMAT':
|
|
|
|
datetime_format = DATETIME_FORMAT
|
|
|
|
if time_format == 'TIME_FORMAT':
|
|
|
|
time_format = TIME_FORMAT
|
|
|
|
return (date_format, datetime_format, time_format)
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def install():
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Installs the gettext function as the default translation function under
|
|
|
|
the name _.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
__builtins__['_'] = gettext
|
|
|
|
|
|
|
|
dot_re = re.compile(r'\S')
|
|
|
|
def blankout(src, char):
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Changes every non-whitespace character to the given char.
|
|
|
|
Used in the templateize function.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
return dot_re.sub(char, src)
|
|
|
|
|
|
|
|
inline_re = re.compile(r"""^\s*trans\s+((?:".*?")|(?:'.*?'))\s*""")
|
|
|
|
block_re = re.compile(r"""^\s*blocktrans(?:\s+|$)""")
|
|
|
|
endblock_re = re.compile(r"""^\s*endblocktrans$""")
|
|
|
|
plural_re = re.compile(r"""^\s*plural$""")
|
|
|
|
constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""")
|
|
|
|
def templateize(src):
|
|
|
|
from django.core.template import tokenize, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
|
|
|
|
"""
|
2005-11-07 05:55:39 +08:00
|
|
|
Turns a Django template into something that is understood by xgettext. It
|
|
|
|
does so by translating the Django translation tags into standard gettext
|
|
|
|
function invocations.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
out = StringIO()
|
|
|
|
intrans = False
|
|
|
|
inplural = False
|
|
|
|
singular = []
|
|
|
|
plural = []
|
|
|
|
for t in tokenize(src):
|
|
|
|
if intrans:
|
|
|
|
if t.token_type == TOKEN_BLOCK:
|
|
|
|
endbmatch = endblock_re.match(t.contents)
|
|
|
|
pluralmatch = plural_re.match(t.contents)
|
|
|
|
if endbmatch:
|
|
|
|
if inplural:
|
|
|
|
out.write(' ngettext(%r,%r,count) ' % (''.join(singular), ''.join(plural)))
|
|
|
|
for part in singular:
|
|
|
|
out.write(blankout(part, 'S'))
|
|
|
|
for part in plural:
|
|
|
|
out.write(blankout(part, 'P'))
|
|
|
|
else:
|
|
|
|
out.write(' gettext(%r) ' % ''.join(singular))
|
|
|
|
for part in singular:
|
|
|
|
out.write(blankout(part, 'S'))
|
|
|
|
intrans = False
|
|
|
|
inplural = False
|
|
|
|
singular = []
|
|
|
|
plural = []
|
|
|
|
elif pluralmatch:
|
|
|
|
inplural = True
|
|
|
|
else:
|
2005-11-07 05:55:39 +08:00
|
|
|
raise SyntaxError, "Translation blocks must not include other block tags: %s" % t.contents
|
2005-11-04 12:59:46 +08:00
|
|
|
elif t.token_type == TOKEN_VAR:
|
|
|
|
if inplural:
|
|
|
|
plural.append('%%(%s)s' % t.contents)
|
|
|
|
else:
|
|
|
|
singular.append('%%(%s)s' % t.contents)
|
|
|
|
elif t.token_type == TOKEN_TEXT:
|
|
|
|
if inplural:
|
|
|
|
plural.append(t.contents)
|
|
|
|
else:
|
|
|
|
singular.append(t.contents)
|
|
|
|
else:
|
|
|
|
if t.token_type == TOKEN_BLOCK:
|
|
|
|
imatch = inline_re.match(t.contents)
|
|
|
|
bmatch = block_re.match(t.contents)
|
|
|
|
cmatches = constant_re.findall(t.contents)
|
|
|
|
if imatch:
|
|
|
|
g = imatch.group(1)
|
|
|
|
if g[0] == '"': g = g.strip('"')
|
|
|
|
elif g[0] == "'": g = g.strip("'")
|
|
|
|
out.write(' gettext(%r) ' % g)
|
|
|
|
elif bmatch:
|
|
|
|
intrans = True
|
|
|
|
inplural = False
|
|
|
|
singular = []
|
|
|
|
plural = []
|
|
|
|
elif cmatches:
|
|
|
|
for cmatch in cmatches:
|
|
|
|
out.write(' _(%s) ' % cmatch)
|
|
|
|
else:
|
|
|
|
out.write(blankout(t.contents, 'B'))
|
|
|
|
elif t.token_type == TOKEN_VAR:
|
|
|
|
parts = t.contents.split('|')
|
|
|
|
cmatch = constant_re.match(parts[0])
|
|
|
|
if cmatch:
|
|
|
|
out.write(' _(%s) ' % cmatch.group(1))
|
|
|
|
for p in parts[1:]:
|
|
|
|
if p.find(':_(') >= 0:
|
|
|
|
out.write(' %s ' % p.split(':',1)[1])
|
|
|
|
else:
|
|
|
|
out.write(blankout(p, 'F'))
|
|
|
|
else:
|
|
|
|
out.write(blankout(t.contents, 'X'))
|
|
|
|
return out.getvalue()
|