2008-03-08 11:55:47 +08:00
|
|
|
"""Translation helper functions."""
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2007-10-27 03:52:42 +08:00
|
|
|
import locale
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import sys
|
2009-12-23 01:58:49 +08:00
|
|
|
import warnings
|
2005-11-04 12:59:46 +08:00
|
|
|
import gettext as gettext_module
|
|
|
|
from cStringIO import StringIO
|
2007-10-27 03:52:42 +08:00
|
|
|
|
2009-03-19 00:55:59 +08:00
|
|
|
from django.utils.importlib import import_module
|
2007-11-17 20:11:54 +08:00
|
|
|
from django.utils.safestring import mark_safe, SafeData
|
2008-07-21 15:57:10 +08:00
|
|
|
from django.utils.thread_support import currentThread
|
2005-11-04 12:59:46 +08:00
|
|
|
|
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
|
|
|
|
|
2008-03-08 11:55:47 +08:00
|
|
|
# This is a cache for normalized accept-header languages to prevent multiple
|
2007-10-27 03:52:42 +08:00
|
|
|
# file lookups when checking the same locale on repeated requests.
|
2005-11-04 12:59:46 +08:00
|
|
|
_accepted = {}
|
|
|
|
|
2007-10-27 03:52:42 +08:00
|
|
|
# Format of Accept-Language header values. From RFC 2616, section 14.4 and 3.9.
|
|
|
|
accept_language_re = re.compile(r'''
|
|
|
|
([A-Za-z]{1,8}(?:-[A-Za-z]{1,8})*|\*) # "en", "en-au", "x-y-z", "*"
|
|
|
|
(?:;q=(0(?:\.\d{,3})?|1(?:.0{,3})?))? # Optional "q=1.00", "q=0.8"
|
|
|
|
(?:\s*,\s*|$) # Multiple accepts per header.
|
|
|
|
''', re.VERBOSE)
|
|
|
|
|
|
|
|
def to_locale(language, to_lower=False):
|
2008-02-06 09:04:30 +08:00
|
|
|
"""
|
|
|
|
Turns a language name (en-us) into a locale name (en_US). If 'to_lower' is
|
|
|
|
True, the last component is lower-cased (en_us).
|
|
|
|
"""
|
2005-11-04 12:59:46 +08:00
|
|
|
p = language.find('-')
|
|
|
|
if p >= 0:
|
2007-10-27 03:52:42 +08:00
|
|
|
if to_lower:
|
|
|
|
return language[:p].lower()+'_'+language[p+1:].lower()
|
|
|
|
else:
|
2010-01-02 05:38:34 +08:00
|
|
|
# Get correct locale for sr-latn
|
|
|
|
if len(language[p+1:]) > 2:
|
|
|
|
return language[:p].lower()+'_'+language[p+1].upper()+language[p+2:].lower()
|
2007-10-27 03:52:42 +08:00
|
|
|
return language[:p].lower()+'_'+language[p+1:].upper()
|
2005-11-04 12:59:46 +08:00
|
|
|
else:
|
|
|
|
return language.lower()
|
|
|
|
|
|
|
|
def to_language(locale):
|
2008-03-08 11:55:47 +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
|
2010-02-16 20:16:01 +08:00
|
|
|
Python 2.4.
|
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:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
self.set_output_charset('utf-8')
|
2005-11-04 12:59:46 +08:00
|
|
|
except AttributeError:
|
|
|
|
pass
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
self.django_output_charset = 'utf-8'
|
2005-11-04 12:59:46 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale')
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2006-05-17 05:28:06 +08:00
|
|
|
if settings.SETTINGS_MODULE is not None:
|
|
|
|
parts = settings.SETTINGS_MODULE.split('.')
|
2009-03-19 00:55:59 +08:00
|
|
|
project = import_module(parts[0])
|
2006-05-17 05:28:06 +08:00
|
|
|
projectpath = os.path.join(os.path.dirname(project.__file__), 'locale')
|
|
|
|
else:
|
|
|
|
projectpath = None
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
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:
|
2010-02-16 20:16:01 +08:00
|
|
|
t = gettext_module.translation('django', path, [loc], DjangoTranslation)
|
2005-11-04 12:59:46 +08:00
|
|
|
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)
|
|
|
|
|
2008-07-07 09:57:05 +08:00
|
|
|
# We want to ensure that, for example, "en-gb" and "en-us" don't share
|
|
|
|
# the same translation object (thus, merging en-us with a local update
|
|
|
|
# doesn't affect en-gb), even though they will both use the core "en"
|
|
|
|
# translation. So we have to subvert Python's internal gettext caching.
|
|
|
|
base_lang = lambda x: x.split('-', 1)[0]
|
|
|
|
if base_lang(lang) in [base_lang(trans) for trans in _translations]:
|
|
|
|
res._info = res._info.copy()
|
|
|
|
res._catalog = res._catalog.copy()
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def _merge(path):
|
|
|
|
t = _translation(path)
|
|
|
|
if t is not None:
|
|
|
|
if res is None:
|
|
|
|
return t
|
|
|
|
else:
|
|
|
|
res.merge(t)
|
|
|
|
return res
|
|
|
|
|
2007-11-19 13:59:58 +08:00
|
|
|
for localepath in settings.LOCALE_PATHS:
|
|
|
|
if os.path.isdir(localepath):
|
|
|
|
res = _merge(localepath)
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
for appname in settings.INSTALLED_APPS:
|
2009-03-19 00:55:59 +08:00
|
|
|
app = import_module(appname)
|
2005-11-04 12:59:46 +08:00
|
|
|
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
|
|
|
|
2010-02-16 20:17:17 +08:00
|
|
|
if projectpath and os.path.isdir(projectpath):
|
|
|
|
res = _merge(projectpath)
|
|
|
|
|
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
|
|
|
"""
|
2010-04-29 22:36:09 +08:00
|
|
|
if isinstance(language, basestring) and language == 'no':
|
|
|
|
warnings.warn(
|
|
|
|
"The use of the language code 'no' is deprecated. "
|
|
|
|
"Please use the 'nb' translation instead.",
|
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
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
|
2007-04-26 21:30:48 +08:00
|
|
|
if currentThread() in _active:
|
2005-11-04 12:59:46 +08:00
|
|
|
del _active[currentThread()]
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def deactivate_all():
|
|
|
|
"""
|
|
|
|
Makes the active translation object a NullTranslations() instance. This is
|
|
|
|
useful when we want delayed translations to appear as the original string
|
|
|
|
for some reason.
|
|
|
|
"""
|
|
|
|
_active[currentThread()] = gettext_module.NullTranslations()
|
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
def get_language():
|
2008-03-08 11:55:47 +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.
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.conf import settings
|
|
|
|
return settings.LANGUAGE_CODE
|
2005-11-04 12:59:46 +08:00
|
|
|
|
2006-05-16 15:35:20 +08:00
|
|
|
def get_language_bidi():
|
|
|
|
"""
|
|
|
|
Returns selected language's BiDi layout.
|
2010-05-09 05:38:27 +08:00
|
|
|
|
|
|
|
* False = left-to-right layout
|
|
|
|
* True = right-to-left layout
|
2006-05-16 15:35:20 +08:00
|
|
|
"""
|
|
|
|
from django.conf import settings
|
2008-08-27 04:08:02 +08:00
|
|
|
|
|
|
|
base_lang = get_language().split('-')[0]
|
|
|
|
return base_lang in settings.LANGUAGES_BIDI
|
2006-06-02 12:20:32 +08:00
|
|
|
|
2005-12-04 20:06:16 +08:00
|
|
|
def catalog():
|
|
|
|
"""
|
2008-03-08 11:55:47 +08:00
|
|
|
Returns the current active catalog for further processing.
|
2005-12-04 20:06:16 +08:00
|
|
|
This can be used if you need to modify the catalog or want to access the
|
|
|
|
whole message catalog instead of just translating one string.
|
|
|
|
"""
|
|
|
|
global _default, _active
|
|
|
|
t = _active.get(currentThread(), None)
|
|
|
|
if t is not None:
|
|
|
|
return t
|
|
|
|
if _default is None:
|
|
|
|
from django.conf import settings
|
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
|
|
|
return _default
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def do_translate(message, translation_function):
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
2007-10-22 01:14:25 +08:00
|
|
|
Translates 'message' using the given 'translation_function' name -- which
|
|
|
|
will be either gettext or ugettext. It uses the current thread 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
|
|
|
"""
|
2009-12-23 01:58:49 +08:00
|
|
|
eol_message = message.replace('\r\n', '\n').replace('\r', '\n')
|
2005-11-04 12:59:46 +08:00
|
|
|
global _default, _active
|
|
|
|
t = _active.get(currentThread(), None)
|
|
|
|
if t is not None:
|
2009-12-23 01:58:49 +08:00
|
|
|
result = getattr(t, translation_function)(eol_message)
|
2007-11-17 20:11:54 +08:00
|
|
|
else:
|
|
|
|
if _default is None:
|
|
|
|
from django.conf import settings
|
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
2009-12-23 01:58:49 +08:00
|
|
|
result = getattr(_default, translation_function)(eol_message)
|
2007-11-17 20:11:54 +08:00
|
|
|
if isinstance(message, SafeData):
|
|
|
|
return mark_safe(result)
|
|
|
|
return result
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
|
|
|
def gettext(message):
|
|
|
|
return do_translate(message, 'gettext')
|
|
|
|
|
|
|
|
def ugettext(message):
|
|
|
|
return do_translate(message, 'ugettext')
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
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
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
language (because they might be used externally) and will be translated
|
|
|
|
later.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
return message
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
def do_ntranslate(singular, plural, number, translation_function):
|
2005-11-04 12:59:46 +08:00
|
|
|
global _default, _active
|
|
|
|
|
|
|
|
t = _active.get(currentThread(), None)
|
|
|
|
if t is not None:
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return getattr(t, translation_function)(singular, plural, number)
|
2005-11-04 12:59:46 +08:00
|
|
|
if _default is None:
|
|
|
|
from django.conf import settings
|
2005-11-16 20:13:28 +08:00
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
return getattr(_default, translation_function)(singular, plural, number)
|
|
|
|
|
|
|
|
def ngettext(singular, plural, number):
|
|
|
|
"""
|
|
|
|
Returns a UTF-8 bytestring of the translation of either the singular or
|
|
|
|
plural, based on the number.
|
|
|
|
"""
|
|
|
|
return do_ntranslate(singular, plural, number, 'ngettext')
|
|
|
|
|
|
|
|
def ungettext(singular, plural, number):
|
|
|
|
"""
|
|
|
|
Returns a unicode strings of the translation of either the singular or
|
|
|
|
plural, based on the number.
|
|
|
|
"""
|
|
|
|
return do_ntranslate(singular, plural, number, 'ungettext')
|
2005-11-04 12:59:46 +08:00
|
|
|
|
|
|
|
def check_for_language(lang_code):
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Checks whether there is a global language file for the given language
|
|
|
|
code. This is used to decide whether a user-provided language is
|
|
|
|
available. This is only used for language codes from either the cookies or
|
|
|
|
session.
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
|
|
|
from django.conf import settings
|
2006-05-02 09:31:56 +08:00
|
|
|
globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale')
|
2005-11-04 12:59:46 +08:00
|
|
|
if gettext_module.find('django', globalpath, [to_locale(lang_code)]) is not None:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get_language_from_request(request):
|
|
|
|
"""
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
Analyzes the request to find what language the user wants the system to
|
|
|
|
show. 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
|
2006-05-02 09:31:56 +08:00
|
|
|
globalpath = os.path.join(os.path.dirname(sys.modules[settings.__module__].__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
|
|
|
|
2008-03-01 02:38:44 +08:00
|
|
|
lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
|
2010-02-16 20:13:48 +08:00
|
|
|
|
|
|
|
if lang_code and lang_code not in supported:
|
|
|
|
lang_code = lang_code.split('-')[0] # e.g. if fr-ca is not supported fallback to fr
|
|
|
|
|
2007-10-27 03:52:42 +08:00
|
|
|
if lang_code and lang_code in supported 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
|
|
|
|
2007-10-27 03:52:42 +08:00
|
|
|
accept = request.META.get('HTTP_ACCEPT_LANGUAGE', '')
|
2008-02-06 09:04:30 +08:00
|
|
|
for accept_lang, unused in parse_accept_lang_header(accept):
|
|
|
|
if accept_lang == '*':
|
2007-10-27 03:52:42 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
# We have a very restricted form for our language files (no encoding
|
|
|
|
# specifier, since they all must be UTF-8 and only one possible
|
|
|
|
# language each time. So we avoid the overhead of gettext.find() and
|
2008-02-06 09:04:30 +08:00
|
|
|
# work out the MO file manually.
|
2007-10-27 03:52:42 +08:00
|
|
|
|
2008-02-06 09:04:30 +08:00
|
|
|
# 'normalized' is the root name of the locale in POSIX format (which is
|
|
|
|
# the format used for the directories holding the MO files).
|
|
|
|
normalized = locale.locale_alias.get(to_locale(accept_lang, True))
|
2007-10-27 03:52:42 +08:00
|
|
|
if not normalized:
|
|
|
|
continue
|
2008-03-08 11:55:47 +08:00
|
|
|
# Remove the default encoding from locale_alias.
|
2007-10-27 03:52:42 +08:00
|
|
|
normalized = normalized.split('.')[0]
|
|
|
|
|
|
|
|
if normalized in _accepted:
|
|
|
|
# We've seen this locale before and have an MO file for it, so no
|
|
|
|
# need to check again.
|
|
|
|
return _accepted[normalized]
|
|
|
|
|
2008-02-06 09:04:30 +08:00
|
|
|
for lang, dirname in ((accept_lang, normalized),
|
|
|
|
(accept_lang.split('-')[0], normalized.split('_')[0])):
|
2008-08-26 06:53:44 +08:00
|
|
|
if lang.lower() not in supported:
|
2007-10-27 03:52:42 +08:00
|
|
|
continue
|
2008-02-06 09:04:30 +08:00
|
|
|
langfile = os.path.join(globalpath, dirname, 'LC_MESSAGES',
|
2007-10-27 03:52:42 +08:00
|
|
|
'django.mo')
|
|
|
|
if os.path.exists(langfile):
|
|
|
|
_accepted[normalized] = lang
|
2008-08-27 13:10:20 +08:00
|
|
|
return lang
|
2005-11-07 05:55:39 +08:00
|
|
|
|
2005-11-04 12:59:46 +08:00
|
|
|
return settings.LANGUAGE_CODE
|
|
|
|
|
|
|
|
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.
|
2006-01-27 23:49:50 +08:00
|
|
|
Used in the templatize 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"""_\(((?:".*?")|(?:'.*?'))\)""")
|
2008-03-08 11:55:47 +08:00
|
|
|
|
2006-01-27 23:49:50 +08:00
|
|
|
def templatize(src):
|
2005-11-04 12:59:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK
|
2005-11-04 12:59:46 +08:00
|
|
|
out = StringIO()
|
|
|
|
intrans = False
|
|
|
|
inplural = False
|
|
|
|
singular = []
|
|
|
|
plural = []
|
2005-11-24 11:32:26 +08:00
|
|
|
for t in Lexer(src, None).tokenize():
|
2005-11-04 12:59:46 +08:00
|
|
|
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:
|
2008-03-08 11:55:47 +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:
|
2007-09-15 13:14:16 +08:00
|
|
|
for fmatch in constant_re.findall(t.contents):
|
|
|
|
out.write(' _(%s) ' % fmatch)
|
2005-11-04 12:59:46 +08:00
|
|
|
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()
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-10-27 03:52:42 +08:00
|
|
|
def parse_accept_lang_header(lang_string):
|
|
|
|
"""
|
|
|
|
Parses the lang_string, which is the body of an HTTP Accept-Language
|
|
|
|
header, and returns a list of (lang, q-value), ordered by 'q' values.
|
|
|
|
|
|
|
|
Any format errors in lang_string results in an empty list being returned.
|
|
|
|
"""
|
|
|
|
result = []
|
|
|
|
pieces = accept_language_re.split(lang_string)
|
|
|
|
if pieces[-1]:
|
|
|
|
return []
|
|
|
|
for i in range(0, len(pieces) - 1, 3):
|
|
|
|
first, lang, priority = pieces[i : i + 3]
|
|
|
|
if first:
|
|
|
|
return []
|
|
|
|
priority = priority and float(priority) or 1.0
|
|
|
|
result.append((lang, priority))
|
|
|
|
result.sort(lambda x, y: -cmp(x[1], y[1]))
|
|
|
|
return result
|
2009-12-23 01:58:49 +08:00
|
|
|
|
|
|
|
# get_date_formats and get_partial_date_formats aren't used anymore by Django
|
|
|
|
# and are kept for backward compatibility.
|
|
|
|
# Note, it's also important to keep format names marked for translation.
|
|
|
|
# For compatibility we still want to have formats on translation catalogs.
|
|
|
|
# That makes template code like {{ my_date|date:_('DATE_FORMAT') }} still work
|
|
|
|
def get_date_formats():
|
|
|
|
"""
|
|
|
|
Checks whether translation files provide a translation for some technical
|
|
|
|
message ID to store date and time formats. If it doesn't contain one, the
|
|
|
|
formats provided in the settings will be used.
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
2010-04-29 22:36:09 +08:00
|
|
|
"'django.utils.translation.get_date_formats' is deprecated. "
|
|
|
|
"Please update your code to use the new i18n aware formatting.",
|
2009-12-23 01:58:49 +08:00
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
|
|
|
from django.conf import settings
|
|
|
|
date_format = ugettext('DATE_FORMAT')
|
|
|
|
datetime_format = ugettext('DATETIME_FORMAT')
|
|
|
|
time_format = ugettext('TIME_FORMAT')
|
|
|
|
if date_format == 'DATE_FORMAT':
|
|
|
|
date_format = settings.DATE_FORMAT
|
|
|
|
if datetime_format == 'DATETIME_FORMAT':
|
|
|
|
datetime_format = settings.DATETIME_FORMAT
|
|
|
|
if time_format == 'TIME_FORMAT':
|
|
|
|
time_format = settings.TIME_FORMAT
|
|
|
|
return date_format, datetime_format, time_format
|
|
|
|
|
|
|
|
def get_partial_date_formats():
|
|
|
|
"""
|
|
|
|
Checks whether translation files provide a translation for some technical
|
|
|
|
message ID to store partial date formats. If it doesn't contain one, the
|
|
|
|
formats provided in the settings will be used.
|
|
|
|
"""
|
|
|
|
warnings.warn(
|
2010-04-29 22:36:09 +08:00
|
|
|
"'django.utils.translation.get_partial_date_formats' is deprecated. "
|
|
|
|
"Please update your code to use the new i18n aware formatting.",
|
2009-12-23 01:58:49 +08:00
|
|
|
PendingDeprecationWarning
|
|
|
|
)
|
|
|
|
from django.conf import settings
|
|
|
|
year_month_format = ugettext('YEAR_MONTH_FORMAT')
|
|
|
|
month_day_format = ugettext('MONTH_DAY_FORMAT')
|
|
|
|
if year_month_format == 'YEAR_MONTH_FORMAT':
|
|
|
|
year_month_format = settings.YEAR_MONTH_FORMAT
|
|
|
|
if month_day_format == 'MONTH_DAY_FORMAT':
|
|
|
|
month_day_format = settings.MONTH_DAY_FORMAT
|
|
|
|
return year_month_format, month_day_format
|
|
|
|
|