From 12123d6d52987bfde261695ea42c8707d0717735 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Tue, 15 Nov 2022 15:31:49 +0000 Subject: [PATCH 01/13] languages: create new script update_po.py The script should do the same work as when one run update_po.sh The Python script can be run on Windows too. --- lang/update-po.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 lang/update-po.py diff --git a/lang/update-po.py b/lang/update-po.py new file mode 100644 index 000000000..1a710b04e --- /dev/null +++ b/lang/update-po.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +""" +Portable script to update po files on most platforms +Make sure the .pot file is up-to-date first by +by calling within the ./po folder: +python ../lang-extract.py --no-missing -s -o Firmware.pot ../../Firmware/[a-zA-Z]*.[ch]* ../../Firmware/mmu2/[a-zA-Z]*.[ch]* +""" + +import sys +from pathlib import Path +import polib +from polib import POFile + +BASE_DIR: Path = Path.cwd() +PO_DIR: Path = BASE_DIR / "po" +PO_FILE_LIST: list[Path] = [] +POT_REFERENCE: POFile = polib.pofile(PO_DIR/'Firmware.pot') + + +def main(): + PO_FILE_LIST = sorted(PO_DIR.glob('**/*.po')) + + for po_file in PO_FILE_LIST: + po = polib.pofile(po_file) + po.merge(POT_REFERENCE) + po.save(po_file) + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + sys.exit(-1) From 65d7605623c2ea9aff4a87befb622e2419678311 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Tue, 15 Nov 2022 15:36:10 +0000 Subject: [PATCH 02/13] languages: update lang-extract.py Fix an issue where it does not run on Windows. When the input argument is regex, we must first search for the files using said regex pattern and then creating a list of files. When opening the files, the encoding needs to be utf-8. --- lang/lang-extract.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/lang/lang-extract.py b/lang/lang-extract.py index f0ba84356..73c2e20ce 100755 --- a/lang/lang-extract.py +++ b/lang/lang-extract.py @@ -6,6 +6,10 @@ import polib import regex import sys import lib.charset as cs +from pathlib import Path, PurePosixPath + +BASE_DIR: Path = Path.cwd() +FILE_LIST: list[Path] = [] def line_warning(path, line, msg): print(f'{path}:{line}: {msg}', file=sys.stderr) @@ -43,7 +47,7 @@ def index_to_line(index, lines): def extract_file(path, catalog, warn_skipped=False): - source = open(path).read() + source = open(path, encoding="utf-8").read() newlines = newline_positions(source) # match internationalized quoted strings @@ -144,7 +148,7 @@ def extract_file(path, catalog, warn_skipped=False): def extract_refs(path, catalog): - source = open(path).read() + source = open(path, encoding="utf-8").read() newlines = newline_positions(source) # match message catalog references to add backrefs @@ -261,13 +265,25 @@ def main(): ap.add_argument('file', nargs='+', help='Input files') args = ap.parse_args() + for path in args.file: + if not Path(path).exists(): + # assume its regex + for file in sorted(BASE_DIR.glob(path)): + FILE_LIST.append(file) + else: + FILE_LIST.append(Path(path)) + + # Convert the path to relative and use Posix format + for index, absolute_path in enumerate(FILE_LIST[:]): + FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(BASE_DIR) + # extract strings catalog = {} - for path in args.file: + for path in FILE_LIST: extract_file(path, catalog, warn_skipped=args.warn_skipped) # process backreferences in a 2nd pass - for path in args.file: + for path in FILE_LIST: extract_refs(path, catalog) # check the catalog entries From 53ced0cb21e8d8e61191dfa90f485ea27fdffadf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 19 Nov 2022 12:20:19 +0000 Subject: [PATCH 03/13] languages: Add new script update-pot.py --- lang/lang-extract.py | 17 ++++++++--- lang/update-pot.py | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 lang/update-pot.py diff --git a/lang/lang-extract.py b/lang/lang-extract.py index 73c2e20ce..677b28ab1 100755 --- a/lang/lang-extract.py +++ b/lang/lang-extract.py @@ -4,13 +4,22 @@ import bisect import codecs import polib import regex +import os import sys import lib.charset as cs from pathlib import Path, PurePosixPath -BASE_DIR: Path = Path.cwd() FILE_LIST: list[Path] = [] +# Absolute path +BASE_DIR: Path = Path.cwd().resolve() +PO_DIR: Path = BASE_DIR / "po" + +# Pathlib can't change the working directory yet +# The script is currently made to assume the working +# directory is ./lang/po +os.chdir(PO_DIR) + def line_warning(path, line, msg): print(f'{path}:{line}: {msg}', file=sys.stderr) @@ -267,15 +276,15 @@ def main(): for path in args.file: if not Path(path).exists(): - # assume its regex - for file in sorted(BASE_DIR.glob(path)): + # assume its regex, search for files that match pattern + for file in sorted(PO_DIR.glob(path)): FILE_LIST.append(file) else: FILE_LIST.append(Path(path)) # Convert the path to relative and use Posix format for index, absolute_path in enumerate(FILE_LIST[:]): - FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(BASE_DIR) + FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(PO_DIR) # extract strings catalog = {} diff --git a/lang/update-pot.py b/lang/update-pot.py new file mode 100644 index 000000000..811ad811a --- /dev/null +++ b/lang/update-pot.py @@ -0,0 +1,69 @@ +#!/usr/bin/env python3 + +""" +Script updates the Firmware.pot file. + +The script does the following: +1. Current Firmware.pot is backed up with a copy, Firmware.pot.bak +2. Runs lang-extract.py with all the correct arguments. +""" + +import sys +import os +from pathlib import Path, PurePath, PurePosixPath +import shutil +import subprocess +from subprocess import CalledProcessError + +BASE_DIR: Path = Path.cwd().resolve() +PROJECT_DIR: Path = BASE_DIR.parent +PO_DIR: Path = BASE_DIR / "po" + +SEARCH_REGEX: str = "[a-zA-Z]*.[ch]*" +SEARCH_PATHS: list[str] = ["./Firmware", "./Firmware/mmu2"] + +def main(): + + # Start by creating a back-up of the current Firmware.pot + shutil.copy(PO_DIR / "Firmware.pot", PO_DIR / "Firmware.pot.bak") + + # Get the relative prepend of Project directory relative to ./po directory + # This should be something like '../../' + # Note: Pathlib's relative_to() doesn't handle this case yet, so let's use os module + rel_path = os.path.relpath(PROJECT_DIR, PO_DIR) + + # We want to search for the C/C++ files relative to the .po/ directory + # Lets append to the search path an absolute path. + for index, search_path in enumerate(SEARCH_PATHS[:]): + try: + # Example: Converts ./Firmware to ../../Firmware + SEARCH_PATHS[index] = PurePath(rel_path).joinpath(search_path) + + # Example: Convert ../../Firmware to ../../Firmware/[a-zA-Z]*.[ch]* + SEARCH_PATHS[index] = PurePosixPath(SEARCH_PATHS[index]).joinpath(SEARCH_REGEX) + except ValueError as error: + print(error) + + # Run the lang-extract.py script + try: + subprocess.check_call( + [ + "python", + "lang-extract.py", + "--no-missing", + "-s", + "-o", + "./Firmware.pot", + SEARCH_PATHS[0], + SEARCH_PATHS[1], + ] + ) + except CalledProcessError as error: + print(error) + + +if __name__ == "__main__": + try: + main() + except KeyboardInterrupt: + sys.exit(-1) From 7966633c1a93074981504580b2fc0f0c35d86085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 26 Nov 2022 15:14:18 +0000 Subject: [PATCH 04/13] Allow languages to be updated from any directory Now these commands will work: python .\lang\update-po.py python .\lang\update-pot.py When current working directory is .\Prusa-Firmware\ Previously, you'd need to call the script within the lang folder. --- lang/lang-extract.py | 2 +- lang/update-po.py | 2 +- lang/update-pot.py | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lang/lang-extract.py b/lang/lang-extract.py index 677b28ab1..929b430b5 100755 --- a/lang/lang-extract.py +++ b/lang/lang-extract.py @@ -12,7 +12,7 @@ from pathlib import Path, PurePosixPath FILE_LIST: list[Path] = [] # Absolute path -BASE_DIR: Path = Path.cwd().resolve() +BASE_DIR: Path = Path.absolute(Path(__file__).parent) PO_DIR: Path = BASE_DIR / "po" # Pathlib can't change the working directory yet diff --git a/lang/update-po.py b/lang/update-po.py index 1a710b04e..28978ff27 100644 --- a/lang/update-po.py +++ b/lang/update-po.py @@ -12,7 +12,7 @@ from pathlib import Path import polib from polib import POFile -BASE_DIR: Path = Path.cwd() +BASE_DIR: Path = Path.absolute(Path(__file__).parent) PO_DIR: Path = BASE_DIR / "po" PO_FILE_LIST: list[Path] = [] POT_REFERENCE: POFile = polib.pofile(PO_DIR/'Firmware.pot') diff --git a/lang/update-pot.py b/lang/update-pot.py index 811ad811a..7e305738e 100644 --- a/lang/update-pot.py +++ b/lang/update-pot.py @@ -15,7 +15,7 @@ import shutil import subprocess from subprocess import CalledProcessError -BASE_DIR: Path = Path.cwd().resolve() +BASE_DIR: Path = Path.absolute(Path(__file__).parent) PROJECT_DIR: Path = BASE_DIR.parent PO_DIR: Path = BASE_DIR / "po" @@ -45,11 +45,12 @@ def main(): print(error) # Run the lang-extract.py script + SCRIPT_PATH = BASE_DIR.joinpath("lang-extract.py") try: subprocess.check_call( [ "python", - "lang-extract.py", + SCRIPT_PATH, "--no-missing", "-s", "-o", From d90859e6a686cf2ab1302e2f243f27a1654fec89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 26 Nov 2022 16:10:48 +0000 Subject: [PATCH 05/13] Add requirements.txt --- lang/requirements.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 lang/requirements.txt diff --git a/lang/requirements.txt b/lang/requirements.txt new file mode 100644 index 000000000..e5e595076 --- /dev/null +++ b/lang/requirements.txt @@ -0,0 +1,3 @@ +polib==1.1.1 +pyelftools==0.29 +regex==2022.9.13 From 4a93288353849c20dfb4e39e4d77c0f0bcd1e153 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 27 Nov 2022 09:35:21 +0000 Subject: [PATCH 06/13] Update README --- lang/README.md | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/lang/README.md b/lang/README.md index e3fdfa0cc..2e8e51217 100644 --- a/lang/README.md +++ b/lang/README.md @@ -8,9 +8,9 @@ Firmware support is controlled by the ``LANG_MODE`` define in the configuration, ### Required tools -Python 3 with the ``regex``, ``pyelftools`` and ``polib`` modules as well as ``gettext`` and ``dos2unix``. On a debian-based distribution, install the required packages with: +Python 3 is the main tool. To install the required packages run the following command in the `lang` folder: - sudo apt-get install python3-regex python3-pyelftools python3-polib gettext dos2unix + pip install -r requirements.txt ### Main summary @@ -26,8 +26,8 @@ High-level tools: * ``config.sh``: Language selection/configuration * ``fw-build.sh``: Builds the final multi-language hex file into this directory * ``fw-clean.sh``: Cleanup temporary files left by ``fw-build.sh`` -* ``update-pot.sh``: Extract internationalized strings from the sources and place them inside ``po/Firmware.pot`` -* ``update-po.sh``: Refresh po file/s with new translations from the main pot file. +* ``update-pot.py``: Extract internationalized strings from the sources and place them inside ``po/Firmware.pot`` +* ``update-po.py``: Refresh po file/s with new translations from the main pot file. Lower-level tools: @@ -47,6 +47,28 @@ This step is already performed for you when using ``build.sh`` or ``PF-build.sh` ### Updating an existing translation +#### How to update `.pot` file + +Run + + python update-pot.py + +to regenerate ``po/Firmware.pot`` and verify that the annotation has been picked up correctly. You can stop here if you only care about the annotation. + +#### How to update `.po` file + +To update a single `.po` file: + + python update-po.py --file Firmware_XY.po + +This will propagate the new strings to your language. This will merge the new strings, update references/annotations as well as marking unused strings as obsolete. + +To update all .po files at once: + + python update-po.py --all + + + #### Typo or incorrect translation in existing text If you see a typo or an incorrect translation, simply edit ``po/Firmware_XY.po`` and make a pull request with the changes. @@ -65,17 +87,15 @@ to preview all translations as formatted on the screen. If some text is missing, but there is no reference text in the po file, you need to refresh the translation file by picking up new strings and annotations from the template. -Run ``./update-po.sh po/Firmware_XY.po`` to propagate the new strings to your language. This will merge the new strings, update references/annotations as well as marking unused strings as obsolete. - -Update the translations, then proceed as for [typo or incorrect translation](#typo-or-incorrect-translation-in-existing-text). +See section [how to update .po file](#how-to-update-.po-file) to update the translations, then proceed as for [typo or incorrect translation](#typo-or-incorrect-translation-in-existing-text). ### Fixing an incorrect screen annotation or english text The screen annotations as well as the original english text is extracted from the firmware sources. **Do not change the main pot file**. The ``pot`` and ``po`` file contains the location of the annotation to help you fix the sources themselves. -Run ``./update-pot.sh`` to regenerate ``po/Firmware.pot`` and verify that the annotation has been picked up correctly. You can stop here if you only care about the annotation. +* See section [how to update .pot file](#how-to-update-.pot-file) to update the reference file. -Run ``./update-po.sh po/Firmware_XY.po`` otherwise to propagate the annotation to your language, then proceed as for [typo or incorrect translation](#typo-or-incorrect-translation-in-existing-text). +* To sync one language: See section [how to update .po file](#how-to-update-.po-file); to propagate the annotation from the `.pot` file to your language, then proceed as for [typo or incorrect translation](#typo-or-incorrect-translation-in-existing-text). ### Adding a new language @@ -83,7 +103,7 @@ Each language is assigned a two-letter ISO639-1 language code. The firmware needs to be aware of the language code. It's probably necessary to update the "Language codes" section in ``Firmware/language.h`` to add the new code as a ``LANG_CODE_XY`` define as well as add the proper language name in the function ``lang_get_name_by_code`` in ``Firmware/language.c``. -It is a good idea to ensure the translation template is up-to-date before starting to translate. Run ``./update-pot.sh`` to regenerate ``po/Firmware.pot`` if possible. +It is a good idea to ensure the translation template is up-to-date before starting to translate. See section [how to update .pot file](#how-to-update-.pot-file). Copy ``po/Firmware.pot`` to ``po/Firmware_XY.po``. The *same* language code needs to be used for the "Language" entry in the metadata. Other entries can be customized freely. From b14f3780d46d90afaa29b69781e82313e0e63a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 27 Nov 2022 10:05:26 +0000 Subject: [PATCH 07/13] Add mutually exclusive arguments --file and --all Only one of these arguments must be supplied, or argparse raises an error. --- lang/update-po.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lang/update-po.py b/lang/update-po.py index 28978ff27..a3b2360ed 100644 --- a/lang/update-po.py +++ b/lang/update-po.py @@ -7,7 +7,8 @@ by calling within the ./po folder: python ../lang-extract.py --no-missing -s -o Firmware.pot ../../Firmware/[a-zA-Z]*.[ch]* ../../Firmware/mmu2/[a-zA-Z]*.[ch]* """ -import sys +import argparse +from sys import stderr, exit from pathlib import Path import polib from polib import POFile @@ -19,7 +20,21 @@ POT_REFERENCE: POFile = polib.pofile(PO_DIR/'Firmware.pot') def main(): - PO_FILE_LIST = sorted(PO_DIR.glob('**/*.po')) + global PO_FILE_LIST + ap = argparse.ArgumentParser() + group = ap.add_mutually_exclusive_group(required=True) + group.add_argument('-f', '--file', help='File path for a single PO file to update Example: ./po/Firmware_cs.po') + group.add_argument('--all', action='store_true', help='Update all PO files at once') + args = ap.parse_args() + + if args.all: + PO_FILE_LIST = sorted(PO_DIR.glob('**/*.po')) + elif args.file: + if Path(args.file).is_file(): + PO_FILE_LIST.append(Path(args.file)) + else: + print("{}: file does not exist or is not a regular file".format(args.file), file=stderr) + return 1 for po_file in PO_FILE_LIST: po = polib.pofile(po_file) @@ -31,4 +46,4 @@ if __name__ == "__main__": try: main() except KeyboardInterrupt: - sys.exit(-1) + exit(-1) From 62c87a1fe58f0c4f6800233e625c8b66d845e4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sun, 27 Nov 2022 10:07:37 +0000 Subject: [PATCH 08/13] Adjust module docstring --- lang/update-po.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/lang/update-po.py b/lang/update-po.py index a3b2360ed..98aaef18d 100644 --- a/lang/update-po.py +++ b/lang/update-po.py @@ -2,9 +2,6 @@ """ Portable script to update po files on most platforms -Make sure the .pot file is up-to-date first by -by calling within the ./po folder: -python ../lang-extract.py --no-missing -s -o Firmware.pot ../../Firmware/[a-zA-Z]*.[ch]* ../../Firmware/mmu2/[a-zA-Z]*.[ch]* """ import argparse From 327254d091bc71a425cee31fea2bc0ff8590b4db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 10 Dec 2022 12:09:42 +0000 Subject: [PATCH 09/13] Expand glob in update-pot on Windows only --- lang/lang-extract.py | 20 +++----------------- lang/update-pot.py | 31 +++++++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lang/lang-extract.py b/lang/lang-extract.py index 929b430b5..a5b74eb2c 100755 --- a/lang/lang-extract.py +++ b/lang/lang-extract.py @@ -7,9 +7,7 @@ import regex import os import sys import lib.charset as cs -from pathlib import Path, PurePosixPath - -FILE_LIST: list[Path] = [] +from pathlib import Path # Absolute path BASE_DIR: Path = Path.absolute(Path(__file__).parent) @@ -274,25 +272,13 @@ def main(): ap.add_argument('file', nargs='+', help='Input files') args = ap.parse_args() - for path in args.file: - if not Path(path).exists(): - # assume its regex, search for files that match pattern - for file in sorted(PO_DIR.glob(path)): - FILE_LIST.append(file) - else: - FILE_LIST.append(Path(path)) - - # Convert the path to relative and use Posix format - for index, absolute_path in enumerate(FILE_LIST[:]): - FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(PO_DIR) - # extract strings catalog = {} - for path in FILE_LIST: + for path in args.file: extract_file(path, catalog, warn_skipped=args.warn_skipped) # process backreferences in a 2nd pass - for path in FILE_LIST: + for path in args.file: extract_refs(path, catalog) # check the catalog entries diff --git a/lang/update-pot.py b/lang/update-pot.py index 7e305738e..13fda1167 100644 --- a/lang/update-pot.py +++ b/lang/update-pot.py @@ -15,14 +15,21 @@ import shutil import subprocess from subprocess import CalledProcessError +# Constants BASE_DIR: Path = Path.absolute(Path(__file__).parent) PROJECT_DIR: Path = BASE_DIR.parent PO_DIR: Path = BASE_DIR / "po" +# Regex pattern to search for source files SEARCH_REGEX: str = "[a-zA-Z]*.[ch]*" + +# Folders to search for messages SEARCH_PATHS: list[str] = ["./Firmware", "./Firmware/mmu2"] + def main(): + # List of source files to extract messages from + FILE_LIST: list[Path] = [] # Start by creating a back-up of the current Firmware.pot shutil.copy(PO_DIR / "Firmware.pot", PO_DIR / "Firmware.pot.bak") @@ -34,16 +41,33 @@ def main(): # We want to search for the C/C++ files relative to the .po/ directory # Lets append to the search path an absolute path. - for index, search_path in enumerate(SEARCH_PATHS[:]): + for index, search_path in enumerate(SEARCH_PATHS.copy()): try: # Example: Converts ./Firmware to ../../Firmware SEARCH_PATHS[index] = PurePath(rel_path).joinpath(search_path) # Example: Convert ../../Firmware to ../../Firmware/[a-zA-Z]*.[ch]* - SEARCH_PATHS[index] = PurePosixPath(SEARCH_PATHS[index]).joinpath(SEARCH_REGEX) + SEARCH_PATHS[index] = PurePosixPath(SEARCH_PATHS[index]).joinpath( + SEARCH_REGEX + ) except ValueError as error: print(error) + # If operating system is Windows, then the script must expand + # the regex expression with glob. On Linux and Mac, the operating + # system takes care of expanding the glob for you. We only need to + # do this manually on Windows. + if sys.platform == "win32": + for pattern in SEARCH_PATHS: + for file in sorted(PO_DIR.glob(str(pattern))): + FILE_LIST.append(file) + + # Convert the path to relative and use Posix format + for index, absolute_path in enumerate(FILE_LIST.copy()): + FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(PO_DIR) + else: + FILE_LIST = SEARCH_PATHS + # Run the lang-extract.py script SCRIPT_PATH = BASE_DIR.joinpath("lang-extract.py") try: @@ -55,8 +79,7 @@ def main(): "-s", "-o", "./Firmware.pot", - SEARCH_PATHS[0], - SEARCH_PATHS[1], + *FILE_LIST, ] ) except CalledProcessError as error: From 280a90479982ddd8de8342fc55a2f5044d378905 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Sat, 10 Dec 2022 12:52:58 +0000 Subject: [PATCH 10/13] ubuntu: subprocess does not allow extended glob It's my understanding that we cannot use extended globbing on Linux because of the way I am invoking the lang-extract script. the python script is not run through bash/shell. We would need to perhaps use Shell = True in check_call() but then the all the input arguments needs to be one string. This commit was tested on Ubuntu 22.04.1 LTS --- lang/update-pot.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/lang/update-pot.py b/lang/update-pot.py index 13fda1167..4d8368c59 100644 --- a/lang/update-pot.py +++ b/lang/update-pot.py @@ -53,20 +53,14 @@ def main(): except ValueError as error: print(error) - # If operating system is Windows, then the script must expand - # the regex expression with glob. On Linux and Mac, the operating - # system takes care of expanding the glob for you. We only need to - # do this manually on Windows. - if sys.platform == "win32": - for pattern in SEARCH_PATHS: - for file in sorted(PO_DIR.glob(str(pattern))): - FILE_LIST.append(file) + # Extend the glob and append all found files into FILE_LIST + for pattern in SEARCH_PATHS: + for file in sorted(PO_DIR.glob(str(pattern))): + FILE_LIST.append(file) - # Convert the path to relative and use Posix format - for index, absolute_path in enumerate(FILE_LIST.copy()): - FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(PO_DIR) - else: - FILE_LIST = SEARCH_PATHS + # Convert the path to relative and use Posix format + for index, absolute_path in enumerate(FILE_LIST.copy()): + FILE_LIST[index] = PurePosixPath(absolute_path).relative_to(PO_DIR) # Run the lang-extract.py script SCRIPT_PATH = BASE_DIR.joinpath("lang-extract.py") From b6ef8ea32c34d140e86df248e3b8e37fa3d29707 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 18 Feb 2023 10:13:34 +0000 Subject: [PATCH 11/13] Add a backup of po files --- lang/update-po.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lang/update-po.py b/lang/update-po.py index 98aaef18d..a1e07da55 100644 --- a/lang/update-po.py +++ b/lang/update-po.py @@ -6,6 +6,7 @@ Portable script to update po files on most platforms import argparse from sys import stderr, exit +import shutil from pathlib import Path import polib from polib import POFile @@ -34,6 +35,9 @@ def main(): return 1 for po_file in PO_FILE_LIST: + # Start by creating a back-up of the .po file + po_file_bak = po_file.with_suffix(".bak") + shutil.copy(PO_DIR / po_file, PO_DIR / po_file_bak) po = polib.pofile(po_file) po.merge(POT_REFERENCE) po.save(po_file) From 526e02a0419f2035383e62dca7d1c257c896deb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gu=C3=B0ni=20M=C3=A1r=20Gilbert?= Date: Sat, 18 Feb 2023 10:30:18 +0000 Subject: [PATCH 12/13] Fix an issue with using --file argument The following command did not work in the lang folder: python .\update-po.py --file ./po/Firmware_cs.po To resolve this we just extract the filename from the path and use the constant PO_DIR to make sure the path is always the same. --- lang/update-po.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lang/update-po.py b/lang/update-po.py index a1e07da55..042b8269b 100644 --- a/lang/update-po.py +++ b/lang/update-po.py @@ -37,7 +37,7 @@ def main(): for po_file in PO_FILE_LIST: # Start by creating a back-up of the .po file po_file_bak = po_file.with_suffix(".bak") - shutil.copy(PO_DIR / po_file, PO_DIR / po_file_bak) + shutil.copy(PO_DIR / po_file.name, PO_DIR / po_file_bak.name) po = polib.pofile(po_file) po.merge(POT_REFERENCE) po.save(po_file) From 940f5b72145b8ec0f986f00702590e80900627b5 Mon Sep 17 00:00:00 2001 From: gudnimg Date: Mon, 20 Feb 2023 17:53:41 +0000 Subject: [PATCH 13/13] Add executable permission for update-po.py and update-pot.py on Linux --- lang/update-po.py | 0 lang/update-pot.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 lang/update-po.py mode change 100644 => 100755 lang/update-pot.py diff --git a/lang/update-po.py b/lang/update-po.py old mode 100644 new mode 100755 diff --git a/lang/update-pot.py b/lang/update-pot.py old mode 100644 new mode 100755