Skip to content

Commit 7ec0bc1

Browse files
t() in a store, and a key no table has filled like a message
A store's t() and setLocale() compiled to bare calls the page could not find; they go through WF.i18n as a page's do. When no table had a key, the runtime returned the key without filling its placeholders. Under i18n.literals that is the words as written, so a string like "{n} missing" showed its braces in the default locale. The key now has its placeholders filled as a message does, which is what the build-time resolver already did.
1 parent 20cb160 commit 7ec0bc1

4 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎src/codegen/js.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,9 @@ impl JsCodegen {
17731773
// call, but `parseInt` is a plain function).
17741774
if self.is_own_member(name, store_states) {
17751775
format!("store.{}({})", name, args_str.join(", "))
1776+
} else if (name == "t" || name == "setLocale") && self.has_i18n() {
1777+
// A store's words, translated as a page's are.
1778+
format!("WF.i18n.{}({})", name, args_str.join(", "))
17761779
} else if matches!(
17771780
name.as_str(),
17781781
// The same set a page reaches: a store's action is where

‎src/runtime/modules/i18n.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
if (text === undefined) text = lookup(key + ".other");
3333
}
3434
if (text === undefined) text = lookup(key);
35-
// Fallback to key itself
36-
if (text === undefined) return key;
35+
// No table has it: the key itself — under `i18n.literals`, the words
36+
// as written — its placeholders filled as a message's are.
37+
if (text === undefined) text = key;
3738
// Interpolate {placeholder} tokens
3839
if (params && text.includes("{")) {
3940
for (const [k, v] of Object.entries(params)) {

‎tests/build_api.rs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,12 +299,12 @@ fn a_project_translated_by_its_own_words() {
299299
std::fs::write(dir.join("src/translations/en.json"), "{}\n").unwrap();
300300
std::fs::write(
301301
dir.join("src/translations/ar.json"),
302-
"{ \"Problems\": \"المشاكل\", \"{n} missing\": \"{n} مفقودة\" }\n",
302+
"{ \"Problems\": \"المشاكل\", \"{n} missing\": \"{n} مفقودة\", \"Ln {line}\": \"سطر {line}\" }\n",
303303
)
304304
.unwrap();
305305
std::fs::write(
306306
dir.join("src/pages/Home.wf"),
307-
"page Home(path: \"/\", title: \"Home\", description: \"Words.\") {\n state n = 3\n Heading(\"Problems\").h1\n Text(\"{n} missing\")\n Icon(\"warning\")\n}\n",
307+
"store Ed {\n state line = 1\n derived caretText = \"Ln {line}\"\n}\npage Home(path: \"/\", title: \"Home\", description: \"Words.\") {\n use Ed\n state n = 3\n Heading(\"Problems\").h1\n Text(\"{n} missing\")\n Text(Ed.caretText)\n Icon(\"warning\")\n}\n",
308308
)
309309
.unwrap();
310310
let vfs = OverlayVfs::over_disk();
@@ -346,6 +346,11 @@ fn a_project_translated_by_its_own_words() {
346346
"the keys are asked for"
347347
);
348348
assert!(js.contains("المشاكل"), "the table is in the build");
349+
assert!(
350+
js.contains("WF.i18n.t(\"Ln {line}\""),
351+
"a store's words go through the table too"
352+
);
353+
assert!(!js.contains(" t(\""), "no bare t()");
349354
// The table counts them as used, and what the other locale is missing.
350355
let t = webfluent::i18n::table::table_via(&vfs, &dir)
351356
.unwrap()

‎tests/js/i18n.test.mjs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,12 @@ test("a locale with a region is written in its language's direction", () => {
6262
i18n.setLocale("ckb");
6363
assert.equal(i18n.dir(), "rtl");
6464
});
65+
66+
test("a key no table has is shown as written, its placeholders filled — a project's words under i18n.literals", () => {
67+
const WF = load("");
68+
const i18n = WF.locales("en", { en: {}, ar: { "{n} missing": "{n} مفقودة" } });
69+
assert.equal(i18n.t("{n} missing", { n: 3 }), "3 missing");
70+
i18n.setLocale("ar");
71+
assert.equal(i18n.t("{n} missing", { n: 3 }), "3 مفقودة");
72+
assert.equal(i18n.t("{a} and {b}", { a: "x", b: "y" }), "x and y");
73+
});

0 commit comments

Comments
 (0)