From 7bc87c7f0b90b33893d585b4aa647d2b13f02907 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Sat, 16 Mar 2019 23:40:01 -0700 Subject: [PATCH] filter-repo: enable internationalization Assuming filter-repo will be merged into git.git, use "git" for the TEXTDOMAIN, and assume its build system will replace "@@LOCALEDIR@@" appropriately. Note that the xgettext command used to grab string translations is nearly identical to the one for C files in git.git; just use --language=python instead and add --join-existing to avoid overwriting the po/git.pot file. In other words, use the command: xgettext -o../git/po/git.pot --join-existing --force-po \ --add-comments=TRANSLATORS: \ --msgid-bugs-address="Git Mailing List " \ --from-code=UTF-8 --language=python \ --keyword=_ --keyword=N_ --keyword="Q_:1,2" \ git-filter-repo To create or update the translation, go to git.git/po and run either of: msginit --locale=XX msgmerge --add-location --backup=off -U XX.po git.pot Once you've updated the translation, within git.git just build as normal. That's all that's needed. Signed-off-by: Elijah Newren --- git-filter-repo | 26 ++++++++++++++++++++++++++ t/t9390-filter-repo.sh | 2 +- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/git-filter-repo b/git-filter-repo index 270716e..3e69a95 100755 --- a/git-filter-repo +++ b/git-filter-repo @@ -35,6 +35,7 @@ from __future__ import print_function import argparse import collections import fnmatch +import gettext import os import re import shutil @@ -51,6 +52,30 @@ __all__ = ["Blob", "Reset", "FileChanges", "Commit", "Tag", "Progress", "string_to_date", "date_to_string", "record_id_rename", "GitUtils", "FilteringOptions", "RepoFilter"] +def gettext_poison(msg): + if "GIT_TEST_GETTEXT_POISON" in os.environ: # pragma: no cover + return "# GETTEXT POISON #" + return gettext.gettext(msg) + +_ = gettext_poison + +def setup_gettext(): + TEXTDOMAIN="git" + podir = os.environ.get("GIT_TEXTDOMAINDIR") or "@@LOCALEDIR@@" + if not os.path.isdir(podir): # pragma: no cover + podir = None # Python has its own fallback; use that + + ## This looks like the most straightforward translation of the relevant + ## code in git.git:gettext.c and git.git:perl/Git/I18n.pm: + #import locale + #locale.setlocale(locale.LC_MESSAGES, ""); + #locale.setlocale(locale.LC_TIME, ""); + #locale.textdomain(TEXTDOMAIN); + #locale.bindtextdomain(TEXTDOMAIN, podir); + ## but the python docs suggest using the gettext module (which doesn't + ## have setlocale()) instead, so: + gettext.textdomain(TEXTDOMAIN); + gettext.bindtextdomain(TEXTDOMAIN, podir); def _timedelta_to_seconds(delta): """ @@ -3116,6 +3141,7 @@ class RepoFilter(object): print("Completely finished after {:.2f} seconds.".format(time.time()-start)) if __name__ == '__main__': + setup_gettext() args = FilteringOptions.parse_args(sys.argv[1:]) if args.analyze: RepoAnalyze.run(args) diff --git a/t/t9390-filter-repo.sh b/t/t9390-filter-repo.sh index 504237c..43731bd 100755 --- a/t/t9390-filter-repo.sh +++ b/t/t9390-filter-repo.sh @@ -415,7 +415,7 @@ test_expect_success 'setup analyze_me' ' ) ' -test_expect_success '--analyze' ' +test_expect_success C_LOCALE_OUTPUT '--analyze' ' ( cd analyze_me &&