Skip to content

Commit 29931fb

Browse files
authored
Merge pull request #187 from openzim/remove_i18n_translation
Remove i18n translation features
2 parents b1a432a + 2a03e81 commit 29931fb

File tree

9 files changed

+4
-128
lines changed

9 files changed

+4
-128
lines changed

.github/workflows/Tests.yaml

-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ jobs:
2020
- name: install ffmpeg and gifsicle
2121
run: sudo apt update && sudo apt install ffmpeg gifsicle
2222

23-
- name: add required locales for tests
24-
run: sudo locale-gen fr_FR.UTF-8 pt_BR.UTF-8 && sudo update-locale
25-
2623
- name: Set up Python ${{ matrix.python }}
2724
uses: actions/setup-python@v4
2825
with:

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3535
- **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
3636
- **BREAKING** Rename `i18.NotFound` to `i18n.NotFoundError`
3737

38+
## Removed
39+
- **BREAKING** Remove translation features in `i18n`: `Locale` class + `_` and `setlocale` functions #134
40+
41+
3842
### Fixed
3943

4044
- Metadata length validation is buggy for unicode strings #158

README.md

-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ zimscraperlib>=1.1,<1.2
3030
* Pillow
3131
* FFmpeg
3232
* gifsicle (>=1.92)
33-
* locale (with at least `fr_FR.UTF-8` and `pt_BR.utf8` locales installed for tests to pass)
3433

3534
## macOS
3635

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

55-
**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.
56-
5754
# Contribution
5855

5956
This project adheres to openZIM's [Contribution Guidelines](https://github.com/openzim/overview/wiki/Contributing).

src/zimscraperlib/i18n.py

-49
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
from __future__ import annotations
55

6-
import gettext
7-
import locale
8-
import pathlib
96
import re
107

118
import babel
@@ -19,52 +16,6 @@ class NotFoundError(ValueError):
1916
pass
2017

2118

22-
class Locale:
23-
short = "en"
24-
name = "en_US.UTF-8"
25-
locale_dir = None
26-
domain = "messages"
27-
translation = gettext.translation("messages", fallback=True)
28-
29-
@classmethod
30-
def setup(cls, locale_dir: pathlib.Path, locale_name: str):
31-
cls.name = locale_name
32-
cls.locale_dir = str(locale_dir)
33-
34-
if "." in locale_name:
35-
cls.lang, cls.encoding = locale_name.split(".")
36-
else:
37-
cls.lang, cls.encoding = locale_name, "UTF-8"
38-
39-
computed = locale.setlocale(locale.LC_ALL, (cls.lang, cls.encoding))
40-
41-
gettext.bindtextdomain(cls.domain, cls.locale_dir)
42-
gettext.textdomain(cls.domain)
43-
44-
cls.translation = gettext.translation(
45-
cls.domain, cls.locale_dir, languages=[cls.lang], fallback=True
46-
)
47-
return computed
48-
49-
50-
def _(text: str) -> str:
51-
"""translates text according to setup'd locale"""
52-
return Locale.translation.gettext(text)
53-
54-
55-
def setlocale(root_dir: pathlib.Path, locale_name: str):
56-
"""set the desired locale for gettext.
57-
58-
call this early"""
59-
try:
60-
return Locale.setup(root_dir / "locale", locale_name)
61-
except locale.Error as exc:
62-
raise locale.Error(
63-
f"Failed to setup '{locale_name}' locale. If this locale is not installed "
64-
"on this system, please install it first."
65-
) from exc
66-
67-
6819
class Lang(dict):
6920

7021
@property
-487 Bytes
Binary file not shown.

tests/i18n/locale/fr/LC_MESSAGES/messages.po

-23
This file was deleted.
-567 Bytes
Binary file not shown.

tests/i18n/locale/pt/LC_MESSAGES/messages.po

-24
This file was deleted.

tests/i18n/test_i18n.py

-26
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,18 @@
11
#!/usr/bin/env python3
22
# vim: ai ts=4 sts=4 et sw=4 nu
33

4-
import locale
5-
import pathlib
64
from unittest.mock import Mock
75

86
import pytest
97

108
from zimscraperlib.i18n import (
119
Lang,
1210
NotFoundError,
13-
_,
1411
find_language_names,
1512
get_language_details,
16-
setlocale,
1713
)
1814

1915

20-
@pytest.mark.parametrize(
21-
"code,expected",
22-
[("en", "en_US.UTF-8"), ("en_us", "en_US.UTF-8"), ("en.utf8", "en_US.UTF-8")],
23-
)
24-
def test_setlocale(tmp_path, code, expected):
25-
assert setlocale(tmp_path, code) == expected
26-
27-
28-
def test_selocale_unsupported(tmp_path):
29-
with pytest.raises(locale.Error):
30-
setlocale(tmp_path, "bam")
31-
32-
3316
@pytest.mark.parametrize(
3417
"query,expected",
3518
[
@@ -222,15 +205,6 @@ def test_lang_name(query, expected):
222205
assert find_language_names(query) == expected
223206

224207

225-
@pytest.mark.parametrize(
226-
"lang,expected",
227-
[("en", "Hello World!"), ("fr", "Bonjour monde !"), ("pt_BR.utf8", "Olá Mundo!")],
228-
)
229-
def test_translation(lang, expected):
230-
setlocale(pathlib.Path(__file__).parent, lang)
231-
assert _("Hello World!") == expected
232-
233-
234208
@pytest.mark.parametrize(
235209
"dict_data",
236210
[{}, {"iso-639-1": "ar"}],

0 commit comments

Comments
 (0)