def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = stack.top if ctx is None: return NullTranslations() locale = get_locale() cache = self.get_translations_cache(ctx) translations = cache.get(str(locale)) if translations is None: translations_dir = self.get_translations_path(ctx) translations = Translations.load(translations_dir, locale, domain=self.domain) # Load plugins translations if isinstance(translations, Translations): # Load core extensions translations from wtforms.i18n import messages_path wtforms_translations = Translations.load(messages_path(), locale, domain='wtforms') translations.merge(wtforms_translations) import flask_security flask_security_translations = Translations.load( join(flask_security.__path__[0], 'translations'), locale, domain='flask_security' ) translations.merge(flask_security_translations) for pkg in entrypoints.get_roots(current_app): package = pkgutil.get_loader(pkg) path = join(package.filename, 'translations') domains = [f.replace(path, '').replace('.pot', '')[1:] for f in iglob(join(path, '*.pot'))] for domain in domains: translations.merge(Translations.load(path, locale, domain=domain)) # Allows the theme to provide or override translations from . import theme theme_translations_dir = join(theme.current.path, 'translations') if exists(theme_translations_dir): domain = theme.current.identifier theme_translations = Translations.load(theme_translations_dir, locale, domain=domain) translations.merge(theme_translations) cache[str(locale)] = translations return translations
def __init__(self, template_name, search_paths, log, html_templates_i18n_dirs=[], locale=None): ''' @template_name e.g. "base.html" @search_paths list type, start elements has high priority ''' self.log = log self.env = None if html_templates_i18n_dirs: if not locale: locale = "en" translations_merge = Translations.load(html_templates_i18n_dirs[0], [locale]) if type(translations_merge) != NullTranslations: for dir in html_templates_i18n_dirs[1:]: translations = Translations.load(dir, [locale]) if type(translations) != NullTranslations: translations_merge.merge(translations) self.env = Environment(extensions=['jinja2.ext.i18n'], loader=FileSystemLoader(search_paths)) self.env.install_gettext_translations(translations_merge) if not self.env: self.env = Environment(loader=FileSystemLoader(search_paths)) self.template = template_name
def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = stack.top if ctx is None: return NullTranslations() locale = get_locale() cache = self.get_translations_cache(ctx) translations = cache.get(str(locale)) if translations is None: translations_dir = self.get_translations_path(ctx) translations = Translations.load(translations_dir, locale, domain=self.domain) # Load plugins translations if isinstance(translations, Translations): for plugin_name in current_app.config['PLUGINS']: module_name = 'udata.ext.{0}'.format(plugin_name) module = import_module(module_name) translations_dir = join(dirname(module.__file__), 'translations') if exists(translations_dir): domain = '-'.join((self.domain, plugin_name)) plugins_translations = Translations.load(translations_dir, locale, domain=domain) translations.merge(plugins_translations) cache[str(locale)] = translations return translations
def load_gettext_translations(directory, domain): """Loads translations from gettext's locale tree""" global _translations global _supported_locales global _use_gettext _translations = {} for lang in os.listdir(directory): if lang.startswith('.'): continue # skip .svn, etc if os.path.isfile(os.path.join(directory, lang)): continue try: # Load existing translation or Null Translations translation = _translations.get(lang, Translations.load()) if isinstance(translation, gettext.NullTranslations): _translations[lang] = Translations.load( directory, [lang], domain ) else: _translations[lang].merge( Translations.load(directory, [lang], domain) ) except Exception, e: logging.error("Cannot load translation for '%s': %s", lang, str(e)) continue
def __init__(self, fileobj=None, locale=None): self.lang = locale self._catalog = {} try: TranslationsBase.__init__(self, fileobj=fileobj) except TypeError: TranslationsBase.__init__(self, fp=fileobj) if not hasattr(self, "plural"): self.plural = lambda n: int(n != 1)
def get_translations(self): """Returns the correct gettext translations that should be used for this request. This will never fail and return a dummy translation object if used outside of the request or if a translation cannot be found. """ ctx = stack.top if ctx is None: return NullTranslations() locale = get_locale() cache = self.get_translations_cache(ctx) translations = cache.get(str(locale)) if translations is None: translations_dir = self.get_translations_path(ctx) translations = Translations.load(translations_dir, locale, domain=self.domain) # Load plugins translations if isinstance(translations, Translations): # Load core extensions translations from wtforms.i18n import messages_path wtforms_translations = Translations.load(messages_path(), locale, domain='wtforms') translations.merge(wtforms_translations) for plugin_name in current_app.config['PLUGINS']: module_name = 'udata_{0}'.format(plugin_name) module = import_module(module_name) translations_dir = join(dirname(module.__file__), 'translations') if exists(translations_dir): domain = '-'.join((self.domain, plugin_name)) plugins_translations = Translations.load( translations_dir, locale, domain=domain) translations.merge(plugins_translations) # Allows the theme to provide or override translations from . import theme theme_translations_dir = join(theme.current.path, 'translations') if exists(theme_translations_dir): domain = theme.current.identifier theme_translations = Translations.load(theme_translations, locale, domain=domain) translations.merge(theme_translations) cache[str(locale)] = translations return translations
def new_translator(languages=None): lang = languages or LANGUAGES translations = Translations.load(dirname(__file__), lang, 'weckan') if not isinstance(translations, Translations): return translations for name, path in EXTRA_TRANSLATIONS: translations.merge(Translations.load(path, lang, name)) return translations
def lookup_translation(): ctx = _request_ctx_stack.top if ctx is None: return None translations = getattr(ctx, 'pycroft_translations', None) if translations is None: translations = Translations() for module in (pycroft, web): os.path.dirname(module.__file__) dirname = os.path.join(ctx.app.root_path, 'translations') translations.merge(Translations.load(dirname, [get_locale()])) ctx.pycroft_translations = translations return translations
def _parse(self, fileobj): TranslationsBase._parse(self, fileobj) try: # Got the end of file minus 4 bytes fileobj.seek(-4, 2) # Read stored pickled data file pointer position pickled_data_pointer_pos = struct.unpack('i', fileobj.read()) fileobj.seek(pickled_data_pointer_pos[0]) # Load pickled data self.client_keys.update(pickle.load(fileobj)) except EOFError: # Catalog does not contain any pickled data at the end of it pass
def get_translation_for(self, locale, domain): translation = self.translations.get((locale, domain), None) if not translation: with self.map_lock: for package in ReahlSystemConfig().translation_packages: for locale_dir in package.__path__: if not isinstance(translation, Translations): translation = Translations.load(dirname=locale_dir, locales=[locale], domain=domain) # Babel 1.3 bug under Python 3: files is a filter object, not a list like in Python 2 translation.files = list(translation.files) else: translation.merge(Translations.load(dirname=locale_dir, locales=[locale], domain=domain)) self.translations[(locale, domain)] = translation return translation or gettext.NullTranslations()
def render_j2_template(config, template, data, locale_=None): """ render Jinja2 template :param config: dict of configuration :param template: template (relative path) :param data: dict of data :param locale_: the requested output Locale :returns: string of rendered template """ custom_templates = False try: templates_path = config['server']['templates']['path'] env = Environment(loader=FileSystemLoader(templates_path), extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'], autoescape=select_autoescape(['html', 'xml'])) custom_templates = True LOGGER.debug('using custom templates: {}'.format(templates_path)) except (KeyError, TypeError): env = Environment(loader=FileSystemLoader(TEMPLATES), extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'], autoescape=select_autoescape(['html', 'xml'])) LOGGER.debug('using default templates: {}'.format(TEMPLATES)) env.filters['to_json'] = to_json env.filters['format_datetime'] = format_datetime env.filters['format_duration'] = format_duration env.filters['human_size'] = human_size env.globals.update(to_json=to_json) env.filters['get_path_basename'] = get_path_basename env.globals.update(get_path_basename=get_path_basename) env.filters['get_breadcrumbs'] = get_breadcrumbs env.globals.update(get_breadcrumbs=get_breadcrumbs) env.filters['filter_dict_by_key_value'] = filter_dict_by_key_value env.globals.update(filter_dict_by_key_value=filter_dict_by_key_value) translations = Translations.load('locale', [locale_]) env.install_gettext_translations(translations) try: template = env.get_template(template) except TemplateNotFound as err: if custom_templates: LOGGER.debug(err) LOGGER.debug('Custom template not found; using default') env = Environment(loader=FileSystemLoader(TEMPLATES), extensions=['jinja2.ext.i18n']) template = env.get_template(template) else: raise return template.render(config=l10n.translate_struct(config, locale_, True), data=data, locale=locale_, version=__version__)
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11515 Show a notice message with new language setting after it is changed. """ from trac.util.translation import Locale, has_babel, \ get_available_locales from pkg_resources import resource_exists, resource_filename if not has_babel: return if not resource_exists('trac', 'locale'): return locale_dir = resource_filename('trac', 'locale') from babel.support import Translations string = 'Your preferences have been saved.' translated = None for second_locale_id in get_available_locales(): tx = Translations.load(locale_dir, second_locale_id) translated = tx.dgettext('messages', string) if string != translated: break # the locale has a translation else: return try: self._tester.go_to_preferences('Localization') tc.formvalue('userprefs', 'language', second_locale_id) tc.submit() tc.find(re.escape(translated)) tc.find('<option selected="selected" value="%s">' % second_locale_id) finally: tc.formvalue('userprefs', 'language', '') # revert to default tc.submit() tc.find('Your preferences have been saved')
def get_translations(): """ Load .po file, cache .mo file repr in memory and return a Babel Translations object. This function is meant for monkey patching into Flask-Babel.""" ctx = _request_ctx_stack.top if ctx is None: return None translations_dict = ctx.app.babel_translations_dict lock = ctx.app.babel_translations_lock locale = str(get_locale()) lock.acquire() if translations_dict.get(locale) is None: mo_file = StringIO() dirname = os.path.join(ctx.app.root_path, 'translations') transfilename = os.path.join(dirname, taint_filename(locale), 'LC_MESSAGES', "messages.po") if os.path.exists(transfilename): catalog = read_po(file(transfilename, "r")) write_mo(mo_file, catalog) mo_file.seek(0) translations = Translations(fp=mo_file) else: translations = gettext.NullTranslations() translations_dict[locale] = translations else: translations = translations_dict[locale] lock.release() return translations
def load_translation(self,langs, dirname, domain): """Loads the first existing translations for known locale and saves the `Lang` object in a global cache for faster lookup on the next request. :parameters: langs : List List of languages as returned by `parse_accept_language_header`. dirname : String Directory of the translations (`tools.I18nTool.mo_dir`). domain : String Gettext domain of the catalog (`tools.I18nTool.domain`). :returns: Lang object with two attributes (Lang.trans = the translations object, Lang.locale = the corresponding Locale object). :rtype: Lang :raises: ImproperlyConfigured if no locale where known. """ locale = None for lang in langs: short = lang[:2].lower() try: locale = Locale.parse(lang) if (domain, short) in _languages: return _languages[(domain, short)] trans = Translations.load(dirname, short, domain) except (ValueError, UnknownLocaleError): continue # If the translation was found, exit loop if isinstance(trans, Translations): break if locale is None: raise ImproperlyConfigured('Default locale not known.') _languages[(domain, short)] = res = Lang(locale, trans) return res
def _get_translation_for_locale(self, locale): """Get translation for a specific locale.""" translations = None for dirname in self.paths: # Load a single catalog. catalog = Translations.load(dirname, [locale], domain=self.domain) if translations is None: if isinstance(catalog, NullTranslations): translations = catalog continue try: # Merge catalog into global catalog translations.merge(catalog) except AttributeError: # Translations is probably NullTranslations if isinstance(catalog, NullTranslations): current_app.logger.debug( "Compiled translations seems to be missing" " in {0}.".format(dirname)) continue raise return translations or NullTranslations()
def setLocale(locale): """ Set the current locale in the current thread context """ ContextManager.set('locale', locale) ContextManager.set('translation', Translations.load(LOCALE_DIR, locale, LOCALE_DOMAIN))
def _get_translation_for_locale(self, locale): """Get translation for a specific locale.""" translations = None for dirname in self.paths: # Load a single catalog. catalog = Translations.load(dirname, [locale], domain=self.domain) if translations is None: if isinstance(catalog, Translations): translations = catalog continue try: # Merge catalog into global catalog translations.merge(catalog) except AttributeError: # Translations is probably NullTranslations if isinstance(catalog, NullTranslations): current_app.logger.debug( 'Compiled translations seems to be missing' ' in {0}.'.format(dirname)) continue raise return translations or NullTranslations()
def render_template(jinja2_env, path, params=None, inline_css=False, locale='en_US', path_locale=None): ''' ''' params = params or {} if isinstance(jinja2_env, str) and os.path.exists(expand_path(jinja2_env)): jinja2_env = get_jinja2_env(jinja2_env) path_locale = expand_path(path_locale) if path_locale else path_locale if locale and path_locale: translations = Translations.load(path_locale, [locale]) jinja2_env.install_gettext_translations(translations, newstyle=True) params['_page'] = path template = jinja2_env.get_template(path) content = template.render(**params) if os.path.basename(path).split('.')[-1].lower() == 'html': if inline_css: log.debug('... Inlining CSS') content = Premailer(content, remove_classes=True, cssutils_logging_level=logging.ERROR, preserve_inline_attachments=False).transform() return content
def load_translation(self, langs, dirname, domain): """Loads the first existing translations for known locale and saves the `Lang` object in a global cache for faster lookup on the next request. :parameters: langs : List List of languages as returned by `parse_accept_language_header`. dirname : String Directory of the translations (`tools.I18nTool.mo_dir`). domain : String Gettext domain of the catalog (`tools.I18nTool.domain`). :returns: Lang object with two attributes (Lang.trans = the translations object, Lang.locale = the corresponding Locale object). :rtype: Lang :raises: ImproperlyConfigured if no locale where known. """ locale = None for lang in langs: short = lang[:2].lower() try: locale = Locale.parse(lang) if (domain, short) in _languages: return _languages[(domain, short)] trans = Translations.load(dirname, short, domain) except (ValueError, UnknownLocaleError) as e: print e continue # If the translation was found, exit loop if isinstance(trans, Translations): break if locale is None: raise ImproperlyConfigured('Default locale not known.') _languages[(domain, short)] = res = Lang(locale, trans) return res
def run(self, root): i18n_dir = self.extension.getConfig('i18n_dir') pot_path = os.path.join(i18n_dir, 'messages.pot') if os.path.exists(pot_path): with open(pot_path, 'r') as f: catalog = pofile.read_po(f) else: catalog = Catalog() lang = self.extension.getConfig('i18n_lang') mo_path = os.path.join(i18n_dir, lang, 'LC_MESSAGES', 'messages.mo') po_path = os.path.join(i18n_dir, lang, 'LC_MESSAGES', 'messages.po') if os.path.exists(po_path): with open(po_path, 'r') as f: lang_catalog = pofile.read_po(f) with open(mo_path, 'w') as mo: mofile.write_mo(mo, lang_catalog) translations = Translations.load(i18n_dir, locales=[lang]) self.translate(catalog, translations, root) with open(pot_path, 'w') as pot_file: pofile.write_po(pot_file, catalog)
def generate_page(lang="en-CA", filename="prod/index.html"): "Generate the library & archives home page" env = jinja2.Environment( autoescape=jinja2.select_autoescape(["html"]), extensions=["jinja2.ext.i18n"], loader=jinja2.FileSystemLoader("./templates/"), trim_blocks=True, lstrip_blocks=True, ) loc = lang.replace("-", "_") translations = Translations.load("locale", [loc]) env.install_gettext_translations(translations) template = env.get_template("library.html") hours = get_hours(lang) news = get_news(lang) databases = get_databases(lang) guides = get_guides(lang) libhelp = 643 if lang == "fr-CA": libhelp = 531 ctx = { "lang": lang, "hours": hours, "news": news, "databases": databases, "guides": guides, "libhelp": libhelp, } with open(filename, mode="w", encoding="utf-8") as outf: outf.write(template.render(ctx))
def runTest(self): """Test for regression of http://trac.edgewall.org/ticket/11515 Show a notice message with new language setting after it is changed. """ from trac.util.translation import has_babel, get_available_locales from pkg_resources import resource_exists, resource_filename if not has_babel: return if not resource_exists("trac", "locale"): return locale_dir = resource_filename("trac", "locale") from babel.support import Translations string = "Your preferences have been saved." translated = None for second_locale in get_available_locales(): tx = Translations.load(locale_dir, second_locale) translated = tx.dgettext("messages", string) if string != translated: break # the locale has a translation else: return try: self._tester.go_to_preferences("Language") tc.formvalue("userprefs", "language", second_locale) tc.submit() tc.find(re.escape(translated)) finally: tc.formvalue("userprefs", "language", "") # revert to default tc.submit() tc.find("Your preferences have been saved")
def catalog_to_translations(catalog): """ Helper function which converts catalog object to translation """ buf = BytesIO() write_mo(buf, catalog, use_fuzzy=True) buf.seek(0) return Translations(fp=buf)
def _get_translations(): translation_locale = request.form.get('use-translation') if translation_locale: babel_ext = flask_babel.current_app.extensions['babel'] translation = Translations.load(next(babel_ext.translation_directories), 'oc') else: translation = _flask_babel_get_translations() return translation
def load_translations(): """Load translations. Returns: Translations """ localizations_dir = pkg_resources.resource_filename( package_or_requirement='client', resource_name='localization') return Translations.load(dirname=localizations_dir, locales=[os.getenv('LC_LANGUAGE'), 'en_US'])
def load_translations(import_name, locale): """Loads gettext translations for the given locale from the specified package represented by the given import name. """ if import_name not in sys.modules: return None path = os.path.abspath(os.path.dirname(sys.modules[import_name].__file__)) path = os.path.join(path, 'locale') return Translations.load(path, [locale])
def pre_process_request(self, req, handler): try: from babel.support import Translations from pkg_resources import resource_filename global translations translations = Translations.load(resource_filename(__name__, 'locale'), req.locale) except ImportError: pass return handler
def get_translations(locale): """Get the translation for a locale.""" locale = Locale.parse(locale) translations = _translations.get(str(locale)) if translations is not None: return translations rv = Translations.load(os.path.dirname(__file__), [locale]) _translations[str(locale)] = rv return rv
def setup_i18n(self): if "HTTP_ACCEPT_LANGUAGE" in self.environ: # locales looks something like: ['en', 'en-us;q=0.7', 'ja;q=0.3'] locales = self.environ["HTTP_ACCEPT_LANGUAGE"].split(",") locales = [l.split(";")[0] for l in locales] else: # Default to English locales = "en" t = Translations.load(dirname="locale", locales=locales, domain="ginga") self.template_context.update(dict(_=t.ugettext, n_=t.ugettext, N_=t.ungettext))
def gettext_for(locale='en'): """ Returns the `ugettext` function for a specific locale Usage:: _ = gettext_for('ja') # Load the 'ja' translation library """ return Translations.load(os.path.join(BASEDIR, 'app', 'translations'), [locale]).ugettext
def get_translations(): # pragma: no cover from django.conf import settings # take only locale codes and discard locale names # languages = next(zip(*settings.LANGUAGES)) languages = get_language() dirname = settings.LOCALE_PATHS[0] translations = Translations.load(dirname, locales=languages, domain='django') return translations
def selectLang(): if request.args.get('lang'): lang = request.args.get("lang") else: return make_response('{}', 404) if lang not in ['en', 'zh_TW']: lang = 'zh_TW' session['lang'] = lang request.babel_translations = Translations.load('locales', [lang]) return make_response('{}', 200)
def activate(self, locale, env_path=None): try: locale_dir = pkg_resources.resource_filename('trac', 'locale') except Exception: self._activate_failed = True return t = Translations.load(locale_dir, locale or 'en_US') if not t or t.__class__ is NullTranslations: t = self._null_translations elif env_path: self._plugin_domains_lock.acquire() try: domains = list(self._plugin_domains.get(env_path, [])) finally: self._plugin_domains_lock.release() for domain, dirname in domains: t.add(Translations.load(dirname, locale, domain)) self._current.translations = t self._activate_failed = False
def setup_i18n( self ): if 'HTTP_ACCEPT_LANGUAGE' in self.environ: # locales looks something like: ['en', 'en-us;q=0.7', 'ja;q=0.3'] locales = self.environ['HTTP_ACCEPT_LANGUAGE'].split( ',' ) locales = [ l.split( ';' )[0] for l in locales ] else: # Default to English locales = 'en' t = Translations.load( dirname='locale', locales=locales, domain='ginga' ) self.template_context.update ( dict( _=t.ugettext, n_=t.ugettext, N_=t.ungettext ) )
def activate(self, locale, env_path=None): try: locale_dir = pkg_resources.resource_filename("trac", "locale") except Exception: self._activate_failed = True return t = Translations.load(locale_dir, locale or "en_US") if not t or t.__class__ is NullTranslations: t = self._null_translations elif env_path: self._plugin_domains_lock.acquire() try: domains = list(self._plugin_domains.get(env_path, [])) finally: self._plugin_domains_lock.release() for domain, dirname in domains: t.add(Translations.load(dirname, locale, domain)) self._current.translations = t self._activate_failed = False
def activate(self, locale, env_path=None): try: locale_dir = pkg_resources.resource_filename("trac", "locale") except Exception: self._activate_failed = True return t = Translations.load(locale_dir, locale or "en_US") if not isinstance(t, Translations): t = self._null_translations else: self._add(t, Translations.load(locale_dir, locale or "en_US", "tracini")) if env_path: with self._plugin_domains_lock: domains = self._plugin_domains.get(env_path, {}) domains = domains.items() for domain, dirname in domains: self._add(t, Translations.load(dirname, locale, domain)) self._current.translations = t self._activate_failed = False
def render_template(path, locale='en_us', save=False, path_build=None, path_static=None, path_templates=None, path_locale=None): env = os.environ os.environ['PATH_BUILD'] = path_build = path_build or env.get( 'PATH_BUILD', './build') os.environ['PATH_STATIC'] = path_static = path_static or env.get( 'PATH_STATIC', './static') os.environ['PATH_TEMPLATES'] = path_templates = path_templates or env.get( 'PATH_TEMPLATES', './templates') path_locale = path_locale or os.environ.get('PATH_LOCALE', './i18n') # grab the jinja2 environment to work with jinja2_env = _get_jinja2_env(path_templates) # add the ext to the jinja environment if save: jinja2_env.filters['url'] = ext_url jinja2_env.filters['static'] = functools.partial(ext_url, static=True) else: jinja2_env.filters['url'] = rel_url jinja2_env.filters['static'] = functools.partial(rel_url, static=True) params = { 'path': path, } os.environ['LOCALE'] = locale translations = Translations.load(path_locale, [locale]) jinja2_env.install_gettext_translations(translations, newstyle=True) lang = locale.split('_')[0] params['lang'] = lang # Render the template template = jinja2_env.get_template(path) content = template.render(**params) # FIXME: WHY NOT USING BRANCH? # Save the rendered page to disk dest_file = os.path.join(path_build, lang, path) dest_path = os.path.dirname(dest_file) # create the directory struct where the file will live if not os.path.exists(dest_path): os.makedirs(dest_path) # dump the rendered file dumps(content, dest_file) assert os.path.exists(dest_file) return content
def gettext_for(locale='en'): """ Returns the `ugettext` function for a specific locale Usage:: _ = gettext_for('ja') # Load the 'ja' translation library """ return Translations.load( os.path.join(BASEDIR, 'app', 'translations'), [locale] ).ugettext
def load_translations(*locales: str, domain=None): """Loads all translation of locales, fallback translation is first locale""" loc_ins = None for loc in reversed(locales): try: loc_ins = Locale.parse(loc) except: continue _translations_dict[loc_ins] = Translations.load( path_locale, loc_ins, domain) _translations_dict[_fallback_ns] = _translations_dict[loc_ins]
def activate(self, locale, env_path=None): try: locale_dir = pkg_resources.resource_filename('trac', 'locale') except Exception: self._activate_failed = True return t = Translations.load(locale_dir, locale or 'en_US') if not t or t.__class__ is NullTranslations: t = self._null_translations else: t.add(Translations.load(locale_dir, locale or 'en_US', 'tracini')) if env_path: with self._plugin_domains_lock: domains = self._plugin_domains.get(env_path, {}) domains = domains.items() for domain, dirname in domains: t.add(Translations.load(dirname, locale, domain)) self._current.translations = t self._activate_failed = False
def pre_process_request(self, req, handler): locale = getattr(req, 'locale', None) if locale: global translations try: from babel.support import Translations from pkg_resources import resource_filename except ImportError: return handler translations = Translations.load(resource_filename(__name__, 'locale'), locale) return handler
def render(self, template_name, desired_lang): """Returns the rendered template with the desired language.""" if not desired_lang: desired_lang = self.current_locale desired_locales_list = [desired_lang] #print("Your desired language is %s" % desired_lang) translations = Translations.load(self.locale_dir, desired_locales_list) self.env.install_gettext_translations(translations) template = self.env.get_template(template_name) return template.render().encode('utf-8') # magic here & avoid error UnicodeEncodeError
def load_gettext_translations(directory, domain): """Loads translations from gettext's locale tree Locale tree is similar to system's /usr/share/locale, like: {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 2. Merge against existing POT file: msgmerge old.po cyclone.po > new.po 3. Compile: msgfmt cyclone.po -o {directory}/pt_BR/LC_MESSAGES/cyclone.mo """ global _translations global _supported_locales global _use_gettext _translations = {} for lang in os.listdir(directory): if lang.startswith('.'): continue # skip .svn, etc if os.path.isfile(os.path.join(directory, lang)): continue try: translation = _translations.get(lang, Translations.load()) # Load NullTranslations if isinstance(translation, gettext.NullTranslations): _translations[lang] = Translations.load( directory, [lang], domain ) else: _translations[lang].merge( Translations.load(directory, [lang], domain) ) except Exception, e: logging.error("Cannot load translation for '%s': %s", lang, str(e)) continue
def load(cls): settings = QtCore.QSettings() locale = settings.value("locale") if not locale: settings.setValue("locale", "en") locale = settings.value("locale") log.debug("Setting locale to: %s", locale) translations = BaseTranslations.load( get_resource("qtct", "i18n"), [locale, "en"], domain='qtct' ) return translations
def create_environment(theme_path, language): loader = FileSystemLoader(searchpath=theme_path, encoding='utf-8') environment = Environment(loader=loader, extensions=['jinja2.ext.i18n']) translations = Translations.load(paths.get_locale_path(), language) environment.install_gettext_translations(translations) # global functions available in the templates environment.globals.update(get_language_name=locale.get_language_name) return environment
def translate_templates(env, loader, settings, verbose=False, debug=False): """ Translate all templates available through `loader'. Returns a big dict with all the translated templates, in all languages : {'login.jinja2': {'en': string, 'sv': string, ...}, 'error.jinja2': ... } :param env: jinja2.Environment() :param loader: jinja2.BaseLoader() :param settings: dict with settings and variables available to the Jinja2 templates :param verbose: boolean, output to stdout or not :param debug: boolean, output debug information to stderr or not :return: dict with translated templates """ languages = {} res = {} locale_dir = pkg_resources.resource_filename(__name__, 'locale') for lang in pkg_resources.resource_listdir(__name__, 'locale'): lang_dir = os.path.join(locale_dir, lang) if not os.path.isdir(lang_dir): if debug: sys.stderr.write("Not a directory: {!r}\n".format(lang_dir)) continue if verbose: languages[lang] = 1 translations = Translations.load(locale_dir, [lang], settings['gettext_domain']) env.install_gettext_translations(translations) for template_file in loader.list_templates(): if template_file.endswith('.swp'): continue template = env.get_template(template_file) translated = template.render(settings=settings) if not template_file in res: res[template_file] = {} res[template_file][lang] = translated.encode('utf-8') if debug: sys.stderr.write("Lang={!s} :\n{!s}\n\n".format(lang, translated.encode('utf-8'))) if verbose: print("\nLanguages : {!r}\nGenerated templates : {!r}\n".format( sorted(languages.keys()), sorted(res.keys()))) return res
def _add_extra_translations(dirname, locales, domain): translator = Translations.load(dirname=dirname, locales=locales, domain=domain) try: pylons.translator.merge(translator) except AttributeError: # this occurs when an extension has 'en' translations that # replace the default strings. As set_lang has not been run, # pylons.translation is the NullTranslation, so we have to # replace the StackedObjectProxy ourselves manually. environ = pylons.request.environ environ["pylons.pylons"].translator = translator if "paste.registry" in environ: environ["paste.registry"].replace(pylons.translator, translator)
def get_translations(locale=None): """Get the translation for a locale.""" if not locale: locale = local('locale') translations = _translations.get(str(locale)) if translations is not None: return translations application = local('application') rv = Translations.load(os.path.join(application.HERE_PATH, application.cache.appname, "i18n"), [locale]) _translations[str(locale)] = rv return rv
def test_empty_translation_with_fallback(self): catalog1 = Catalog(locale='fr_FR') catalog1.add(u'', '''\ "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n''') catalog1.add(u'Fuzz', '') buf1 = BytesIO() mofile.write_mo(buf1, catalog1) buf1.seek(0) catalog2 = Catalog(locale='fr') catalog2.add(u'', '''\ "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n''') catalog2.add(u'Fuzz', 'Flou') buf2 = BytesIO() mofile.write_mo(buf2, catalog2) buf2.seek(0) translations = Translations(fp=buf1) translations.add_fallback(Translations(fp=buf2)) self.assertEqual(u'Flou', translations.ugettext('Fuzz'))
def _load_domain(self, domain, fallback=True): """Load the given domain from one of the pre-configured locale dirs. Returns a :class:`gettext.NullTranslations` instance if no translations could be found for a non-critical domain. This allows untranslated plugins to be rendered in English instead of an error being raised. :param domain: A domain name. :param fallback: An optional flag that, when True, returns a :class:`gettext.NullTranslations` instance if no translations file could be found for the given language(s). :rtype: :class:`gettext.GNUTranslations` :returns: The native python translator instance for this domain. :raises DomainError: If no locale dir has been configured for this domain and the fallback is off. :raises LanguageError: If no translations could be found for this domain in this locale and the fallback is off. """ locale_dirs = self._locale_dirs.get(domain, None) if locale_dirs: if isinstance(locale_dirs, basestring): locale_dirs = (locale_dirs, ) translation_list = self._load_translations(domain, locale_dirs, fallback) if (not fallback) and len(translation_list) == 0: msg = 'No %r translations found for %r in %r.' raise LanguageError(msg % (domain, self._languages, locale_dirs)) translations = Translations(domain=domain) for translation in translation_list: translations.merge(translation) elif fallback: translations = NullTranslations() else: raise DomainError('No localedir specified for domain %r' % domain) self._domains[domain] = translations return translations