|
@@ -64,6 +64,7 @@ def to_locale(language, to_lower=False):
|
|
|
else:
|
|
|
return language.lower()
|
|
|
|
|
|
+
|
|
|
def to_language(locale):
|
|
|
"""Turns a locale name (en_US) into a language name (en-us)."""
|
|
|
p = locale.find('_')
|
|
@@ -72,6 +73,7 @@ def to_language(locale):
|
|
|
else:
|
|
|
return locale.lower()
|
|
|
|
|
|
+
|
|
|
class DjangoTranslation(gettext_module.GNUTranslations):
|
|
|
"""
|
|
|
This class sets up the GNUTranslations context with regard to output
|
|
@@ -98,6 +100,7 @@ class DjangoTranslation(gettext_module.GNUTranslations):
|
|
|
def __repr__(self):
|
|
|
return "<DjangoTranslation lang:%s>" % self.__language
|
|
|
|
|
|
+
|
|
|
def translation(language):
|
|
|
"""
|
|
|
Returns a translation object.
|
|
@@ -179,6 +182,7 @@ def translation(language):
|
|
|
|
|
|
return current_translation
|
|
|
|
|
|
+
|
|
|
def activate(language):
|
|
|
"""
|
|
|
Fetches the translation object for a given tuple of application name and
|
|
@@ -187,6 +191,7 @@ def activate(language):
|
|
|
"""
|
|
|
_active.value = translation(language)
|
|
|
|
|
|
+
|
|
|
def deactivate():
|
|
|
"""
|
|
|
Deinstalls the currently active translation object so that further _ calls
|
|
@@ -195,6 +200,7 @@ def deactivate():
|
|
|
if hasattr(_active, "value"):
|
|
|
del _active.value
|
|
|
|
|
|
+
|
|
|
def deactivate_all():
|
|
|
"""
|
|
|
Makes the active translation object a NullTranslations() instance. This is
|
|
@@ -203,6 +209,7 @@ def deactivate_all():
|
|
|
"""
|
|
|
_active.value = gettext_module.NullTranslations()
|
|
|
|
|
|
+
|
|
|
def get_language():
|
|
|
"""Returns the currently selected language."""
|
|
|
t = getattr(_active, "value", None)
|
|
@@ -215,6 +222,7 @@ def get_language():
|
|
|
from django.conf import settings
|
|
|
return settings.LANGUAGE_CODE
|
|
|
|
|
|
+
|
|
|
def get_language_bidi():
|
|
|
"""
|
|
|
Returns selected language's BiDi layout.
|
|
@@ -227,6 +235,7 @@ def get_language_bidi():
|
|
|
base_lang = get_language().split('-')[0]
|
|
|
return base_lang in settings.LANGUAGES_BIDI
|
|
|
|
|
|
+
|
|
|
def catalog():
|
|
|
"""
|
|
|
Returns the current active catalog for further processing.
|
|
@@ -243,6 +252,7 @@ def catalog():
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
|
|
return _default
|
|
|
|
|
|
+
|
|
|
def do_translate(message, translation_function):
|
|
|
"""
|
|
|
Translates 'message' using the given 'translation_function' name -- which
|
|
@@ -266,6 +276,7 @@ def do_translate(message, translation_function):
|
|
|
return mark_safe(result)
|
|
|
return result
|
|
|
|
|
|
+
|
|
|
def gettext(message):
|
|
|
"""
|
|
|
Returns a string of the translation of the message.
|
|
@@ -280,6 +291,7 @@ else:
|
|
|
def ugettext(message):
|
|
|
return do_translate(message, 'ugettext')
|
|
|
|
|
|
+
|
|
|
def pgettext(context, message):
|
|
|
msg_with_ctxt = "%s%s%s" % (context, CONTEXT_SEPARATOR, message)
|
|
|
result = ugettext(msg_with_ctxt)
|
|
@@ -288,6 +300,7 @@ def pgettext(context, message):
|
|
|
result = message
|
|
|
return result
|
|
|
|
|
|
+
|
|
|
def gettext_noop(message):
|
|
|
"""
|
|
|
Marks strings for translation but doesn't translate them now. This can be
|
|
@@ -297,6 +310,7 @@ def gettext_noop(message):
|
|
|
"""
|
|
|
return message
|
|
|
|
|
|
+
|
|
|
def do_ntranslate(singular, plural, number, translation_function):
|
|
|
global _default
|
|
|
|
|
@@ -308,6 +322,7 @@ def do_ntranslate(singular, plural, number, translation_function):
|
|
|
_default = translation(settings.LANGUAGE_CODE)
|
|
|
return getattr(_default, translation_function)(singular, plural, number)
|
|
|
|
|
|
+
|
|
|
def ngettext(singular, plural, number):
|
|
|
"""
|
|
|
Returns a string of the translation of either the singular or plural,
|
|
@@ -327,6 +342,7 @@ else:
|
|
|
"""
|
|
|
return do_ntranslate(singular, plural, number, 'ungettext')
|
|
|
|
|
|
+
|
|
|
def npgettext(context, singular, plural, number):
|
|
|
msgs_with_ctxt = ("%s%s%s" % (context, CONTEXT_SEPARATOR, singular),
|
|
|
"%s%s%s" % (context, CONTEXT_SEPARATOR, plural),
|
|
@@ -337,6 +353,7 @@ def npgettext(context, singular, plural, number):
|
|
|
result = ungettext(singular, plural, number)
|
|
|
return result
|
|
|
|
|
|
+
|
|
|
def all_locale_paths():
|
|
|
"""
|
|
|
Returns a list of paths to user-provides languages files.
|
|
@@ -346,6 +363,7 @@ def all_locale_paths():
|
|
|
os.path.dirname(upath(sys.modules[settings.__module__].__file__)), 'locale')
|
|
|
return [globalpath] + list(settings.LOCALE_PATHS)
|
|
|
|
|
|
+
|
|
|
def check_for_language(lang_code):
|
|
|
"""
|
|
|
Checks whether there is a global language file for the given language
|
|
@@ -359,6 +377,7 @@ def check_for_language(lang_code):
|
|
|
return False
|
|
|
check_for_language = memoize(check_for_language, _checked_languages, 1)
|
|
|
|
|
|
+
|
|
|
def get_supported_language_variant(lang_code, supported=None, strict=False):
|
|
|
"""
|
|
|
Returns the language-code that's listed in supported languages, possibly
|
|
@@ -386,6 +405,7 @@ def get_supported_language_variant(lang_code, supported=None, strict=False):
|
|
|
return supported_code
|
|
|
raise LookupError(lang_code)
|
|
|
|
|
|
+
|
|
|
def get_language_from_path(path, supported=None, strict=False):
|
|
|
"""
|
|
|
Returns the language-code if there is a valid language-code
|
|
@@ -406,6 +426,7 @@ def get_language_from_path(path, supported=None, strict=False):
|
|
|
except LookupError:
|
|
|
return None
|
|
|
|
|
|
+
|
|
|
def get_language_from_request(request, check_path=False):
|
|
|
"""
|
|
|
Analyzes the request to find what language the user wants the system to
|
|
@@ -470,6 +491,8 @@ def get_language_from_request(request, check_path=False):
|
|
|
return settings.LANGUAGE_CODE
|
|
|
|
|
|
dot_re = re.compile(r'\S')
|
|
|
+
|
|
|
+
|
|
|
def blankout(src, char):
|
|
|
"""
|
|
|
Changes every non-whitespace character to the given char.
|
|
@@ -653,6 +676,7 @@ def templatize(src, origin=None):
|
|
|
out.write(blankout(t.contents, 'X'))
|
|
|
return force_str(out.getvalue())
|
|
|
|
|
|
+
|
|
|
def parse_accept_lang_header(lang_string):
|
|
|
"""
|
|
|
Parses the lang_string, which is the body of an HTTP Accept-Language
|