From 177d98489a5d46bb7cf91e96d83244110ae69082 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Wed, 18 Nov 2020 09:12:25 +1000 Subject: [PATCH] update pylib ngettext references --- pylib/anki/importing/mnemo.py | 5 +---- pylib/anki/importing/noteimp.py | 12 +++--------- qt/po/scripts/rewrite-refs.py | 31 ++++++++++++++++++++++++------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/pylib/anki/importing/mnemo.py b/pylib/anki/importing/mnemo.py index 40c58056d..ed798ecab 100644 --- a/pylib/anki/importing/mnemo.py +++ b/pylib/anki/importing/mnemo.py @@ -7,7 +7,6 @@ from typing import cast from anki.db import DB from anki.importing.noteimp import ForeignCard, ForeignNote, NoteImporter -from anki.lang import ngettext from anki.rsbackend import TR from anki.stdmodels import addBasicModel, addClozeModel @@ -104,9 +103,7 @@ acq_reps+ret_reps, lapses, card_type_id from cards""" self.total += total self._addCloze(cloze) self.total += total - self.log.append( - ngettext("%d note imported.", "%d notes imported.", self.total) % self.total - ) + self.log.append(self.col.tr(TR.IMPORTING_NOTE_IMPORTED, count=self.total)) def fields(self): return self._fields diff --git a/pylib/anki/importing/noteimp.py b/pylib/anki/importing/noteimp.py index e4a380c06..d90c18dfb 100644 --- a/pylib/anki/importing/noteimp.py +++ b/pylib/anki/importing/noteimp.py @@ -7,7 +7,6 @@ from typing import Dict, List, Optional, Tuple, Union from anki.collection import Collection from anki.consts import NEW_CARDS_RANDOM, STARTING_FACTOR from anki.importing.base import Importer -from anki.lang import ngettext from anki.rsbackend import TR from anki.utils import ( fieldChecksum, @@ -221,20 +220,15 @@ class NoteImporter(Importer): if conf["new"]["order"] == NEW_CARDS_RANDOM: self.col.sched.randomizeCards(did) - part1 = ngettext("%d note added", "%d notes added", len(new)) % len(new) - part2 = ( - ngettext("%d note updated", "%d notes updated", self.updateCount) - % self.updateCount - ) + part1 = self.col.tr(TR.IMPORTING_NOTE_ADDED, count=len(new)) + part2 = self.col.tr(TR.IMPORTING_NOTE_UPDATED, count=self.updateCount) if self.importMode == UPDATE_MODE: unchanged = dupeCount - self.updateCount elif self.importMode == IGNORE_MODE: unchanged = dupeCount else: unchanged = 0 - part3 = ( - ngettext("%d note unchanged", "%d notes unchanged", unchanged) % unchanged - ) + part3 = self.col.tr(TR.IMPORTING_NOTE_UNCHANGED, count=unchanged) self.log.append("%s, %s, %s." % (part1, part2, part3)) self.log.extend(updateLog) self.total = len(self._ids) diff --git a/qt/po/scripts/rewrite-refs.py b/qt/po/scripts/rewrite-refs.py index b6ac9d545..9d9803c7d 100644 --- a/qt/po/scripts/rewrite-refs.py +++ b/qt/po/scripts/rewrite-refs.py @@ -3,11 +3,11 @@ import glob, re, json, stringcase files = ( - # glob.glob("../../pylib/**/*.py", recursive=True) - # glob.glob("../../qt/**/*.py", recursive=True) - glob.glob("../../qt/**/forms/*.ui", recursive=True) + glob.glob("../../pylib/**/*.py", recursive=True) + # + glob.glob("../../qt/**/*.py", recursive=True) + # glob.glob("../../qt/**/forms/*.ui", recursive=True) ) -string_re = re.compile(r"(.*?)") +string_re = re.compile(r'ngettext\(\s*"(.+?)",\s+".+?",\s+(.+?)\) % \2') map = json.load(open("keys_by_text.json")) @@ -19,6 +19,7 @@ blacklist = { "Show %s", "~", "about:blank", + "%d card imported.", # previewer.py needs updating to fix these "Shortcut key: R", "Shortcut key: B", @@ -52,16 +53,28 @@ def decode_ents(html): return reEnts.sub(fixup, html) +def munge_key(key): + if key == "browsing-note": + return "browsing-note-count" + if key == "card-templates-card": + return "card-templates-card-count" + return key + + def repl(m): + print(m.group(0)) text = decode_ents(m.group(1)) if text in blacklist: return m.group(0) (module, key) = map[text] - screaming = stringcase.constcase(key) - print(screaming) + key = munge_key(key) - return f"{screaming}" + screaming = stringcase.constcase(key) + + ret = f"tr_legacyglobal(TR.{screaming}, count={m.group(2)})" + print(ret) + return ret for file in files: @@ -70,4 +83,8 @@ for file in files: buf = open(file).read() buf2 = string_re.sub(repl, buf) if buf != buf2: + lines = buf2.split("\n") + lines.insert(3, "from anki.rsbackend import TR") + lines.insert(3, "from anki.lang import tr_legacyglobal") + buf2 = "\n".join(lines) open(file, "w").write(buf2)