コード例 #1
0
def test_set_locale(app):
    """Test get translations for language with existing/missing *.mo files."""
    Babel(app)
    with app.test_request_context():
        with set_locale("en"):
            assert str(get_locale()) == "en"
        with set_locale("da"):
            assert str(get_locale()) == "da"
コード例 #2
0
def test_get_translations_existing_and_missing_mo(app):
    """Test get translations for language with existing/missing *.mo files."""
    app.config['I18N_LANGUAGES'] = [('de', 'German')]

    d = MultidirDomain(entrypoint='invenio_i18n.translations')

    with app.test_request_context():
        with set_locale('en'):
            assert isinstance(d.get_translations(), Translations)
        with set_locale('de'):
            assert isinstance(d.get_translations(), NullTranslations)
コード例 #3
0
def test_get_translations_existing_and_missing_mo(app):
    """Test get translations for language with existing/missing *.mo files."""
    app.config['I18N_LANGUAGES'] = [('de', 'German')]

    d = MultidirDomain(entrypoint='invenio_i18n.translations')

    with app.test_request_context():
        with set_locale('en'):
            assert isinstance(d.get_translations(), Translations)
        with set_locale('de'):
            assert isinstance(d.get_translations(), NullTranslations)
コード例 #4
0
def test_get_translations_existing_and_missing_mo(app):
    """Test get translations for language with existing/missing *.mo files."""
    app.config["I18N_LANGUAGES"] = [("de", "German")]
    Babel(app)

    d = MultidirDomain(entry_point_group="invenio_i18n.translations")

    with app.test_request_context():
        with set_locale("en"):
            assert isinstance(d.get_translations(), Translations)
        with set_locale("de"):
            assert isinstance(d.get_translations(), NullTranslations)
コード例 #5
0
def test_set_locale(app):
    """Test get translations for language with existing/missing *.mo files."""
    # Wokring outside request context
    try:
        with set_locale('en'):
            assert False
    except RuntimeError:
        pass

    with app.test_request_context():
        with set_locale('en'):
            assert get_locale() == 'en'
        with set_locale('da'):
            assert get_locale() == 'da'
コード例 #6
0
def test_set_locale(app):
    """Test get translations for language with existing/missing *.mo files."""
    # Wokring outside request context
    try:
        with set_locale('en'):
            assert False
    except RuntimeError:
        pass

    with app.test_request_context():
        with set_locale('en'):
            assert get_locale() == 'en'
        with set_locale('da'):
            assert get_locale() == 'da'
コード例 #7
0
def test_jinja_templates(app):
    """Test template rendering."""
    InvenioI18N(app)

    assert app.jinja_env.filters["datetimeformat"]
    assert app.jinja_env.filters["toutc"]
    assert app.jinja_env.filters["tousertimezone"]

    dt = datetime(1987, 3, 5, 17, 12)
    dt_tz = datetime(1987, 3, 5, 17, 12, tzinfo=timezone("CET"))

    with app.test_request_context():
        assert render_template_string("{{dt|datetimeformat}}", dt=dt) == "Mar 5, 1987, 5:12:00 PM"
        assert render_template_string("{{dt|toutc}}", dt=dt_tz) == "1987-03-05 16:12:00"
        assert render_template_string("{{dt|tousertimezone}}", dt=dt_tz) == "1987-03-05 16:12:00+00:00"
        assert render_template_string("{{_('Translate')}}") == "Translate"

        tpl = r"{% trans %}Block translate{{var}}{% endtrans %}"
        assert render_template_string(tpl, var="!") == "Block translate!"

        assert render_template_string("{{'en'|language_name}}") == "English"
        assert render_template_string("{{'da'|language_name}}") == "Danish"
        assert render_template_string("{{'en'|language_name_local}}") == "English"
        assert render_template_string("{{'da'|language_name_local}}") == "dansk"

        with set_locale("da"):
            assert render_template_string("{{'en'|language_name}}") == "engelsk"
            assert render_template_string("{{'da'|language_name}}") == "dansk"
            assert render_template_string("{{'en'|language_name_local}}") == "English"
            assert render_template_string("{{'da'|language_name_local}}") == "dansk"
コード例 #8
0
def test_set_locale_no_app_ctx():
    """Test get translations when there is no application ctx."""
    # Wokring outside request context
    try:
        with set_locale("en"):
            assert False
    except RuntimeError:
        pass
コード例 #9
0
def test_merge_translations(app):
    """Test initialization."""
    d = MultidirDomain(paths=[join(dirname(__file__), 'translations')],
                       entrypoint='invenio_i18n.translations')

    with app.test_request_context():
        with set_locale('en'):
            t = d.get_translations()
            assert isinstance(t, Translations)
            # Only in tests/translations
            assert t.gettext('Test string') == 'Test string from test'
            # Only in invenio_i18n/translations
            assert t.gettext('Block translate %s') % 'a' \
                == 'Block translate a'
            # In both - tests/translations overwrites invenio_i18n/translations
            assert t.gettext('Translate') == 'From test catalog'
コード例 #10
0
def test_merge_translations(app):
    """Test initialization."""
    d = MultidirDomain(
        paths=[join(dirname(__file__), 'translations')],
        entrypoint='invenio_i18n.translations')

    with app.test_request_context():
        with set_locale('en'):
            t = d.get_translations()
            assert isinstance(t, Translations)
            # Only in tests/translations
            assert t.gettext('Test string') == 'Test string from test'
            # Only in invenio_i18n/translations
            assert t.gettext('Block translate %s') % 'a' \
                == 'Block translate a'
            # In both - tests/translations overwrites invenio_i18n/translations
            assert t.gettext('Translate') == 'From test catalog'
コード例 #11
0
def test_merge_translations(app):
    """Test initialization."""
    Babel(app)

    d = MultidirDomain(
        paths=[join(dirname(__file__), "translations")],
        entry_point_group="invenio_i18n.translations",
    )

    with app.test_request_context():
        with set_locale("en"):
            t = d.get_translations()
            assert isinstance(t, Translations)
            # Only in tests/translations
            assert t.gettext("Test string") == "Test string from test"
            # Only in invenio_i18n/translations
            assert t.gettext("Block translate %s") % "a" == "Block translate a"
            # In both - tests/translations overwrites invenio_i18n/translations
            assert t.gettext("Translate") == "From test catalog"
コード例 #12
0
def test_jinja_templates(app):
    """Test template rendering."""
    InvenioI18N(app)

    assert app.jinja_env.filters['datetimeformat']
    assert app.jinja_env.filters['toutc']
    assert app.jinja_env.filters['tousertimezone']

    dt = datetime(1987, 3, 5, 17, 12)
    dt_tz = datetime(1987, 3, 5, 17, 12, tzinfo=timezone('CET'))

    with app.test_request_context():
        assert render_template_string(
            '{{dt|datetimeformat}}', dt=dt) == \
            'Mar 5, 1987, 5:12:00 PM'
        assert render_template_string(
            '{{dt|toutc}}', dt=dt_tz) == \
            '1987-03-05 16:12:00'
        assert render_template_string(
            '{{dt|tousertimezone}}', dt=dt_tz) == \
            '1987-03-05 16:12:00+00:00'
        assert render_template_string('{{_("Translate")}}') == 'Translate'

        tpl = r'{% trans %}Block translate{{var}}{% endtrans %}'
        assert render_template_string(tpl, var='!') == 'Block translate!'

        assert render_template_string('{{"en"|language_name}}') == 'English'
        assert render_template_string('{{"da"|language_name}}') == 'Danish'
        assert render_template_string('{{"en"|language_name_local}}') \
            == 'English'
        assert render_template_string('{{"da"|language_name_local}}') \
            == 'dansk'

        with set_locale('da'):
            assert render_template_string('{{"en"|language_name}}') \
                == 'engelsk'
            assert render_template_string('{{"da"|language_name}}') \
                == 'dansk'
            assert render_template_string('{{"en"|language_name_local}}') \
                == 'English'
            assert render_template_string('{{"da"|language_name_local}}') \
                == 'dansk'
コード例 #13
0
def test_jinja_templates(app):
    """Test template rendering."""
    InvenioI18N(app)

    assert app.jinja_env.filters['datetimeformat']
    assert app.jinja_env.filters['toutc']
    assert app.jinja_env.filters['tousertimezone']

    dt = datetime(1987, 3, 5, 17, 12)
    dt_tz = datetime(1987, 3, 5, 17, 12, tzinfo=timezone('CET'))

    with app.test_request_context():
        assert render_template_string(
            '{{dt|datetimeformat}}', dt=dt) == \
            'Mar 5, 1987, 5:12:00 PM'
        assert render_template_string(
            '{{dt|toutc}}', dt=dt_tz) == \
            '1987-03-05 16:12:00'
        assert render_template_string(
            '{{dt|tousertimezone}}', dt=dt_tz) == \
            '1987-03-05 16:12:00+00:00'
        assert render_template_string('{{_("Translate")}}') == 'Translate'

        tpl = r'{% trans %}Block translate{{var}}{% endtrans %}'
        assert render_template_string(tpl, var='!') == 'Block translate!'

        assert render_template_string('{{"en"|language_name}}') == 'English'
        assert render_template_string('{{"da"|language_name}}') == 'Danish'
        assert render_template_string('{{"en"|language_name_local}}') \
            == 'English'
        assert render_template_string('{{"da"|language_name_local}}') \
            == 'dansk'

        with set_locale('da'):
            assert render_template_string('{{"en"|language_name}}') \
                == 'engelsk'
            assert render_template_string('{{"da"|language_name}}') \
                == 'dansk'
            assert render_template_string('{{"en"|language_name_local}}') \
                == 'English'
            assert render_template_string('{{"da"|language_name_local}}') \
                == 'dansk'
コード例 #14
0
def test_jinja_templates(app):
    """Test template rendering."""
    InvenioI18N(app)

    assert app.jinja_env.filters["datetimeformat"]
    assert app.jinja_env.filters["toutc"]
    assert app.jinja_env.filters["tousertimezone"]

    dt = datetime(1987, 3, 5, 17, 12)
    dt_tz = datetime(1987, 3, 5, 17, 12, tzinfo=timezone("CET"))

    with app.test_request_context():
        assert (render_template_string("{{dt|datetimeformat}}",
                                       dt=dt) == "Mar 5, 1987, 5:12:00 PM")
        assert render_template_string("{{dt|toutc}}",
                                      dt=dt_tz) == "1987-03-05 16:12:00"
        assert (render_template_string(
            "{{dt|tousertimezone}}", dt=dt_tz) == "1987-03-05 16:12:00+00:00")
        assert render_template_string('{{_("Translate")}}') == "Translate"

        tpl = r"{% trans %}Block translate{{var}}{% endtrans %}"
        assert render_template_string(tpl, var="!") == "Block translate!"

        assert render_template_string('{{"en"|language_name}}') == "English"
        assert render_template_string('{{"da"|language_name}}') == "Danish"
        assert render_template_string(
            '{{"en"|language_name_local}}') == "English"
        assert render_template_string(
            '{{"da"|language_name_local}}') == "dansk"

        with set_locale("da"):
            assert render_template_string(
                '{{"en"|language_name}}') == "engelsk"
            assert render_template_string('{{"da"|language_name}}') == "dansk"
            assert render_template_string(
                '{{"en"|language_name_local}}') == "English"
            assert render_template_string(
                '{{"da"|language_name_local}}') == "dansk"