Skip to content

Remove i18n translation features #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/Tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ jobs:
- name: install ffmpeg and gifsicle
run: sudo apt update && sudo apt install ffmpeg gifsicle

- name: add required locales for tests
run: sudo locale-gen fr_FR.UTF-8 pt_BR.UTF-8 && sudo update-locale

- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v4
with:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **BREAKING** `i18n.get_language_details()`, `i18n.get_iso_lang_data()`, `i18n.find_language_names()` and `i18n.update_with_macro` now process / return a new typed `Lang` class #151
- **BREAKING** Rename `i18.NotFound` to `i18n.NotFoundError`

## Removed
- **BREAKING** Remove translation features in `i18n`: `Locale` class + `_` and `setlocale` functions #134


### Fixed

- Metadata length validation is buggy for unicode strings #158
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ zimscraperlib>=1.1,<1.2
* Pillow
* FFmpeg
* gifsicle (>=1.92)
* locale (with at least `fr_FR.UTF-8` and `pt_BR.utf8` locales installed for tests to pass)

## macOS

Expand All @@ -52,8 +51,6 @@ sudo apt install libmagic1 wget ffmpeg \
apk add ffmpeg gifsicle libmagic wget libjpeg
```

**Nota:** Alpine does not have `locale` support, so i18n features do not work on Alpine, see https://github.com/openzim/python-scraperlib/issues/134 ; there is one corresponding test which is failing.

# Contribution

This project adheres to openZIM's [Contribution Guidelines](https://github.com/openzim/overview/wiki/Contributing).
Expand Down
49 changes: 0 additions & 49 deletions src/zimscraperlib/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@

from __future__ import annotations

import gettext
import locale
import pathlib
import re

import babel
Expand All @@ -19,52 +16,6 @@ class NotFoundError(ValueError):
pass


class Locale:
short = "en"
name = "en_US.UTF-8"
locale_dir = None
domain = "messages"
translation = gettext.translation("messages", fallback=True)

@classmethod
def setup(cls, locale_dir: pathlib.Path, locale_name: str):
cls.name = locale_name
cls.locale_dir = str(locale_dir)

if "." in locale_name:
cls.lang, cls.encoding = locale_name.split(".")
else:
cls.lang, cls.encoding = locale_name, "UTF-8"

computed = locale.setlocale(locale.LC_ALL, (cls.lang, cls.encoding))

gettext.bindtextdomain(cls.domain, cls.locale_dir)
gettext.textdomain(cls.domain)

cls.translation = gettext.translation(
cls.domain, cls.locale_dir, languages=[cls.lang], fallback=True
)
return computed


def _(text: str) -> str:
"""translates text according to setup'd locale"""
return Locale.translation.gettext(text)


def setlocale(root_dir: pathlib.Path, locale_name: str):
"""set the desired locale for gettext.

call this early"""
try:
return Locale.setup(root_dir / "locale", locale_name)
except locale.Error as exc:
raise locale.Error(
f"Failed to setup '{locale_name}' locale. If this locale is not installed "
"on this system, please install it first."
) from exc


class Lang(dict):

@property
Expand Down
Binary file removed tests/i18n/locale/fr/LC_MESSAGES/messages.mo
Binary file not shown.
23 changes: 0 additions & 23 deletions tests/i18n/locale/fr/LC_MESSAGES/messages.po

This file was deleted.

Binary file removed tests/i18n/locale/pt/LC_MESSAGES/messages.mo
Binary file not shown.
24 changes: 0 additions & 24 deletions tests/i18n/locale/pt/LC_MESSAGES/messages.po

This file was deleted.

26 changes: 0 additions & 26 deletions tests/i18n/test_i18n.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
#!/usr/bin/env python3
# vim: ai ts=4 sts=4 et sw=4 nu

import locale
import pathlib
from unittest.mock import Mock

import pytest

from zimscraperlib.i18n import (
Lang,
NotFoundError,
_,
find_language_names,
get_language_details,
setlocale,
)


@pytest.mark.parametrize(
"code,expected",
[("en", "en_US.UTF-8"), ("en_us", "en_US.UTF-8"), ("en.utf8", "en_US.UTF-8")],
)
def test_setlocale(tmp_path, code, expected):
assert setlocale(tmp_path, code) == expected


def test_selocale_unsupported(tmp_path):
with pytest.raises(locale.Error):
setlocale(tmp_path, "bam")


@pytest.mark.parametrize(
"query,expected",
[
Expand Down Expand Up @@ -222,15 +205,6 @@ def test_lang_name(query, expected):
assert find_language_names(query) == expected


@pytest.mark.parametrize(
"lang,expected",
[("en", "Hello World!"), ("fr", "Bonjour monde !"), ("pt_BR.utf8", "Olá Mundo!")],
)
def test_translation(lang, expected):
setlocale(pathlib.Path(__file__).parent, lang)
assert _("Hello World!") == expected


@pytest.mark.parametrize(
"dict_data",
[{}, {"iso-639-1": "ar"}],
Expand Down
Loading