Example #1
0
def translations(ln):
    """Exposes translations in JSON format.

    :param ln: language ISO 639-1 Code (two chars).
    """
    domain = get_domain()
    paths = domain.paths
    try:
        path = next(p for p in paths if p.find('rero_ils') > -1)
    except StopIteration:
        current_app.logger.error(f'translations for {ln} does not exist')
        abort(404)

    po_file_name = f'{path}/{ln}/LC_MESSAGES/{domain.domain}.po'
    if not os.path.isfile(po_file_name):
        abort(404)
    data = {}
    try:
        po = polib.pofile(po_file_name)
    except Exception:
        current_app.logger.error(f'unable to open po file: {po_file_name}')
        abort(404)
    for entry in po:
        data[entry.msgid] = entry.msgstr or entry.msgid
    return jsonify(data)
Example #2
0
def get_translations(lang):
    """Exposes translations in JSON format.

    :param lang: language ISO 639-1 Code (two chars).
    """
    domain = get_domain()

    path = next(p for p in domain.paths if p.find('sonar/translations') > -1)

    po_file_name = f'{path}/{lang}/LC_MESSAGES/{domain.domain}.po'

    if not os.path.isfile(po_file_name):
        abort(404)

    data = {}

    try:
        translations = polib.pofile(po_file_name)
    except Exception:
        current_app.logger.error(
            'unable to open po file: {po}'.format(po=po_file_name))
        abort(404)

    for entry in translations:
        data[entry.msgid] = entry.msgstr or entry.msgid

    return jsonify(data)
Example #3
0
def get_translation_domain(plugin_name=_use_context):
    """Get the translation domain for the given plugin

    If `plugin_name` is omitted, the plugin will be taken from current_plugin.
    If `plugin_name` is None, the core translation domain ('indico') will be used.
    """
    if plugin_name is None:
        return get_domain()
    else:
        plugin = None
        if has_app_context():
            from indico.core.plugins import plugin_engine
            plugin = plugin_engine.get_plugin(plugin_name) if plugin_name is not _use_context else current_plugin
        if plugin:
            return plugin.translation_domain
        else:
            return get_domain()
Example #4
0
def mock_translations(monkeypatch, request_context):
    domain = get_domain()
    locales = {
        'fr_FR': ('French', 'France'),
        'en_GB': ('English', 'United Kingdom')
    }
    monkeypatch.setattr('indico.util.i18n.get_all_locales', lambda: locales)
    monkeypatch.setattr(domain, 'get_translations', MockTranslations)
Example #5
0
def get_translation_domain(plugin_name=_use_context):
    """Get the translation domain for the given plugin

    If `plugin_name` is omitted, the plugin will be taken from current_plugin.
    If `plugin_name` is None, the core translation domain ('indico') will be used.
    """
    if plugin_name is None:
        return get_domain()
    else:
        plugin = None
        if has_app_context():
            from indico.core.plugins import plugin_engine
            plugin = plugin_engine.get_plugin(plugin_name) if plugin_name is not _use_context else current_plugin
        if plugin:
            return plugin.translation_domain
        else:
            return get_domain()
Example #6
0
def mock_translations(monkeypatch, request_context):
    domain = get_domain()
    locales = {'fr_FR': 'French',
               'en_GB': 'English'}
    monkeypatch.setattr('indico.util.i18n.get_all_locales', lambda: locales)
    monkeypatch.setattr(domain, 'get_translations', MockTranslations)
def mock_translations(monkeypatch, request_context):
    domain = get_domain()
    locales = {'fr_FR': 'French', 'en_GB': 'English'}
    monkeypatch.setattr('fossir.util.i18n.get_all_locales', lambda: locales)
    monkeypatch.setattr(domain, 'get_translations', MockTranslations)
Example #8
0
 def merge_translations():
     from flask_babelex import get_domain
     get_domain().get_translations().merge(
         flask_user_domain.get_translations())