2015-07-29 00:26:23 +05:30
|
|
|
#! /usr/bin/env python3
|
2012-03-12 22:13:12 +05:30
|
|
|
#
|
2012-03-15 22:41:31 +05:30
|
|
|
# update_po - a gramps tool to update translations
|
2012-03-12 22:13:12 +05:30
|
|
|
#
|
|
|
|
# Copyright (C) 2006-2006 Kees Bakker
|
2012-03-14 19:40:21 +05:30
|
|
|
# Copyright (C) 2006 Brian Matherly
|
|
|
|
# Copyright (C) 2008 Stephen George
|
2012-03-12 22:13:12 +05:30
|
|
|
# Copyright (C) 2012
|
2020-08-22 02:57:23 +05:30
|
|
|
# Copyright (C) 2020 Nick Hall
|
2012-03-12 22:13:12 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2014-08-09 07:59:07 +05:30
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2013-05-06 01:11:13 +05:30
|
|
|
#
|
2012-10-16 14:21:05 +05:30
|
|
|
|
|
|
|
"""
|
|
|
|
update_po.py for Gramps translations.
|
|
|
|
|
2016-05-18 15:17:34 +05:30
|
|
|
Examples:
|
2012-10-16 14:21:05 +05:30
|
|
|
python update_po.py -t
|
|
|
|
|
|
|
|
Tests if 'gettext' and 'python' are well configured.
|
|
|
|
|
|
|
|
python update_po.py -h
|
|
|
|
|
|
|
|
Calls help and command line interface.
|
|
|
|
|
|
|
|
python update_po.py -p
|
|
|
|
|
|
|
|
Generates a new template/catalog (gramps.pot).
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-10-16 14:21:05 +05:30
|
|
|
python update_po.py -m de.po
|
|
|
|
|
|
|
|
Merges 'de.po' file with 'gramps.pot'.
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-10-16 14:21:05 +05:30
|
|
|
python update_po.py -k de.po
|
|
|
|
|
|
|
|
Checks 'de.po' file, tests to compile and generates a textual resume.
|
|
|
|
"""
|
|
|
|
|
2012-10-03 17:46:06 +05:30
|
|
|
from __future__ import print_function
|
2012-03-12 22:13:12 +05:30
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2019-01-01 23:03:52 +05:30
|
|
|
import shutil
|
2012-12-01 02:03:44 +05:30
|
|
|
from argparse import ArgumentParser
|
2019-06-14 03:54:11 +05:30
|
|
|
from tokenize import tokenize, STRING, COMMENT, NL, TokenError
|
2023-07-31 19:10:59 +05:30
|
|
|
|
2012-12-14 02:13:32 +05:30
|
|
|
# Windows OS
|
2012-03-12 22:13:12 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
if sys.platform in ["linux", "linux2", "darwin", "cygwin"] or shutil.which("msgmerge"):
|
|
|
|
msgmergeCmd = "msgmerge"
|
|
|
|
msgfmtCmd = "msgfmt"
|
|
|
|
msgattribCmd = "msgattrib"
|
|
|
|
xgettextCmd = "xgettext"
|
|
|
|
pythonCmd = os.path.join(sys.prefix, "bin", "python3")
|
|
|
|
elif sys.platform == "win32":
|
2012-03-15 22:41:31 +05:30
|
|
|
# GetText Win 32 obtained from http://gnuwin32.sourceforge.net/packages/gettext.htm
|
|
|
|
# ....\gettext\bin\msgmerge.exe needs to be on the path
|
2023-07-31 19:10:59 +05:30
|
|
|
msgmergeCmd = os.path.join(
|
|
|
|
"C:", "Program Files(x86)", "gettext", "bin", "msgmerge.exe"
|
|
|
|
)
|
|
|
|
msgfmtCmd = os.path.join("C:", "Program Files(x86)", "gettext", "bin", "msgfmt.exe")
|
|
|
|
msgattribCmd = os.path.join(
|
|
|
|
"C:", "Program Files(x86)", "gettext", "bin", "msgattrib.exe"
|
|
|
|
)
|
|
|
|
xgettextCmd = os.path.join(
|
|
|
|
"C:", "Program Files(x86)", "gettext", "bin", "xgettext.exe"
|
|
|
|
)
|
|
|
|
pythonCmd = os.path.join(sys.prefix, "bin", "python.exe")
|
2012-12-14 02:13:32 +05:30
|
|
|
|
|
|
|
# Others OS
|
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
elif sys.platform in ["linux", "linux2", "darwin", "cygwin"]:
|
|
|
|
msgmergeCmd = "msgmerge"
|
|
|
|
msgfmtCmd = "msgfmt"
|
|
|
|
msgattribCmd = "msgattrib"
|
|
|
|
xgettextCmd = "xgettext"
|
|
|
|
pythonCmd = os.path.join(sys.prefix, "bin", "python3")
|
2012-10-03 17:46:06 +05:30
|
|
|
else:
|
2013-01-21 01:05:35 +05:30
|
|
|
print("Found platform %s, OS %s" % (sys.platform, os.name))
|
2023-07-31 19:10:59 +05:30
|
|
|
print("Update PO ERROR: unknown system, don't know msgmerge, ... commands")
|
2012-10-03 17:46:06 +05:30
|
|
|
sys.exit(0)
|
2012-12-14 02:13:32 +05:30
|
|
|
|
|
|
|
# List of available languages, useful for grouped actions
|
|
|
|
|
|
|
|
# need files with po extension
|
2023-07-31 19:10:59 +05:30
|
|
|
LANG = [file for file in os.listdir(".") if file.endswith(".po")]
|
2012-12-14 02:13:32 +05:30
|
|
|
# add a special 'all' argument (for 'check' and 'merge' arguments)
|
2012-12-02 22:52:40 +05:30
|
|
|
LANG.append("all")
|
2012-12-14 02:13:32 +05:30
|
|
|
# visual polish on the languages list
|
2012-12-02 22:52:40 +05:30
|
|
|
LANG.sort()
|
2012-03-15 22:41:31 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
def tests():
|
|
|
|
"""
|
|
|
|
Testing installed programs.
|
|
|
|
We made tests (-t flag) by displaying versions of tools if properly
|
|
|
|
installed. Cannot run all commands without 'gettext' and 'python'.
|
|
|
|
"""
|
|
|
|
try:
|
2023-07-31 19:10:59 +05:30
|
|
|
print("\n====='msgmerge'=(merge our translation)================\n")
|
|
|
|
os.system("""%(program)s -V""" % {"program": msgmergeCmd})
|
2012-03-15 22:41:31 +05:30
|
|
|
except:
|
2023-07-31 19:10:59 +05:30
|
|
|
print(
|
|
|
|
"Please, install %(program)s for updating your translation"
|
|
|
|
% {"program": msgmergeCmd}
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
try:
|
2023-07-31 19:10:59 +05:30
|
|
|
print("\n==='msgfmt'=(format our translation for installation)==\n")
|
|
|
|
os.system("""%(program)s -V""" % {"program": msgfmtCmd})
|
2012-03-15 22:41:31 +05:30
|
|
|
except:
|
2023-07-31 19:10:59 +05:30
|
|
|
print(
|
|
|
|
"Please, install %(program)s for checking your translation"
|
|
|
|
% {"program": msgfmtCmd}
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
try:
|
2023-07-31 19:10:59 +05:30
|
|
|
print("\n===='msgattrib'==(list groups of messages)=============\n")
|
|
|
|
os.system("""%(program)s -V""" % {"program": msgattribCmd})
|
2012-03-19 21:30:19 +05:30
|
|
|
except:
|
2023-07-31 19:10:59 +05:30
|
|
|
print(
|
|
|
|
"Please, install %(program)s for listing groups of messages"
|
|
|
|
% {"program": msgattribCmd}
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-19 21:30:19 +05:30
|
|
|
try:
|
|
|
|
print("\n===='xgettext' =(generate a new template)==============\n")
|
2023-07-31 19:10:59 +05:30
|
|
|
os.system("""%(program)s -V""" % {"program": xgettextCmd})
|
2012-03-17 00:05:35 +05:30
|
|
|
except:
|
2023-07-31 19:10:59 +05:30
|
|
|
print(
|
|
|
|
"Please, install %(program)s for generating a new template"
|
|
|
|
% {"program": xgettextCmd}
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-17 00:05:35 +05:30
|
|
|
try:
|
2012-03-19 21:30:19 +05:30
|
|
|
print("\n=================='python'=============================\n")
|
2023-07-31 19:10:59 +05:30
|
|
|
os.system("""%(program)s -V""" % {"program": pythonCmd})
|
2012-03-15 22:41:31 +05:30
|
|
|
except:
|
2023-07-31 19:10:59 +05:30
|
|
|
print("Please, install python")
|
|
|
|
|
2012-12-14 02:13:32 +05:30
|
|
|
|
2012-03-12 22:13:12 +05:30
|
|
|
def main():
|
2012-03-15 22:41:31 +05:30
|
|
|
"""
|
|
|
|
The utility for handling translation stuff.
|
|
|
|
What is need by Gramps, nothing more.
|
|
|
|
"""
|
2016-04-19 04:45:05 +05:30
|
|
|
|
|
|
|
parser = ArgumentParser(
|
2023-07-31 19:10:59 +05:30
|
|
|
description="This program generates a new template and "
|
|
|
|
"also provides some common features.",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-t",
|
|
|
|
"--test",
|
|
|
|
action="store_true",
|
|
|
|
dest="test",
|
|
|
|
default=True,
|
|
|
|
help="test if 'python' and 'gettext' are properly installed",
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
"-x",
|
|
|
|
"--xml",
|
|
|
|
action="store_true",
|
|
|
|
dest="xml",
|
|
|
|
default=False,
|
|
|
|
help="extract messages from xml based file formats",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-g",
|
|
|
|
"--glade",
|
|
|
|
action="store_true",
|
|
|
|
dest="glade",
|
|
|
|
default=False,
|
|
|
|
help="extract messages from glade file format only",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--clean",
|
|
|
|
action="store_true",
|
|
|
|
dest="clean",
|
|
|
|
default=False,
|
|
|
|
help="remove created files",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--pot",
|
|
|
|
action="store_true",
|
|
|
|
dest="catalog",
|
|
|
|
default=False,
|
|
|
|
help="create a new catalog",
|
|
|
|
)
|
|
|
|
|
|
|
|
update = parser.add_argument_group("Update", "Maintenance around translations")
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
# need at least one argument (sv.po, de.po, etc ...)
|
2012-12-01 02:03:44 +05:30
|
|
|
|
2016-04-19 04:45:05 +05:30
|
|
|
# lang.po files maintenance
|
2023-07-31 19:10:59 +05:30
|
|
|
update.add_argument(
|
|
|
|
"-m", dest="merge", choices=LANG, help="merge lang.po files with last catalog"
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
update.add_argument("-k", dest="check", choices=LANG, help="check lang.po files")
|
2016-04-19 04:45:05 +05:30
|
|
|
|
|
|
|
# testing stage
|
2023-07-31 19:10:59 +05:30
|
|
|
trans = parser.add_argument_group(
|
|
|
|
"Translation", "Display content of translations file"
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 02:03:44 +05:30
|
|
|
# need one argument (eg, de.po)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
trans.add_argument(
|
|
|
|
"-u",
|
|
|
|
dest="untranslated",
|
|
|
|
choices=[file for file in os.listdir(".") if file.endswith(".po")],
|
|
|
|
help="list untranslated messages",
|
|
|
|
)
|
|
|
|
trans.add_argument(
|
|
|
|
"-f",
|
|
|
|
dest="fuzzy",
|
|
|
|
choices=[file for file in os.listdir(".") if file.endswith(".po")],
|
|
|
|
help="list fuzzy messages",
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 02:03:44 +05:30
|
|
|
args = parser.parse_args()
|
2012-12-01 15:15:14 +05:30
|
|
|
namespace, extra = parser.parse_known_args()
|
2012-12-01 02:03:44 +05:30
|
|
|
|
|
|
|
if args.test:
|
2012-03-15 22:41:31 +05:30
|
|
|
tests()
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 02:03:44 +05:30
|
|
|
if args.xml:
|
2012-03-15 22:41:31 +05:30
|
|
|
extract_xml()
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 02:03:44 +05:30
|
|
|
if args.glade:
|
2012-10-03 21:25:36 +05:30
|
|
|
create_filesfile()
|
2012-03-15 22:41:31 +05:30
|
|
|
extract_glade()
|
2023-07-31 19:10:59 +05:30
|
|
|
if os.path.isfile("tmpfiles"):
|
|
|
|
os.unlink("tmpfiles")
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 02:03:44 +05:30
|
|
|
if args.catalog:
|
2012-03-15 22:41:31 +05:30
|
|
|
retrieve()
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 02:03:44 +05:30
|
|
|
if args.clean:
|
2012-03-15 22:41:31 +05:30
|
|
|
clean()
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 15:40:44 +05:30
|
|
|
if args.merge:
|
2023-07-31 19:10:59 +05:30
|
|
|
# retrieve() windows os?
|
|
|
|
if sys.argv[2:] == ["all"]:
|
2012-12-02 22:52:40 +05:30
|
|
|
sys.argv[2:] = LANG
|
2012-12-01 02:03:44 +05:30
|
|
|
merge(sys.argv[2:])
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 15:40:44 +05:30
|
|
|
if args.check:
|
2023-07-31 19:10:59 +05:30
|
|
|
# retrieve() windows os?
|
|
|
|
if sys.argv[2:] == ["all"]:
|
2012-12-02 22:52:40 +05:30
|
|
|
sys.argv[2:] = LANG
|
2012-12-01 02:03:44 +05:30
|
|
|
check(sys.argv[2:])
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 15:46:44 +05:30
|
|
|
if args.untranslated:
|
2012-12-01 02:03:44 +05:30
|
|
|
untranslated(sys.argv[2:])
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-12-01 15:46:44 +05:30
|
|
|
if args.fuzzy:
|
2012-12-01 02:03:44 +05:30
|
|
|
fuzzy(sys.argv[2:])
|
2012-10-03 21:07:44 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
|
2012-10-03 21:07:44 +05:30
|
|
|
def create_filesfile():
|
|
|
|
"""
|
|
|
|
Create a file with all files that we should translate.
|
2016-04-19 04:45:05 +05:30
|
|
|
These are all python files not in POTFILES.skip added with those in
|
2012-10-03 21:07:44 +05:30
|
|
|
POTFILES.in
|
|
|
|
"""
|
|
|
|
dir = os.getcwd()
|
2023-07-31 19:10:59 +05:30
|
|
|
topdir = os.path.normpath(os.path.join(dir, "..", "gramps"))
|
2012-10-03 21:07:44 +05:30
|
|
|
lentopdir = len(topdir)
|
2023-07-31 19:10:59 +05:30
|
|
|
with open("POTFILES.in") as f:
|
|
|
|
infiles = dict(
|
|
|
|
["../" + file.strip(), None]
|
|
|
|
for file in f
|
|
|
|
if file.strip() and not file[0] == "#"
|
|
|
|
)
|
|
|
|
|
|
|
|
with open("POTFILES.skip") as f:
|
|
|
|
notinfiles = dict(
|
|
|
|
["../" + file.strip(), None] for file in f if file and not file[0] == "#"
|
|
|
|
)
|
|
|
|
|
|
|
|
for dirpath, dirnames, filenames in os.walk(topdir):
|
|
|
|
root, subdir = os.path.split(dirpath)
|
|
|
|
if subdir.startswith("."):
|
|
|
|
# don't continue in this dir
|
|
|
|
dirnames[:] = []
|
|
|
|
continue
|
|
|
|
for dirname in dirnames:
|
|
|
|
# Skip hidden and system directories:
|
|
|
|
if dirname.startswith(".") or dirname in ["po", "locale"]:
|
|
|
|
dirnames.remove(dirname)
|
|
|
|
# add the files which are python or glade files
|
|
|
|
# if the directory does not exist or is a link, do nothing
|
|
|
|
if not os.path.isdir(dirpath) or os.path.islink(dirpath):
|
|
|
|
continue
|
|
|
|
|
|
|
|
for filename in os.listdir(dirpath):
|
|
|
|
name = os.path.split(filename)[1]
|
|
|
|
if name.endswith(".py") or name.endswith(".glade"):
|
|
|
|
full_filename = os.path.join(dirpath, filename)
|
|
|
|
# Skip the file if in POTFILES.skip
|
|
|
|
if full_filename[lentopdir:] in notinfiles:
|
|
|
|
infiles["../gramps" + full_filename[lentopdir:]] = None
|
|
|
|
# now we write out all the files in form ../gramps/filename
|
|
|
|
with open("tmpfiles", "w") as f:
|
2016-04-19 04:38:21 +05:30
|
|
|
for file in sorted(infiles.keys()):
|
|
|
|
f.write(file)
|
2023-07-31 19:10:59 +05:30
|
|
|
f.write("\n")
|
|
|
|
|
2012-10-03 21:07:44 +05:30
|
|
|
|
2012-10-03 17:46:06 +05:30
|
|
|
def listing(name, extensionlist):
|
2012-03-15 22:41:31 +05:30
|
|
|
"""
|
|
|
|
List files according to extensions.
|
|
|
|
Parsing from a textual file (gramps) is faster and easy for maintenance.
|
|
|
|
Like POTFILES.in and POTFILES.skip
|
|
|
|
"""
|
2016-04-19 04:38:21 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
with open("tmpfiles") as f:
|
|
|
|
files = [file.strip() for file in f if file and not file[0] == "#"]
|
2016-04-19 04:38:21 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
with open(name, "w") as temp:
|
2016-04-19 04:38:21 +05:30
|
|
|
for entry in files:
|
|
|
|
for ext in extensionlist:
|
|
|
|
if entry.endswith(ext):
|
|
|
|
temp.write(entry)
|
2023-07-31 19:10:59 +05:30
|
|
|
temp.write("\n")
|
2016-04-19 04:38:21 +05:30
|
|
|
break
|
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
def headers():
|
|
|
|
"""
|
|
|
|
Look at existing C file format headers.
|
2016-04-19 04:45:05 +05:30
|
|
|
Generated by 'intltool-extract' but want to get rid of this
|
2012-03-15 22:41:31 +05:30
|
|
|
dependency (perl, just a set of tools).
|
|
|
|
"""
|
|
|
|
headers = []
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
# in.h; extract_xml
|
2023-07-31 19:10:59 +05:30
|
|
|
if os.path.isfile("""fragments.pot"""):
|
|
|
|
headers.append("""fragments.pot""")
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
return headers
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
def extract_xml():
|
|
|
|
"""
|
2020-08-22 02:57:23 +05:30
|
|
|
Extract translation strings from XML based, mime and desktop files.
|
|
|
|
Uses custom ITS rules found in the po/its directory.
|
2012-03-15 22:41:31 +05:30
|
|
|
"""
|
2023-07-31 19:10:59 +05:30
|
|
|
if not os.path.isfile("gramps.pot"):
|
2020-08-22 02:57:23 +05:30
|
|
|
create_template()
|
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
for input_file in [
|
|
|
|
"../data/holidays.xml",
|
|
|
|
"../data/tips.xml",
|
|
|
|
"../data/org.gramps_project.Gramps.xml.in",
|
|
|
|
"../data/org.gramps_project.Gramps.appdata.xml.in",
|
|
|
|
"../data/org.gramps_project.Gramps.desktop.in",
|
|
|
|
]:
|
|
|
|
os.system(
|
|
|
|
(
|
|
|
|
"GETTEXTDATADIR=. %(xgettext)s -F -j "
|
|
|
|
"-o gramps.pot --from-code=UTF-8 %(inputfile)s"
|
|
|
|
)
|
|
|
|
% {"xgettext": xgettextCmd, "inputfile": input_file}
|
|
|
|
)
|
|
|
|
print(input_file)
|
|
|
|
|
2016-04-19 04:38:21 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
def create_template():
|
|
|
|
"""
|
|
|
|
Create a new file for template, if it does not exist.
|
|
|
|
"""
|
2023-07-31 19:10:59 +05:30
|
|
|
with open("gramps.pot", "w") as template:
|
2016-04-19 04:45:05 +05:30
|
|
|
pass
|
2016-04-19 04:38:21 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
def extract_glade():
|
|
|
|
"""
|
|
|
|
Extract messages from a temp file with all .glade
|
|
|
|
"""
|
2023-07-31 19:10:59 +05:30
|
|
|
if not os.path.isfile("gramps.pot"):
|
2012-03-15 22:41:31 +05:30
|
|
|
create_template()
|
2012-03-12 22:13:12 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
listing("glade.txt", [".glade"])
|
|
|
|
os.system(
|
|
|
|
"""%(xgettext)s -F --add-comments -j -L Glade """
|
|
|
|
"""--from-code=UTF-8 -o gramps.pot --files-from=glade.txt"""
|
|
|
|
% {"xgettext": xgettextCmd}
|
|
|
|
)
|
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
|
2019-06-14 03:54:11 +05:30
|
|
|
def xml_fragments():
|
2023-07-31 19:10:59 +05:30
|
|
|
"""search through the file for xml fragments that contain the
|
2019-06-14 03:54:11 +05:30
|
|
|
'translate="yes">string<' pattern. These need to be added to the message
|
2023-07-31 19:10:59 +05:30
|
|
|
catalog"""
|
|
|
|
with open("tmpfiles") as __f:
|
|
|
|
files = [
|
|
|
|
file.strip()
|
|
|
|
for file in __f
|
|
|
|
if file and not (file[0] == "#") and file.endswith(".py\n")
|
|
|
|
]
|
2019-06-14 03:54:11 +05:30
|
|
|
print("Checking for XML fragments in Python files")
|
|
|
|
modop = int(len(files) / 20)
|
2023-07-31 19:10:59 +05:30
|
|
|
wfp = open("fragments.pot", "w", encoding="utf-8")
|
2023-07-26 22:02:11 +05:30
|
|
|
wfp.write('msgid ""\n')
|
|
|
|
wfp.write('msgstr ""\n')
|
|
|
|
wfp.write('"Content-Type: text/plain; charset=UTF-8\\n"\n\n')
|
2019-06-14 03:54:11 +05:30
|
|
|
for indx, filename in enumerate(files):
|
|
|
|
if not indx % modop:
|
2023-07-31 19:10:59 +05:30
|
|
|
print(int(indx / len(files) * 100), end="\r")
|
|
|
|
fp = open(filename, "rb")
|
2019-06-14 03:54:11 +05:30
|
|
|
try:
|
|
|
|
tokens = tokenize(fp.readline)
|
|
|
|
in_string = False
|
|
|
|
for _token, _text, _start, _end, _line in tokens:
|
|
|
|
if _text.startswith('"""') or _text.startswith("'''"):
|
|
|
|
_text = _text[3:]
|
|
|
|
elif _text.startswith('"') or _text.startswith("'"):
|
|
|
|
_text = _text[1:]
|
|
|
|
if _text.endswith('"""') or _text.endswith("'''"):
|
|
|
|
_text = _text[:-3]
|
|
|
|
elif _text.endswith('"') or _text.endswith("'"):
|
|
|
|
_text = _text[:-1]
|
|
|
|
if _token == STRING and not in_string:
|
|
|
|
in_string = True
|
|
|
|
line_no = _start[0]
|
|
|
|
text = _text
|
|
|
|
continue
|
|
|
|
elif _token == STRING and in_string:
|
|
|
|
text += _text
|
|
|
|
continue
|
|
|
|
elif _token == COMMENT or _token == NL and in_string:
|
|
|
|
# need to ignore comments and concatinate strings
|
|
|
|
_ml = True
|
|
|
|
continue
|
|
|
|
elif in_string:
|
|
|
|
in_string = False
|
|
|
|
end = 0
|
|
|
|
# _find_message_in_xml(text)
|
|
|
|
while True:
|
|
|
|
fnd = text.find('translatable="yes">', end)
|
|
|
|
if fnd == -1:
|
|
|
|
break
|
2023-07-31 19:10:59 +05:30
|
|
|
end = text.find("<", fnd)
|
2019-06-14 03:54:11 +05:30
|
|
|
if end == -1:
|
2023-07-31 19:10:59 +05:30
|
|
|
print(
|
|
|
|
"\nBad xml fragment '%s' at %s line %d"
|
|
|
|
% (text[fnd:], filename, _start[0])
|
|
|
|
)
|
2019-06-14 03:54:11 +05:30
|
|
|
break
|
|
|
|
msg = text[fnd + 19 : end]
|
2023-07-31 19:10:59 +05:30
|
|
|
if "%s" in msg or (msg.startswith("{") and msg.endswith("}")):
|
|
|
|
print(
|
|
|
|
"\n#: %s:%d Are you sure you want to "
|
|
|
|
'translate the "%%s"???' % (filename, line_no)
|
|
|
|
)
|
2019-06-14 03:54:11 +05:30
|
|
|
break
|
2023-07-31 19:10:59 +05:30
|
|
|
wfp.write(
|
|
|
|
'#: %s:%d\nmsgid "%s"\nmsgstr ""\n'
|
|
|
|
% (filename, line_no, msg)
|
|
|
|
)
|
2019-06-14 03:54:11 +05:30
|
|
|
except TokenError as e:
|
2023-07-31 19:10:59 +05:30
|
|
|
print(
|
|
|
|
"\n%s: %s, line %d, column %d"
|
|
|
|
% (e.args[0], filename, e.args[1][0], e.args[1][1]),
|
|
|
|
file=sys.stderr,
|
|
|
|
)
|
2019-06-14 03:54:11 +05:30
|
|
|
finally:
|
|
|
|
fp.close()
|
|
|
|
wfp.close()
|
|
|
|
|
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
def retrieve():
|
|
|
|
"""
|
|
|
|
Extract messages from all files used by Gramps (python, glade, xml)
|
|
|
|
"""
|
2013-05-04 21:24:14 +05:30
|
|
|
create_template()
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-10-03 21:25:36 +05:30
|
|
|
create_filesfile()
|
2019-06-14 03:54:11 +05:30
|
|
|
xml_fragments()
|
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
listing("python.txt", [".py", ".py.in"])
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2013-05-06 01:11:13 +05:30
|
|
|
# additional keywords must always be kept in sync with those in genpot.sh
|
2023-07-31 19:10:59 +05:30
|
|
|
os.system(
|
|
|
|
"""%(xgettext)s -F --add-comments=Translators -j """
|
|
|
|
"""--directory=./ -d gramps -L Python """
|
|
|
|
"""-o gramps.pot --files-from=python.txt """
|
|
|
|
"""--debug --keyword=_ --keyword=ngettext """
|
|
|
|
"""--keyword=_T_ --keyword=trans_text:1,2c """
|
|
|
|
"""--keyword=_:1,2c --keyword=_T_:1,2c """
|
|
|
|
"""--keyword=sgettext --from-code=UTF-8""" % {"xgettext": xgettextCmd}
|
|
|
|
)
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
extract_glade()
|
2020-08-22 02:57:23 +05:30
|
|
|
extract_xml()
|
2016-04-19 04:45:05 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
# C format header (.h extension)
|
|
|
|
for h in headers():
|
2023-07-31 19:10:59 +05:30
|
|
|
print("xgettext for %s" % h)
|
|
|
|
os.system(
|
|
|
|
"""%(xgettext)s -F --add-comments=Translators -j """
|
|
|
|
"""-o gramps.pot --keyword=N_ --from-code=UTF-8 %(head)s"""
|
|
|
|
% {"xgettext": xgettextCmd, "head": h}
|
|
|
|
)
|
2012-03-15 22:41:31 +05:30
|
|
|
clean()
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
|
2012-03-15 22:41:31 +05:30
|
|
|
def clean():
|
|
|
|
"""
|
|
|
|
Remove created files (C format headers, temp listings)
|
|
|
|
"""
|
|
|
|
for h in headers():
|
|
|
|
if os.path.isfile(h):
|
2012-03-17 11:30:16 +05:30
|
|
|
os.unlink(h)
|
2023-07-31 19:10:59 +05:30
|
|
|
print("Remove %(head)s" % {"head": h})
|
|
|
|
|
|
|
|
if os.path.isfile("python.txt"):
|
|
|
|
os.unlink("python.txt")
|
|
|
|
print("Remove 'python.txt'")
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
if os.path.isfile("glade.txt"):
|
|
|
|
os.unlink("glade.txt")
|
|
|
|
print("Remove 'glade.txt'")
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2023-07-31 19:10:59 +05:30
|
|
|
if os.path.isfile("tmpfiles"):
|
|
|
|
os.unlink("tmpfiles")
|
|
|
|
print("Remove 'tmpfiles'")
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2012-10-03 21:08:42 +05:30
|
|
|
|
2012-12-02 22:52:40 +05:30
|
|
|
def merge(args):
|
2012-03-15 22:41:31 +05:30
|
|
|
"""
|
|
|
|
Merge messages with 'gramps.pot'
|
|
|
|
"""
|
2012-12-02 22:52:40 +05:30
|
|
|
for arg in args:
|
2023-07-31 19:10:59 +05:30
|
|
|
if arg == "all":
|
2016-04-19 04:45:05 +05:30
|
|
|
continue
|
2023-07-31 19:10:59 +05:30
|
|
|
print("Merge %(lang)s with current template" % {"lang": arg})
|
|
|
|
os.system(
|
|
|
|
"""%(msgmerge)s -U %(lang)s gramps.pot"""
|
|
|
|
% {"msgmerge": msgmergeCmd, "lang": arg}
|
|
|
|
)
|
|
|
|
print("Updated file: '%(lang)s'." % {"lang": arg})
|
|
|
|
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2012-12-02 22:52:40 +05:30
|
|
|
def check(args):
|
2012-03-15 22:41:31 +05:30
|
|
|
"""
|
|
|
|
Check the translation file
|
|
|
|
"""
|
2012-12-02 22:52:40 +05:30
|
|
|
for arg in args:
|
2023-07-31 19:10:59 +05:30
|
|
|
if arg == "all":
|
2012-12-02 22:52:40 +05:30
|
|
|
continue
|
2023-07-31 19:10:59 +05:30
|
|
|
print(
|
|
|
|
"Checked file: '%(lang.po)s'. See '%(txt)s.txt'."
|
|
|
|
% {"lang.po": arg, "txt": arg[:-3]}
|
|
|
|
)
|
|
|
|
os.system(
|
|
|
|
"""%(python)s ./check_po -s %(lang.po)s > %(lang)s.txt"""
|
|
|
|
% {"python": pythonCmd, "lang.po": arg, "lang": arg[:-3]}
|
|
|
|
)
|
|
|
|
os.system(
|
|
|
|
"""%(msgfmt)s -c -v %(lang.po)s""" % {"msgfmt": msgfmtCmd, "lang.po": arg}
|
|
|
|
)
|
|
|
|
|
2012-03-12 22:13:12 +05:30
|
|
|
|
2012-12-01 15:46:44 +05:30
|
|
|
def untranslated(arg):
|
2012-03-19 21:30:19 +05:30
|
|
|
"""
|
|
|
|
List untranslated messages
|
|
|
|
"""
|
2023-07-31 19:10:59 +05:30
|
|
|
os.system(
|
|
|
|
"""%(msgattrib)s --untranslated %(lang.po)s"""
|
|
|
|
% {"msgattrib": msgattribCmd, "lang.po": arg[0]}
|
|
|
|
)
|
|
|
|
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2012-12-01 15:46:44 +05:30
|
|
|
def fuzzy(arg):
|
2012-03-19 21:30:19 +05:30
|
|
|
"""
|
|
|
|
List fuzzy messages
|
|
|
|
"""
|
2023-07-31 19:10:59 +05:30
|
|
|
os.system(
|
|
|
|
"""%(msgattrib)s --only-fuzzy --no-obsolete %(lang.po)s"""
|
|
|
|
% {"msgattrib": msgattribCmd, "lang.po": arg[0]}
|
|
|
|
)
|
|
|
|
|
2012-10-03 17:46:06 +05:30
|
|
|
|
2012-03-12 22:13:12 +05:30
|
|
|
if __name__ == "__main__":
|
2016-01-01 02:47:35 +05:30
|
|
|
main()
|