collective / pytest-plone

Pytest plugin to test Plone addons
GNU General Public License v2.0
3 stars 0 forks source link

Create fixture to generate .mo files #5

Closed ericof closed 9 months ago

ericof commented 9 months ago

Example usage:

@pytest.fixture(scope="session", autouse=True)
def generate_mo():
    """Generate .mo files."""
    import collective.blog

    locales_path = Path(collective.blog.__file__).parent / "locales"
    po_files: Generator = locales_path.glob("**/*.po")
    for po_file in po_files:
        parent: Path = po_file.parent
        domain: str = po_file.name[: -len(po_file.suffix)]
        mo_file: Path = parent / f"{domain}.mo"
        try:
            mo = Msgfmt(f"{po_file}", name=domain).getAsFile()
        except PoSyntaxError:
            continue
        else:
            with open(mo_file, "wb") as f_out:
                f_out.write(mo.read())
davisagli commented 9 months ago

I think you just need to set the zope_i18n_compile_mo_files environment variable to true, then zope.i18n will take care of it

ericof commented 9 months ago
@pytest.fixture(scope="session", autouse=True)
def generate_mo():
    """Generate .mo files."""
    key = "zope_i18n_compile_mo_files"
    current_value = os.getenv(key, None)
    os.environ[key] = "1"
    yield
    if current_value is None:
        os.environ.pop(key)
    else:
        os.environ[key] = current_value