From 2a03e81385b5ffe149ad66ba3c968e66ce067f9e Mon Sep 17 00:00:00 2001 From: benoit74 Date: Fri, 2 Aug 2024 13:54:33 +0000 Subject: [PATCH] Remove i18n translation features --- .github/workflows/Tests.yaml | 3 -- CHANGELOG.md | 4 ++ README.md | 3 -- src/zimscraperlib/i18n.py | 49 ------------------- tests/i18n/locale/fr/LC_MESSAGES/messages.mo | Bin 487 -> 0 bytes tests/i18n/locale/fr/LC_MESSAGES/messages.po | 23 --------- tests/i18n/locale/pt/LC_MESSAGES/messages.mo | Bin 567 -> 0 bytes tests/i18n/locale/pt/LC_MESSAGES/messages.po | 24 --------- tests/i18n/test_i18n.py | 26 ---------- 9 files changed, 4 insertions(+), 128 deletions(-) delete mode 100644 tests/i18n/locale/fr/LC_MESSAGES/messages.mo delete mode 100644 tests/i18n/locale/fr/LC_MESSAGES/messages.po delete mode 100644 tests/i18n/locale/pt/LC_MESSAGES/messages.mo delete mode 100644 tests/i18n/locale/pt/LC_MESSAGES/messages.po diff --git a/.github/workflows/Tests.yaml b/.github/workflows/Tests.yaml index 3e4de467..0fd2de44 100644 --- a/.github/workflows/Tests.yaml +++ b/.github/workflows/Tests.yaml @@ -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: diff --git a/CHANGELOG.md b/CHANGELOG.md index 7c833e6b..e94635fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 903829c8..835832d2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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). diff --git a/src/zimscraperlib/i18n.py b/src/zimscraperlib/i18n.py index 333739d4..3ad11da8 100644 --- a/src/zimscraperlib/i18n.py +++ b/src/zimscraperlib/i18n.py @@ -3,9 +3,6 @@ from __future__ import annotations -import gettext -import locale -import pathlib import re import babel @@ -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 diff --git a/tests/i18n/locale/fr/LC_MESSAGES/messages.mo b/tests/i18n/locale/fr/LC_MESSAGES/messages.mo deleted file mode 100644 index fe8f79b01902fada593b4f3120a994f6487efbaf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 487 zcmaJ-O-sW-6pZ322!dx1uQx@vZVS?qeqcXRO-b64#(Lg1+nBQ54V#VF|KQc%=FQ*Y zi-OgQ1H;ZN!<)zZI6Zo^5QoS!z)gt;8AH2SFD*< z8@@8ci^+oPJXO+xSvVcLg9sLGIQOO#JLH+th6H(%#-v{)If;}5ZqW1m+um>(y7Rd` zn?_`yIWstc44L5$)M=d(dP8UfYBh(_ONziC3HftcHvOCPS#C(Anal+7;AVM4_1=Bc->w12X%vd8gijZ`nDQ z(1qHC9e9Dezeuf0?SYbpOWbLfVUyu6<~kEBl`U93F`aYMDa@KQ_qlTSHP^(ID;1|Q naiF, 2020. -# -msgid "" -msgstr "" -"Project-Id-Version: PROJECT VERSION\n" -"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2020-05-07 11:00+0000\n" -"PO-Revision-Date: 2020-05-07 11:00+0000\n" -"Last-Translator: FULL NAME \n" -"Language: fr\n" -"Language-Team: fr \n" -"Plural-Forms: nplurals=2; plural=(n > 1)\n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=utf-8\n" -"Content-Transfer-Encoding: 8bit\n" -"Generated-By: Babel 2.7.0\n" - -#: tests/i18n/test_i18n.py:113 -msgid "Hello World!" -msgstr "Bonjour monde !" diff --git a/tests/i18n/locale/pt/LC_MESSAGES/messages.mo b/tests/i18n/locale/pt/LC_MESSAGES/messages.mo deleted file mode 100644 index 50485339c4caf489a091003e8ef2a042b87ade41..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 567 zcmaJ;OHRWu5Dkb`V8xonumFL;#R*@9x=`8xMG39a0Bh5j5M=DiaS$AY18^4Zfj9~i zDpA-l(v#o#&G^~lkHxuH2V)kogxIW;U(rOqfMAHv8OOQ6bsn)%H=GM8HQZ<;vt?&s z^n(~#(#y!TFlC`t0E47|5q46z4wGT8f9WP-tc@jcnHL#3sq&JfI)E^4_oCx=x0{5+ zp*!fOq+feoa?AN?U9jR#zvy$-KH_d#cf2THUsT((bw&_u_CmWv@Zq zj#gGEeCNv;Z*Rr3-5N_?s6!atb5jb-Dtku`rgil^?t~$s8tJT1c>o8ug>}z_5(bBk b-}4f{3BMH*{Kh^yIDPs4263e_z3hAel7N@q diff --git a/tests/i18n/locale/pt/LC_MESSAGES/messages.po b/tests/i18n/locale/pt/LC_MESSAGES/messages.po deleted file mode 100644 index 8e770a9a..00000000 --- a/tests/i18n/locale/pt/LC_MESSAGES/messages.po +++ /dev/null @@ -1,24 +0,0 @@ -# Arabic translations for PROJECT. -# Copyright (C) 2020 ORGANIZATION -# This file is distributed under the same license as the PROJECT project. -# FIRST AUTHOR , 2020. -# -msgid "" -msgstr "" -"Project-Id-Version: PROJECT VERSION\n" -"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" -"POT-Creation-Date: 2020-05-07 11:00+0000\n" -"PO-Revision-Date: 2020-05-07 11:00+0000\n" -"Last-Translator: FULL NAME \n" -"Language: ar\n" -"Language-Team: ar \n" -"Plural-Forms: nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : " -"n%100>=3 && n%100<=10 ? 3 : n%100>=0 && n%100<=2 ? 4 : 5)\n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=utf-8\n" -"Content-Transfer-Encoding: 8bit\n" -"Generated-By: Babel 2.7.0\n" - -#: tests/i18n/test_i18n.py:113 -msgid "Hello World!" -msgstr "Olá Mundo!" diff --git a/tests/i18n/test_i18n.py b/tests/i18n/test_i18n.py index bbff772a..5ae2b960 100644 --- a/tests/i18n/test_i18n.py +++ b/tests/i18n/test_i18n.py @@ -1,8 +1,6 @@ #!/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 @@ -10,26 +8,11 @@ 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", [ @@ -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"}],