Skip to content
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

bridge: Drop translations from manifests.js, introduce m-i18n.js #21516

Merged
merged 2 commits into from
Jan 14, 2025
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
27 changes: 21 additions & 6 deletions pkg/base1/test-locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ const ru = {
"$0 bit": ["$0 bits", "$0 бит", "$0 бита", "$0 бит"]
};

const zh_CN = {
"": {
language: "zh_CN",
"language-direction": "ltr",
"plural-forms": (n) => 0,
},
"$0 important hit": [null, "$0 次命中(含关键命中)"],
};

QUnit.test("public api", function (assert) {
assert.equal(typeof cockpit.locale, "function", "cockpit.locale is a function");
});
Expand Down Expand Up @@ -83,12 +92,18 @@ QUnit.test("ngettext simple", function (assert) {
QUnit.test("ngettext complex", function (assert) {
cockpit.locale(null); /* clear it */
cockpit.locale(ru);
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 0), "$0 бит", "zero things");
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 1), "$0 бит", "one thing");
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 5), "$0 бит", "multiple things");
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 23), "$0 бита", "genitive singular");
assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 1), "$0 byte", "default one");
assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 2), "$0 bytes", "default multiple");
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 0), "$0 бит", "ru, zero things");
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 1), "$0 бит", "ru, one thing");
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 5), "$0 бит", "ru, multiple things");
assert.equal(cockpit.ngettext("$0 bit", "$0 bits", 23), "$0 бита", "ru, genitive singular");
assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 1), "$0 byte", "ru, default one");
assert.equal(cockpit.ngettext("$0 byte", "$0 bytes", 2), "$0 bytes", "ru, default multiple");

cockpit.locale(null); /* clear it */
cockpit.locale(zh_CN);
[0, 1, 2].forEach(i =>
assert.equal(cockpit.ngettext("$0 important hit", "$0 XXX", i), "$0 次命中(含关键命中)", `zh_CN, ${i} things`)
);
});

QUnit.test("translate document", function (assert) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/shell/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link href="shell.css" rel="stylesheet" />
<link href="../../static/branding.css" rel="stylesheet" />
<script src="../base1/cockpit.js"></script>
<script src="../manifests.js"></script>
<script src="../manifests-i18n.js"></script>
<script src="po.js"></script>
<script src="shell.js"></script>
</head>
Expand Down
31 changes: 17 additions & 14 deletions src/cockpit/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,26 +516,27 @@ def reload_hint(self):
self.reload()
self.saw_first_reload_hint = True

def load_manifests_js(self, headers: JsonObject) -> Document:
def load_manifests_js(self, headers: JsonObject, *, i18n: bool) -> Document:
logger.debug('Serving /manifests.js')

chunks: List[bytes] = []

# Send the translations required for the manifest files, from each package
locales = parse_accept_language(get_str(headers, 'Accept-Language', ''))
for name, package in self.packages.items():
if name in ['static', 'base1']:
continue
if i18n:
locales = parse_accept_language(get_str(headers, 'Accept-Language', ''))
for name, package in self.packages.items():
if name in ['static', 'base1']:
continue

# find_translation will always find at least 'en'
translation = package.load_translation('po.manifest.js', locales)
with translation.data:
if translation.content_encoding == 'gzip':
data = gzip.decompress(translation.data.read())
else:
data = translation.data.read()
# find_translation will always find at least 'en'
translation = package.load_translation('po.manifest.js', locales)
with translation.data:
if translation.content_encoding == 'gzip':
data = gzip.decompress(translation.data.read())
else:
data = translation.data.read()

chunks.append(data)
chunks.append(data)

chunks.append(b"""
(function (root, data) {
Expand Down Expand Up @@ -573,7 +574,9 @@ def load_path(self, path: str, headers: JsonObject) -> Document:
if packagename is not None:
return self.packages[packagename].load_path(filename, headers)
elif filename == 'manifests.js':
return self.load_manifests_js(headers)
return self.load_manifests_js(headers, i18n=False)
elif filename == 'manifests-i18n.js':
return self.load_manifests_js(headers, i18n=True)
elif filename == 'manifests.json':
return self.load_manifests_json()
else:
Expand Down
2 changes: 1 addition & 1 deletion test/pytest/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_translation(pkgdir):
assert document.data.read() == b''

# make sure the manifest translations get sent along with manifests.js
document = packages.load_path('/manifests.js', {'Accept-Language': 'de'})
document = packages.load_path('/manifests-i18n.js', {'Accept-Language': 'de'})
contents = document.data.read()
assert b'eins\n' in contents
assert b'zwo\n' in contents
Expand Down
Loading