Skip to content

Commit 2bb6b51

Browse files
committed
feat(i18n): separate state by domain
1 parent 775f6b5 commit 2bb6b51

File tree

9 files changed

+197
-115
lines changed

9 files changed

+197
-115
lines changed

examples/i18n/locale/de.po

+10-7
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: @local/i18n 0.1.0\n"
1010
"Report-Msgid-Bugs-To: Jane Doe <[email protected]>\n"
11-
"POT-Creation-Date: 2025-01-27 14:01+0100\n"
11+
"POT-Creation-Date: 2025-02-19 14:57+0100\n"
1212
"PO-Revision-Date: 2025-01-27 14:07+0100\n"
1313
"Last-Translator: \n"
14-
"Language-Team: de <[email protected]>\n"
1514
"Language: de\n"
15+
"Language-Team: de <[email protected]>\n"
16+
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
1617
"MIME-Version: 1.0\n"
1718
"Content-Type: text/plain; charset=utf-8\n"
1819
"Content-Transfer-Encoding: 8bit\n"
19-
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
2020
"Generated-By: Babel 2.16.0\n"
21-
"X-Generator: Poedit 3.5\n"
2221

2322
#. This comment will be shown in the .pot file.
2423
#: python/local/i18n/__init__.py:17
@@ -30,9 +29,13 @@ msgid ""
3029
"If you or a loved one has been diagnosed with mesothelioma, you may be "
3130
"entitled to financial compensation."
3231
msgstr ""
33-
"Falls Sie oder eine Ihnen nahestehende Person eine Mesotheliom-"
34-
"Diagnose erhalten haben, könnten Sie für finanzielle Entschädigung in "
35-
"Frage kommen."
32+
"Falls Sie oder eine Ihnen nahestehende Person eine Mesotheliom-Diagnose "
33+
"erhalten haben, könnten Sie für finanzielle Entschädigung in Frage "
34+
"kommen."
35+
36+
#: python/local/i18n/__init__.py:32
37+
msgid "It's all wrong!"
38+
msgstr "Es ist alles falsch!"
3639

3740
#. This comment will be shown in the .pot file.
3841
#: templates/formulation.xhtml.j2:4

examples/i18n/locale/en.po

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: @local/i18n 0.1.0\n"
1010
"Report-Msgid-Bugs-To: Jane Doe <[email protected]>\n"
11-
"POT-Creation-Date: 2025-01-27 14:01+0100\n"
11+
"POT-Creation-Date: 2025-02-19 14:57+0100\n"
1212
"PO-Revision-Date: 2025-01-27 14:01+0100\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language: en\n"
@@ -30,8 +30,11 @@ msgid ""
3030
"entitled to financial compensation."
3131
msgstr ""
3232

33+
#: python/local/i18n/__init__.py:32
34+
msgid "It's all wrong!"
35+
msgstr ""
36+
3337
#. This comment will be shown in the .pot file.
3438
#: templates/formulation.xhtml.j2:4
3539
msgid "This string should be localized!"
3640
msgstr ""
37-

examples/i18n/locale/local.i18n.pot

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ msgid ""
99
msgstr ""
1010
"Project-Id-Version: @local/i18n 0.1.0\n"
1111
"Report-Msgid-Bugs-To: Jane Doe <[email protected]>\n"
12-
"POT-Creation-Date: 2025-01-27 14:01+0100\n"
12+
"POT-Creation-Date: 2025-02-19 14:57+0100\n"
1313
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1414
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1515
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -29,6 +29,10 @@ msgid ""
2929
"entitled to financial compensation."
3030
msgstr ""
3131

32+
#: python/local/i18n/__init__.py:32
33+
msgid "It's all wrong!"
34+
msgstr ""
35+
3236
#. This comment will be shown in the .pot file.
3337
#: templates/formulation.xhtml.j2:4
3438
msgid "This string should be localized!"

examples/i18n/python/local/i18n/__init__.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# This file is part of the QuestionPy SDK. (https://questionpy.org)
22
# The QuestionPy SDK is free software released under terms of the MIT license. See LICENSE.md.
33
# (c) Technische Universität Berlin, innoCampus <[email protected]>
4-
from questionpy import Attempt, NeedsManualScoringError, Question, make_question_type_init
4+
from questionpy import Attempt, NeedsManualScoringError, Question, i18n, make_question_type_init
55
from questionpy.form import FormModel, static_text
6-
from questionpy.i18n import gettext as _
76
from questionpy_common.elements import StaticTextElement
87

9-
10-
def _(a):
11-
return a
8+
_, _N = i18n.get_for(__package__)
129

1310

1411
class I18NModel(FormModel):
12+
# TODO: Implement deferred translation.
13+
_ = lambda x: x # noqa: E731
14+
1515
txt: StaticTextElement = static_text(
1616
# TRANSLATORS: This comment will be shown in the .pot file.
1717
_("Important Notice"),
@@ -27,6 +27,10 @@ def _compute_score(self) -> float:
2727
def formulation(self) -> str:
2828
return self.jinja2.get_template("formulation.xhtml.j2").render()
2929

30+
@property
31+
def general_feedback(self) -> str | None:
32+
return "<div>" + _("It's all wrong!") + "</div>"
33+
3034

3135
class I18NQuestion(Question):
3236
attempt_class = I18NAttempt

poetry.lock

+48-53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ python = "^3.11"
2828
aiohttp = "^3.9.3"
2929
pydantic = "^2.6.4"
3030
PyYAML = "^6.0.1"
31-
questionpy-server = { git = "https://github.com/questionpy-org/questionpy-server.git", rev = "aca709b1dd8cef935ac4bf4bb47471959aac6779" }
31+
questionpy-server = { git = "https://github.com/questionpy-org/questionpy-server.git", rev = "a7d5229524bc95005c7bc409b94a349c095c97b3" }
3232
jinja2 = "^3.1.3"
3333
aiohttp-jinja2 = "^1.6"
3434
lxml = "~5.1.0"

questionpy/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
"WorkerResourceLimits",
9090
"create_jinja2_environment",
9191
"get_qpy_environment",
92+
"i18n",
9293
"make_question_type_init",
9394
"set_qpy_environment",
9495
]
@@ -97,8 +98,7 @@
9798
def make_question_type_init(
9899
question_class: type[Question], *, wrap_question: Callable[[Question], QuestionInterface] = QuestionWrapper
99100
) -> PackageInitFunction:
100-
def init(package: Package, env: Environment) -> QuestionTypeWrapper:
101-
i18n.initialize(package, env)
101+
def init(package: Package) -> QuestionTypeWrapper:
102102
return QuestionTypeWrapper(question_class, package, wrap_question=wrap_question)
103103

104104
return init

questionpy/_ui.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ def create_jinja2_environment(attempt: "Attempt", question: "Question") -> jinja
102102
"question_type": type(question),
103103
})
104104

105-
if i18n.is_initialized():
105+
translations = i18n.get_translations_of_package(package)
106+
if translations:
106107
env.add_extension("jinja2.ext.i18n")
107-
_, translations = i18n.get_state()
108-
env.install_gettext_translations(translations, newstyle=True)
108+
env.install_gettext_translations(translations, newstyle=True) # type: ignore[attr-defined]
109109

110110
return env

0 commit comments

Comments
 (0)