From 35d8a16804d86a1f2f8eea724a550fda17d62acd Mon Sep 17 00:00:00 2001 From: Cory Francis Myers Date: Wed, 1 Sep 2021 12:45:34 -0700 Subject: [PATCH] configure_locale_and_language(): switches from class-based to GNU gettext API Fixes #1293 by using gettext.[bind]textdomain(), which will allow from gettext import gettext as _ to use the globally-configured locale without running afoul of mypy. This reverts commit 4dd4d13b2816202f4727a349a330d031a95f2237. --- securedrop_client/app.py | 7 ++----- securedrop_client/gui/main.py | 1 + securedrop_client/gui/widgets.py | 2 ++ securedrop_client/logic.py | 1 + tests/gui/test_widgets.py | 1 + tests/test_logic.py | 1 + 6 files changed, 8 insertions(+), 5 deletions(-) diff --git a/securedrop_client/app.py b/securedrop_client/app.py index 4c554bb65..e42ac69f9 100644 --- a/securedrop_client/app.py +++ b/securedrop_client/app.py @@ -72,11 +72,8 @@ def configure_locale_and_language() -> str: language_code = current_locale[:2] except ValueError: # pragma: no cover language_code = "en" # pragma: no cover - # DEBUG/TRANSLATE: override the language code here (e.g. to Chinese). - # language_code = 'zh' - gettext.translation( - "messages", localedir=localedir, languages=[language_code], fallback=True - ).install("ngettext") + gettext.bindtextdomain("messages", localedir=localedir) + gettext.textdomain("messages") return language_code diff --git a/securedrop_client/gui/main.py b/securedrop_client/gui/main.py index 613ac2d87..b5d0214e2 100644 --- a/securedrop_client/gui/main.py +++ b/securedrop_client/gui/main.py @@ -20,6 +20,7 @@ along with this program. If not, see . """ import logging +from gettext import gettext as _ from typing import Dict, List, Optional # noqa: F401 from PyQt5.QtCore import Qt diff --git a/securedrop_client/gui/widgets.py b/securedrop_client/gui/widgets.py index 217b23695..39159dba7 100644 --- a/securedrop_client/gui/widgets.py +++ b/securedrop_client/gui/widgets.py @@ -19,6 +19,8 @@ import html import logging import sys +from gettext import gettext as _ +from gettext import ngettext from typing import Dict, List, Optional, Union # noqa: F401 from uuid import uuid4 diff --git a/securedrop_client/logic.py b/securedrop_client/logic.py index a28e841a4..cd979e9e0 100644 --- a/securedrop_client/logic.py +++ b/securedrop_client/logic.py @@ -22,6 +22,7 @@ import logging import os import uuid +from gettext import gettext as _ from typing import Any, Dict, List, Tuple, Type, Union # noqa: F401 import arrow diff --git a/tests/gui/test_widgets.py b/tests/gui/test_widgets.py index 3067c1f3f..dbbaa8b3a 100644 --- a/tests/gui/test_widgets.py +++ b/tests/gui/test_widgets.py @@ -4,6 +4,7 @@ import math import random from datetime import datetime +from gettext import gettext as _ from unittest.mock import Mock, PropertyMock, patch import arrow diff --git a/tests/test_logic.py b/tests/test_logic.py index 147cad5c7..6e48f0557 100644 --- a/tests/test_logic.py +++ b/tests/test_logic.py @@ -5,6 +5,7 @@ import datetime import logging import os +from gettext import gettext as _ from typing import Type from unittest.mock import Mock, call