diff --git a/crates/craft-i18n-tests/src/tree.rs b/crates/craft-i18n-tests/src/tree.rs index 6e861812..ba01f767 100644 --- a/crates/craft-i18n-tests/src/tree.rs +++ b/crates/craft-i18n-tests/src/tree.rs @@ -105,9 +105,12 @@ pub fn check_tree(root: &Path) -> Vec { /// Every Draft list under `root/drafts` (ADR 0007): `drafts//.tsv`, one /// `contextsource text` row per Draft, escaped as in a catalog. An entry whose catalog row -/// is missing fails, so a list never names a row the app doesn't show. +/// is missing fails, so a list never names a row the app doesn't show; so does an app entry for a +/// shared string's override, which Weblate never offers and so can't accept. fn draft_problems(root: &Path, catalogs: &BTreeMap) -> Vec { let mut problems = Vec::new(); + let shared: HashSet<&(String, String)> = + catalogs.values().flat_map(|l| l.common.keys()).collect(); let dir = root.join("drafts"); let locales = match entries(&dir) { Ok(locales) => locales, @@ -150,6 +153,10 @@ fn draft_problems(root: &Path, catalogs: &BTreeMap) -> Vec { Some(format!("no such row in locales/{locale}/{file}")) } + Ok(key) if file != COMMON && shared.contains(&key) => Some( + "an override of a shared string; Weblate doesn't offer it, so it can't be accepted there" + .into(), + ), Ok(_) => None, }; if let Some(problem) = problem { diff --git a/crates/craft-i18n-tests/tests/locales.rs b/crates/craft-i18n-tests/tests/locales.rs index 887c7ae0..c32b0e47 100644 --- a/crates/craft-i18n-tests/tests/locales.rs +++ b/crates/craft-i18n-tests/tests/locales.rs @@ -65,15 +65,19 @@ fn clean_tree_passes() { #[test] fn draft_list_entry_needs_its_catalog_row() { let common = format!("{H}{NAME}\tSave\tEnregistrer\n"); + let app = format!("{H}\tSave\tSauvegarder\n\tPlay\tJouer\n"); assert_problems( "drafts", &[ ("locales/fr/common.tsv", &common), + ("locales/fr/anicraft.tsv", &app), ("drafts/fr/common.tsv", "# Drafts\n\tSave\n\tOpen\n\tSave\n"), - ("drafts/fr/anicraft.tsv", "\tSave\n"), + ("drafts/fr/anicraft.tsv", "\tPlay\n\tSave\n"), + ("drafts/fr/cadcraft.tsv", "\tPlay\n"), ], &[ - "drafts/fr/anicraft.tsv: line 1: no such row", + "drafts/fr/anicraft.tsv: line 2: an override", + "drafts/fr/cadcraft.tsv: line 1: no such row", "drafts/fr/common.tsv: line 3: no such row", "drafts/fr/common.tsv: line 4: listed twice", ], diff --git a/docs/catalog-format.md b/docs/catalog-format.md index c06ad023..62ef5e3d 100644 --- a/docs/catalog-format.md +++ b/docs/catalog-format.md @@ -84,7 +84,10 @@ context English source text one row per Draft, written exactly as in the - `#` lines and empty lines are ignored. Two columns, escaped as in the catalog. - CI fails a row that names no row of its catalog, or that is listed twice. - Weblate shows a listed row as "Needs editing". When a Reviewer accepts it, the next sync PR - takes it off the list (and changes its wording if the Reviewer did). + takes it off the list (and changes its wording if the Reviewer did). If the row changed on + `main` after the export, it stays listed, and Weblate shows the new wording as "Needs editing". +- An app list names only that app's own rows, not an override of a shared string: Weblate offers + overrides nowhere, so they can't be accepted there. - Rows added before ADR 0007 are not listed. ## What an app shows diff --git a/tools/test_weblate_bridge.py b/tools/test_weblate_bridge.py index 30a0be4d..0b321571 100644 --- a/tools/test_weblate_bridge.py +++ b/tools/test_weblate_bridge.py @@ -191,6 +191,24 @@ class Bridge(unittest.TestCase): self.assertIn("\tBold\tFettdruck\n", self.tsv("de", "wordcraft")) self.assertEqual(path.read_text(encoding="utf-8"), "") + def test_a_draft_main_rewords_after_acceptance_stays_listed(self): + path = self.drafts("\ufeff\tBold\n") + self.export() + self.set_po("wordcraft", "de", '#, fuzzy\nmsgid "Bold"', 'msgid "Bold"') + self.write("de", "wordcraft", ("", "Open", "Aufmachen"), ("", "Bold", "Halbfett"), + ("@msg", "Couldn't open {name}: {e}", "{name} ließ sich nicht öffnen: {e}")) + self.assertEqual(wb.export(self.po, self.base, self.root), []) + self.assertIn('#, fuzzy\nmsgid "Bold"\nmsgstr "Halbfett"\n', self.po_file("wordcraft", "de")) + self.assertEqual(wb.import_(self.po, self.base, self.root)[0], []) + self.assertEqual(path.read_text(encoding="utf-8"), "\ufeff\tBold\n") + + def test_an_accepted_draft_after_a_bom_leaves_the_list(self): + path = self.drafts("\ufeff\tBold\n") + self.export() + self.set_po("wordcraft", "de", '#, fuzzy\nmsgid "Bold"', 'msgid "Bold"') + self.assertEqual(wb.import_(self.po, self.base, self.root)[0], []) + self.assertEqual(path.read_text(encoding="utf-8"), "") + def test_fuzzy_and_bad_plurals(self): self.export() self.set_po("common", "de", 'msgid "Open"\nmsgstr "Öffnen"', '#, fuzzy\nmsgid "Open"\nmsgstr "Auf"') diff --git a/tools/weblate_bridge.py b/tools/weblate_bridge.py index 73f992c7..7eae4acf 100644 --- a/tools/weblate_bridge.py +++ b/tools/weblate_bridge.py @@ -230,8 +230,9 @@ def po_values(path, nplurals, problems, fuzzy=None): def weblate_changes(po_dir, base_dir, root, problems): """({(component, locale): {key: (translation at the last export or None, Weblate's)}} for every - translation Weblate changed since the last export, {(component, locale): {key}} for every - entry exported fuzzy that Weblate now has non-fuzzy: an accepted Draft).""" + translation Weblate changed since the last export, {(component, locale): {key: Weblate's}} for + every entry exported fuzzy that Weblate now has non-fuzzy: an accepted Draft, with the wording + the Reviewer accepted).""" table = plural_table(root) out, accepted = {}, {} for component in COMPONENTS: @@ -245,7 +246,7 @@ def weblate_changes(po_dir, base_dir, root, problems): if changed: out[(component, locale)] = changed if was_fuzzy & now.keys(): - accepted[(component, locale)] = was_fuzzy & now.keys() + accepted[(component, locale)] = {k: now[k] for k in was_fuzzy & now.keys()} return out, accepted @@ -318,12 +319,14 @@ def export(po_dir, base_dir, root): for i in sorted(e["strs"]))) for e in existing if e["id"] != ""} body = "" - draft = listed.get((locale, component), set()) - accepted.get((component, locale), set()) + draft = listed.get((locale, component), set()) + ok = accepted.get((component, locale), {}) for k in keys: value = values.get(k, "") was = old.get(k) flags = was[0] if was and was[1] == value else set() - if k in draft: + # Fuzzy until a Reviewer accepted this very wording (main may have changed it since). + if k in draft and ok.get(k) != value: flags = flags | {"fuzzy"} body += "\n" + render_entry(k, value, n, flags) write_if_changed(path, header(component, locale, table) + body) @@ -379,7 +382,7 @@ def edit_catalog(path, updates, name, locale, app): def edit_drafts(path, keys): """Take `keys` off a Draft list, changing only those lines.""" lines = path.read_text(encoding="utf-8").split("\n") - kept = [l for l in lines if tuple(l.rstrip("\r").split("\t")) not in keys] + kept = [l for l in lines if tuple(l.lstrip("\ufeff").rstrip("\r").split("\t")) not in keys] path.write_text("\n".join(kept), encoding="utf-8", newline="\n") @@ -391,7 +394,7 @@ def import_(po_dir, base_dir, root): cats = catalogs(root) shared = {k for (_, c), rows in cats.items() if c == "common" for k in rows} - listed = [] + listed, applied = [], {} for (component, locale), rows in sorted(changes.items()): if not (root / "locales" / locale).is_dir(): problems.append(f"{component}/{locale}.po: no such locale in locales/") @@ -409,15 +412,17 @@ def import_(po_dir, base_dir, root): continue updates[key] = value listed.append(change) + applied[(component, locale)] = updates if updates: path = root / "locales" / locale / f"{component}.tsv" edit_catalog(path, updates, language_name(root, locale), locale, COMPONENTS[component]) - # An accepted Draft leaves its list, unless its row changed on main too (a conflict). - conflicts = {(c["component"], c["locale"], c["context"], c["source"]) for c in listed if c.get("conflict")} + # An accepted Draft leaves its list only if the catalog now holds the wording the Reviewer + # accepted: not if main changed the row since the export. listed_drafts = drafts(root) - for (component, locale), keys in sorted(accepted.items()): - keys = {k for k in keys & listed_drafts.get((locale, component), set()) - if (component, locale, *k) not in conflicts} + for (component, locale), ok in sorted(accepted.items()): + final = cats.get((locale, component), {}) | applied.get((component, locale), {}) + keys = {k for k, v in ok.items() if k in listed_drafts.get((locale, component), set()) + and final.get(k) == v} if keys: edit_drafts(root / "drafts" / locale / f"{component}.tsv", keys) listed += [{"component": component, "locale": locale, "context": k[0], "source": k[1],