예제 #1
0
파일: i18n.py 프로젝트: odtvince/udata
    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
예제 #2
0
    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
예제 #3
0
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
예제 #4
0
 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
예제 #5
0
파일: i18n.py 프로젝트: rossjones/udata
    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
예제 #6
0
파일: i18n.py 프로젝트: seiteta/udata
    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
예제 #7
0
파일: __init__.py 프로젝트: etalab/weckan
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
예제 #8
0
 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()
예제 #9
0
파일: lib.py 프로젝트: kejbaly2/comdev
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
예제 #10
0
    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")
예제 #11
0
    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()
예제 #12
0
 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
예제 #13
0
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))
예제 #14
0
    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)
예제 #15
0
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))
예제 #16
0
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__)
예제 #17
0
파일: i18ntool.py 프로젝트: sim0nx/mematool
    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
예제 #18
0
    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')
예제 #19
0
파일: babel.py 프로젝트: N03/invenio
    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()
예제 #20
0
파일: webapp.py 프로젝트: yomguy/searx
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
예제 #21
0
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
예제 #22
0
 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
예제 #23
0
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'])
예제 #24
0
파일: utils.py 프로젝트: garyyeap/zoe-robot
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])
예제 #25
0
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])
예제 #26
0
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
예제 #27
0
 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 ) )
예제 #28
0
 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))
예제 #29
0
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
예제 #30
0
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)
예제 #31
0
 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
예제 #32
0
 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 ) )
예제 #33
0
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
예제 #34
0
 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
예제 #35
0
 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
예제 #36
0
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
예제 #37
0
 def wrapper(request):
     env.install_gettext_translations(NullTranslations())
     langs = request.headers['Accept-Language']
     pt_index = langs.find('pt')
     en_index = langs.find('en')
     if pt_index > en_index or pt_index == en_index == -1:
         env.install_gettext_translations(
             Translations.load('translations/',
                               locales=('en', ),
                               domain='messages'))
     return view(request, env)
예제 #38
0
 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
예제 #39
0
    def __init__(self, invoice):
        self.invoice = invoice
        self.invoice_datetime = ConfigStore.parse_date(
            self.invoice.invoice_date)
        self.config = ConfigStore.get_configuration(when=self.invoice_datetime)

        self.translation = Translations.load('locale',
                                             locales=[self.invoice.language],
                                             domain='pyinvoice')
        self.ugettext = self.translation.ugettext
        self.format = Format(locale=self.invoice.language)
예제 #40
0
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
예제 #41
0
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]
예제 #42
0
 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
예제 #43
0
    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
예제 #44
0
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
예제 #45
0
def send_notifications_on_new_post(post, lang):
    from ckan.model import User
    template_dir = os.path.join(os.path.dirname(__file__), 'templates')
    locale_dir = os.path.join(os.path.dirname(__file__), 'i18n')
    env = jinja2.Environment(loader=jinja2.FileSystemLoader(template_dir),
                             extensions=['jinja2.ext.i18n'])
    translations = Translations.load(locale_dir, [lang],
                                     domain='ckanext-forum')
    env.install_gettext_translations(translations)
    env.globals['get_locale'] = lambda: lang
    post_author = User.get(post.author_id)

    thread = Thread.get_by_id(post.thread_id)
    author_ids = set([p.author_id
                      for p in thread.forum_posts] + [thread.author_id])
    author_ids -= set([
        u.user_id for u in Unsubscription.filter_by_thread_id(post.thread_id)
    ])

    for author_id in author_ids:
        if author_id == post_author.id:
            continue
        user = User.get(author_id)
        unsubscribe_url = tk.url_for('forum_unsubscribe',
                                     base64_name=base64.b64encode(user.name),
                                     thread_id=thread.id)
        context = {
            'user_name':
            user.name,
            'site_title':
            tk.config.get('ckan.site_title'),
            'site_url':
            tk.config.get('ckan.site_url'),
            'post_content':
            post.content,
            'title':
            env.globals['gettext']('New post'),
            'unsubscribe_url':
            urljoin(tk.config['ckan.site_url'], unsubscribe_url),
            'username':
            post_author.name,
            'thread_url':
            urljoin(tk.config['ckan.site_url'], thread.get_absolute_url()),
        }
        template = env.get_template('forum_new_post_mail.html')
        body = template.render(context)
        log.debug('Email body %s', body)
        tk.get_action('send_mail')(
            {}, {
                'to': user.email,
                'subject': env.globals['gettext']('New post'),
                'message_html': body
            })
예제 #46
0
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
예제 #47
0
 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
예제 #48
0
    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
예제 #49
0
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
예제 #50
0
파일: server_run.py 프로젝트: agdsn/pycroft
 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
예제 #51
0
파일: i18n.py 프로젝트: eawag-rdm/ckan
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)
예제 #52
0
파일: __init__.py 프로젝트: deprecate/rdrei
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
예제 #53
0
파일: bottle.py 프로젝트: Gagaro/PimpMyBot
    def prepare(self, *args, **kwargs):
        from babel.support import Translations
        from utils.translations import TRANSLATIONS_DIR
        from utils.config import Configuration

        translation = Translations.load(
            TRANSLATIONS_DIR,
            locales=Configuration.get().lang,
            domain='pimpmybot'
        )
        kwargs['filters'] = {
            'datetimeformat': datetimeformat
        }
        super(PmbJinja2Template, self).prepare(*args, **kwargs)
        self.env.install_gettext_translations(translation)
예제 #54
0
파일: webapp.py 프로젝트: AAFC-MBB/galaxy-1
 def setup_i18n( self ):
     locales = []
     if 'HTTP_ACCEPT_LANGUAGE' in self.environ:
         # locales looks something like: ['en', 'en-us;q=0.7', 'ja;q=0.3']
         client_locales = self.environ['HTTP_ACCEPT_LANGUAGE'].split( ',' )
         for locale in client_locales:
             try:
                 locales.append( Locale.parse( locale.split( ';' )[0].strip(), sep='-' ).language )
             except Exception as e:
                 log.debug( "Error parsing locale '%s'. %s: %s", locale, type( e ), e )
     if not locales:
         # 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 ) )
예제 #55
0
def set_translations(locale):
    """Sets the locale and translations object for the current request. Most
    functions in this module depends on the translations object being set to
    work properly.

    :param locale:
        The locale code. For example, ``en_US`` or ``pt_BR``.
    :return:
        ``None``.
    """
    if locale not in _translations:
        options = list(set([locale, get_config(__name__, 'locale')]))
        _translations[locale] = Translations.load('locale', options, 'messages')

    local.locale = locale
    local.translations = _translations[locale]
예제 #56
0
파일: __init__.py 프로젝트: ktmud/david
def get_translations(locale, domain='messages'):
    locale = str(locale)
    # to unify locale names
    if locale.startswith('zh_hans'):
        locale = 'zh_CN'
    elif locale.startswith('zh_hant'):
        locale = 'zh_TW'

    ident = locale + '/' + domain

    if ident in cached_translations:
        return cached_translations[ident]

    dirname, domain = AVAILABLE_DOMAINS[domain]
    translations = Translations.load(dirname, locale, domain=domain)
    cached_translations[ident] = translations
    return translations