コード例 #1
0
def test_xclim_translations(locale, official_indicators):
    loc, dic = xloc.get_local_dict(locale)
    assert "attrs_mapping" in dic
    assert "modifiers" in dic["attrs_mapping"]
    for translatable, translations in dic["attrs_mapping"].items():
        if translatable != "modifiers":
            assert isinstance(translations, list)
            assert len(translations) <= len(dic["attrs_mapping"]["modifiers"])
    assert set(dic["attrs_mapping"].keys()).symmetric_difference(
        default_formatter.mapping.keys()) == {"modifiers"}

    translated_inds = []
    registry = official_indicators.copy()
    for indicator, fields in dic.items():
        if indicator != "attrs_mapping":
            # Checking that the translated indicator does exist
            # For translations of children of MultiIndicators, only check that the indicator exists
            if "." in indicator:
                indicator = indicator.split(".")[0]
                assert indicator in registry or indicator in translated_inds
            else:
                assert registry.pop(indicator)
                translated_inds.append(indicator)
            # Only translatable attributes are translated
            assert set(fields.keys()).issubset(xloc.TRANSLATABLE_ATTRS)

    # Remove virtual modules' indicators, those have a dotted name.
    untranslated = [k for k in registry.keys() if "." not in k]

    if len(untranslated) > 0:
        pytest.fail(
            f"Indicators {', '.join(untranslated)} do not have translations for official locale {locale}."
        )
コード例 #2
0
def test_xclim_translations(locale, official_indicators):
    loc, dic = xloc.get_local_dict(locale)
    assert "attrs_mapping" in dic
    assert "modifiers" in dic["attrs_mapping"]
    for translatable, translations in dic["attrs_mapping"].items():
        if translatable != "modifiers":
            assert isinstance(translations, list)
            assert len(translations) <= len(dic["attrs_mapping"]["modifiers"])
    assert set(dic["attrs_mapping"].keys()).symmetric_difference(
        default_formatter.mapping.keys()) == {"modifiers"}

    untranslated = []
    incomplete = []
    for indname, indcls in official_indicators.items():
        is_complete = True
        trans = indcls.translate_attrs(locale)
        if trans == {"cf_attrs": []}:
            untranslated.append(indname)
            continue
        # Both global attrs are present
        is_complete = not ({"title", "abstract"} - set(trans.keys()))
        for attrs, transattrs in zip(indcls.cf_attrs, trans["cf_attrs"]):
            if {"long_name", "description"} - set(transattrs.keys()):
                is_complete = False

        if not is_complete:
            incomplete.append(indname)

    if len(untranslated) > 0 or len(incomplete) > 0:
        pytest.fail(
            f"Indicators {', '.join(untranslated)} do not have translations and {', '.join(incomplete)} have incomplete ones for official locale {locale}."
        )
コード例 #3
0
def test_xclim_translations(locale):
    registry_cp = registry.copy()
    loc, dic = xloc.get_local_dict(locale)
    assert "attrs_mapping" in dic
    assert "modifiers" in dic["attrs_mapping"]
    for translatable, translations in dic["attrs_mapping"].items():
        if translatable != "modifiers":
            assert isinstance(translations, list)
            assert len(translations) <= len(dic["attrs_mapping"]["modifiers"])
    assert set(dic["attrs_mapping"].keys()).symmetric_difference(
        default_formatter.mapping.keys()) == {"modifiers"}

    translated_inds = []
    # Remove unofficial indicators (as those created during the tests)
    for identifier, cls in registry.items():
        if not cls.__module__.startswith("xclim"):
            registry_cp.pop(identifier)

    for indicator, fields in dic.items():
        if indicator != "attrs_mapping":
            # Checking that the translated indicator does exist
            # For translations of children of MultiIndicators, only check that the indicator exists
            if "." in indicator:
                indicator = indicator.split(".")[0]
                assert indicator in registry_cp or indicator in translated_inds
            else:
                assert registry_cp.pop(indicator)
                translated_inds.append(indicator)
            # Only translatable attributes are translated
            assert set(fields.keys()).issubset(xloc.TRANSLATABLE_ATTRS)

    if bool(registry_cp):
        pytest.fail(
            f"Indicators {','.join(registry_cp.keys())} do not have translations for official locale {locale}."
        )
コード例 #4
0
ファイル: test_locales.py プロジェクト: cehbrecht/xclim
def test_local_dict(tmp_path):
    loc, dic = xloc.get_local_dict("fr")
    assert loc == "fr"
    assert (
        dic["TG_MEAN"]["long_name"] == "Moyenne de la température journalière moyenne"
    )

    loc, dic = xloc.get_local_dict(esperanto)
    assert loc == "eo"
    assert dic["TG_MEAN"]["long_name"] == "Meza ciutaga averaga temperaturo"

    with (tmp_path / "ru.json").open("w", encoding="utf-8") as f:
        json.dump(russian[1], f, ensure_ascii=False)

    loc, dic = xloc.get_local_dict(("ru", tmp_path / "ru.json"))
    assert loc == "ru"
    assert dic["TG_MEAN"]["long_name"] == "Среднее значение среднесуточной температуры."

    with pytest.raises(xloc.UnavailableLocaleError):
        xloc.get_local_dict("tlh")
コード例 #5
0
def generate_local_dict(locale: str, init_english: bool = False):
    """Generate a dictionary with keys for each indicators and translatable attributes.

    Parameters
    ----------
    locale : str
        Locale in the IETF format
    init_english : bool
        If True, fills the initial dictionary with the english versions of the attributes.
        Defaults to False.
    """
    from xclim.core.indicator import registry

    best_locale = get_best_locale(locale)
    if best_locale is not None:
        locname, attrs = get_local_dict(best_locale)
        for ind_name in attrs.copy().keys():
            if ind_name != "attrs_mapping" and ind_name not in registry:
                attrs.pop(ind_name)
    else:
        attrs = {}

    attrs_mapping = attrs.setdefault("attrs_mapping", {})
    attrs_mapping.setdefault("modifiers", [""])
    for key, value in default_formatter.mapping.items():
        attrs_mapping.setdefault(key, [value[0]])

    eng_attr = ""
    for ind_name, indicator in registry.items():
        ind_attrs = attrs.setdefault(ind_name, {})
        for translatable_attr in set(TRANSLATABLE_ATTRS).difference(
                set(indicator._cf_names)):
            if init_english:
                eng_attr = getattr(indicator, translatable_attr)
                if not isinstance(eng_attr, str):
                    eng_attr = ""
            ind_attrs.setdefault(f"{translatable_attr}", eng_attr)

        for var_attrs in indicator.cf_attrs:
            # In the case of single output, put var attrs in main dict
            if len(indicator.cf_attrs) > 1:
                ind_attrs = attrs.setdefault(
                    f"{ind_name}.{var_attrs['var_name']}", {})

            for translatable_attr in set(TRANSLATABLE_ATTRS).intersection(
                    set(indicator._cf_names)):
                if init_english:
                    eng_attr = var_attrs.get(translatable_attr)
                    print(translatable_attr, eng_attr)
                    if not isinstance(eng_attr, str):
                        eng_attr = ""
                ind_attrs.setdefault(f"{translatable_attr}", eng_attr)
    return attrs