def do_ntranslate(singular, plural, number, translation_function): language = trans_real.get_language() translation = trans_real.translation(language) number = translation.plural(number) kw = {'msgid': singular, 'plural': number, 'language': language, 'domain': 'django'} try: message = Message.objects.get(**kw) except Message.DoesNotExist: # Miss msgstr = do_ntranslate_old(singular, plural, number, translation_function) kw['msgstr'] = msgstr message = Message(**kw) message.save() return msgstr # Hit translation = message.translation if not translation: return do_ntranslate_old(singular, plural, number, translation_function) return translation
def _activate(locale): # XXX TODO: When it comes time to load .mo files on the fly and merge # them, this is the place to do it. We'll also need to implement our own # caching since the _translations stuff is built on a per locale basis, # not per locale + some key # Django caches the translation objects here t = django_trans._translations.get(locale, None) if t is not None: return t # Django's activate() simply calls translation() and adds it to a global. # We'll do the same here, first calling django's translation() so it can # do everything it needs to do, and then calling gettext directly to # load the rest. t = django_trans.translation(locale) try: """When trying to load css, js, and images through the Django server gettext() throws an exception saying it can't find the .mo files. I suspect this has something to do with Django trying not to load extra stuff for requests that won't need it. I do know that I don't want to try to debug it. This is what Django does in their function also. """ #If you've got extra .mo files to load, this is the place. path = import_module(settings.SETTINGS_MODULE).path bonus = gettext.translation('messages', path('locale'), [locale], django_trans.DjangoTranslation) t.merge(bonus) except IOError: pass return t
def get_user_translation(self, user): """ Convenience method which you probably do not want to override """ if USE_I18N: return trans_real.translation(self.get_user_language_code(user)) else: return None
def __init__(self, full_name, course_name, organization, filename, teachers, language): self.full_name = full_name self.course_name = course_name self.organization = organization self.filename = filename self.teachers = teachers self._ = translation(language if language else "fr").ugettext
def _render_part(self, template, **kwargs): t = translation(self.language) if template == "": # by convention, translating the empty string "" # returns metadata about the translation file, which # no one really ever wants to see return template tmpl = t.gettext(template) return tmpl % kwargs
def get_regex(self, lang=None): lang = lang or get_language() # Only attempt to get the translation of the regex if the regex string # is not empty. The empty string is handled as a special case by # Django's gettext. It's where it stores its metadata. if self._raw_regex != '': regex_in_lang = translation(lang).ugettext(self._raw_regex) else: regex_in_lang = self._raw_regex return self._regex_dict.setdefault(lang, re.compile(regex_in_lang, re.UNICODE))
def get_regex(self, lang=None): lang = lang or get_language() # Only attempt to get the translation of the regex if the regex string # is not empty. The empty string is handled as a special case by # Django's gettext. It's where it stores its metadata. if self._raw_regex != '': regex_in_lang = translation(lang).ugettext(self._raw_regex) else: regex_in_lang = self._raw_regex return self._regex_dict.setdefault(lang, re.compile(regex_in_lang, re.UNICODE))
def __init__( self, full_name, course_name, organization, filename, teachers, language ): self.full_name = full_name self.course_name = course_name self.organization = organization self.filename = filename self.teachers = teachers self._ = translation(language if language else "fr").ugettext
def setup_mofile_with_entry(self, poentry, language='en'): locale_location = join(settings.LOCALE_DIR, 'locale', language, 'LC_MESSAGES') pofile = polib.pofile(join(locale_location, 'django.po')) pofile.append(poentry) pofile.save() pofile.save_as_mofile(join(locale_location, 'django.mo')) jit_locale = gettext_module.translation('django', join(settings.LOCALE_DIR, 'locale'), [trans_real.to_locale(language)], trans_real.DjangoTranslation) jit_locale.set_language(language) locale = trans_real.translation(language) locale.merge(jit_locale)
def matches_in_any_language(string_to_translate, string_to_check): def clean(s): return s.lower().strip() if clean(string_to_translate) == clean(string_to_check): return True for lang in ["en", "sw"]: translated = translation(lang).gettext(string_to_translate) if clean(translated) == clean(string_to_check): return True else: pass #print "%s is not %s" % (translated, string_to_check) return False
def trans_in_lang(string, lang): """ Translate a string into a specific language (which can be different than the set language). Usage: {{ var|trans_in_lang:"fr" }} """ if check_for_language(lang): return translation(lang).ugettext(string) return string
def trans_in_lang(string, lang): """ Translate a string into a specific language (which can be different than the set language). Usage: {{ var|trans_in_lang:"fr" }} """ if check_for_language(lang): return translation(lang).ugettext(string) return string
def _activate(locale): # XXX TODO: When it comes time to load .mo files on the fly and merge # them, this is the place to do it. We'll also need to implement our own # caching since the _translations stuff is built on a per locale basis, # not per locale + some key locale = django_trans.to_locale(locale) # Django caches the translation objects here t = django_trans._translations.get(locale, None) if t is not None: return t # Django's activate() simply calls translation() and adds it to a global. # We'll do the same here, first calling django's translation() so it can # do everything it needs to do, and then calling gettext directly to # load the rest. We make a deepcopy because Django will return the en-US # catalog if it doesn't have a locale (but we do). We don't want to merge # our foreign catalog into en-US. Since Django stuck the en-US catalog # into its cache for this locale, we have to update that too. t = copy.deepcopy(django_trans.translation(locale)) t.set_language(locale) try: # When trying to load css, js, and images through the Django server # gettext() throws an exception saying it can't find the .mo files. I # suspect this has something to do with Django trying not to load # extra stuff for requests that won't need it. I do know that I don't # want to try to debug it. This is what Django does in their function # also. # # We check for SETTINGS_MODULE here because if it's not here, then # it's possible we're in a test using override_settings and we don't # want to flip out. settings_module = getattr(settings, 'SETTINGS_MODULE', None) if settings_module: # If you've got extra .mo files to load, this is the place. path = import_module(settings_module).path domain = getattr(settings, 'TEXT_DOMAIN', 'messages') bonus = gettext.translation(domain, path('locale'), [locale], django_trans.DjangoTranslation) t.merge(bonus) # Overwrite t (defaults to en-US) with our real locale's plural form t.plural = bonus.plural except IOError: pass django_trans._translations[locale] = t return t
def _activate(locale): # XXX TODO: When it comes time to load .mo files on the fly and merge # them, this is the place to do it. We'll also need to implement our own # caching since the _translations stuff is built on a per locale basis, # not per locale + some key locale = django_trans.to_locale(locale) # Django caches the translation objects here t = django_trans._translations.get(locale, None) if t is not None: return t # Django's activate() simply calls translation() and adds it to a global. # We'll do the same here, first calling django's translation() so it can # do everything it needs to do, and then calling gettext directly to # load the rest. We make a deepcopy because Django will return the en-US # catalog if it doesn't have a locale (but we do). We don't want to merge # our foreign catalog into en-US. Since Django stuck the en-US catalog # into its cache for this locale, we have to update that too. t = copy.deepcopy(django_trans.translation(locale)) t.set_language(locale) try: # When trying to load css, js, and images through the Django server # gettext() throws an exception saying it can't find the .mo files. I # suspect this has something to do with Django trying not to load # extra stuff for requests that won't need it. I do know that I don't # want to try to debug it. This is what Django does in their function # also. # # We check for SETTINGS_MODULE here because if it's not here, then # it's possible we're in a test using override_settings and we don't # want to flip out. settings_module = getattr(settings, 'SETTINGS_MODULE', None) if settings_module: # If you've got extra .mo files to load, this is the place. path = import_module(settings_module).path domain = getattr(settings, 'TEXT_DOMAIN', 'messages') bonus = gettext.translation(domain, path('locale'), [locale], django_trans.DjangoTranslation) t.merge(bonus) # Overwrite t (defaults to en-US) with our real locale's plural form t.plural = bonus.plural except IOError: pass django_trans._translations[locale] = t return t
def matches_in_any_language(string_to_translate, string_to_check): def clean(s): return s.lower().strip() if clean(string_to_translate) == clean(string_to_check): return True for lang in ["en", "sw"]: translated = translation(lang).gettext(string_to_translate) if clean(translated) == clean(string_to_check): return True else: pass #print "%s is not %s" % (translated, string_to_check) return False
def setup_mofile_with_entry(self, poentry, language='en'): locale_location = join(settings.LOCALE_DIR, 'locale', language, 'LC_MESSAGES') pofile = polib.pofile(join(locale_location, 'django.po')) pofile.append(poentry) pofile.save() pofile.save_as_mofile(join(locale_location, 'django.mo')) jit_locale = gettext_module.translation( 'django', join(settings.LOCALE_DIR, 'locale'), [trans_real.to_locale(language)], trans_real.DjangoTranslation) jit_locale.set_language(language) locale = trans_real.translation(language) locale.merge(jit_locale)
def do_translate(message, translation_function, lang_code): """ 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. """ t = _active.get(currentThread(), None) if t is not None: result = getattr(t, translation_function)(message) else: if _default is None: from django.conf import settings _default = translation(lang_code) result = getattr(_default, translation_function)(message) if isinstance(message, SafeData): return mark_safe(result) return result
def merge_translation(self, context): """ Context wrapper which modifies the given language's translation catalog using the i18n service, if found. """ language = get_language() i18n_service = context.get('_i18n_service', None) if i18n_service: # Cache the original translation object to reduce overhead if language not in self._translations: self._translations[language] = trans_real.DjangoTranslation(language) translation = trans_real.translation(language) translation.merge(i18n_service) yield # Revert to original translation object if language in self._translations: trans_real._translations[language] = self._translations[language] # Re-activate the current language to reset translation caches trans_real.activate(language)
def get_translation_object(*, django_language_code: str, domain: str) -> DjangoTranslation: """ Return a DjangoTranslation object suitable to activate when we're wanting to render templates for this language code and domain. (The domain is typically specific to one or a few licenses that have common translations.) """ license_locale_dir = os.path.join( settings.TRANSLATION_REPOSITORY_DIRECTORY, "translations") # Start with a translation object for the domain for this license. license_translation_object = DjangoTranslation( language=django_language_code, domain=domain, localedirs=[license_locale_dir]) # Add a fallback to the standard Django translation for this language. This gets us the # non-legalcode parts of the pages. license_translation_object.add_fallback(translation(django_language_code)) return license_translation_object
def merge_translation(self, context): """ Context wrapper which modifies the given language's translation catalog using the i18n service, if found. """ language = get_language() i18n_service = context.get('_i18n_service', None) if i18n_service: # Cache the original translation object to reduce overhead if language not in self._translations: self._translations[language] = trans_real.DjangoTranslation( language) translation = trans_real.translation(language) translation.merge(i18n_service) yield # Revert to original translation object if language in self._translations: trans_real._translations[language] = self._translations[language] # Re-activate the current language to reset translation caches trans_real.activate(language)
def translate_to(value, language_code): t = translation(language_code) return getattr(t, 'gettext')(value)
def _render_part(self, template, **kwargs): t = translation(self.language) tmpl = t.gettext(template) return tmpl % kwargs
""" t = _active.get(currentThread(), None) if t is not None: result = getattr(t, translation_function)(message) else: if _default is None: from django.conf import settings _default = translation(lang_code) result = getattr(_default, translation_function)(message) if isinstance(message, SafeData): return mark_safe(result) return result LOCAL_LANGUAGES = [] for code, value in settings.LANGUAGES: t = translation(code) trans_value = getattr(t, 'gettext')(value) LOCAL_LANGUAGES.append((code, trans_value)) def utils(request): """ all utils objects: - 'intelligent' language object - OpenID """ return { 'LOCAL_LANGUAGES' : LOCAL_LANGUAGES, 'OPENID' : settings.OPENID, }
def translate_to(value, language_code): t = translation(language_code) return getattr(t, 'gettext')(value)
def _render_part(self, template, **kwargs): t = translation(self.language) tmpl = t.gettext(template) return tmpl % kwargs
def ready(self): for lang, fallback in settings.FALLBACK_LANGUAGES.items(): translation(lang)._fallback = translation(fallback)
def get_regex(self, lang=None): lang = lang or get_language() return self._regex_dict.setdefault(lang, re.compile(translation(lang).ugettext(self._raw_regex), re.UNICODE))
# coding=utf-8 import os import logging from django.core.management.base import BaseCommand, CommandParser from django.conf import settings from django.apps import apps from django.utils.translation.trans_real import translation from prettytable import PrettyTable, ALL, FRAME, NONE logger = logging.getLogger(__name__) TRANS = translation(settings.LANGUAGE_CODE) BASE_DIR = os.path.join(settings.BASE_DIR, 'doc', 'source') AUTHOR_NAME = os.popen("git config --global --get user.name").read().strip() AUTHOR_EMAIL = os.popen("git config --global --get user.email").read().strip() FILE_MAP = { 'REF': '/rst/api/%(app_name)s/ref.%(out_put)s.rst', 'RST_I': '/rst/api/%(app_name)s/%(model_name)s.dbinfo.rst', 'RST_C': '/rst/api/%(app_name)s/%(target)s.create.rst', 'RST_L': '/rst/api/%(app_name)s/%(target)s.list.rst', 'RST_R': '/rst/api/%(app_name)s/%(target)s.detail.rst', 'RST_U': '/rst/api/%(app_name)s/%(target)s.update.rst', 'RST_A': '/rst/api/%(app_name)s/%(target)s.action.rst', 'RST_D': '/rst/api/%(app_name)s/%(target)s.delete.rst', 'JSON_L': '/static/json/api/%(app_name)s/%(target)s.list.json', 'JSON_C': '/static/json/api/%(app_name)s/%(target)s.create.json', 'JSON_R': '/static/json/api/%(app_name)s/%(target)s.detail.json', 'JSON_U': '/static/json/api/%(app_name)s/%(target)s.update.json', 'JSON_A': '/static/json/api/%(app_name)s/%(target)s.action.json',
def get_regex(self, lang=None): lang = lang or get_language() return self._regex_dict.setdefault(lang, re.compile(translation(lang).ugettext(self._raw_regex), re.UNICODE))