Prusa-Firmware/lang/lang-check.py
Yuri D'Elia 0d7680dbf7
New PO-based language translation support (#3471)
* lang: Add a PO language extractor with FW metadata support

Implement a straight-to-po language extractor which supports our custom
language requirements:

- _i/_I/ISTR for text string definitions
- _T for catalog translations (with back-reference support)
- //// EOL comments with:
  - MSG_ catalog entry name identifiers
  - c=X r=Y annotations for screen dimensioning checks
- Crude support for commented lines

All source locations are correctly referenced in the PO, with the
metadata colleted in the comment for further processing.

Several checks are implemented already during extraction:

- Correct catalog name assignment (no duplicates)
- Metadata checks for each entry

Further checks will be implemented by directly checking the translated PO file.

Requires "polib" and "regex" python modules.

* lang: Adapt lang-check to work directly on PO/POT files

* lang: Allow lang-extract to generate stable (pre-sorted) output directly

* lang: Further extend lang-extract consistency/error checking

- Do not parse inside preprocessor conditionals
- Distinguish between references and definitions
- Warn about missing references and definitions

* lang: lang-extract: warn about incorrect PROGMEM assignments

Check that ISTR is used along with PROGMEM_I1 in an attempt to spot
useless translated catalogs.

* lang: lang-extract: Improved handling of same-line translations

Correctly reference metadata on same-line translations.

* lang: lang-extract: Handle _O as a cat-ref

https://github.com/prusa3d/Prusa-Firmware/pull/3434

* lang: lang-extract: Warn about unused catalog definitions

* lang: lang-extract: Allow propagating translation comments via //

The definition:

    code //// definition [// comment]

will check [definition] as before, but blindly accumulate // comment.
The comment is then re-appended back into the PO files for translators
with the form:

    definition
    comment
    comment...

* lang: Fix incorrect display definitions

* lang: lang-extract: Check source encoding/charmap

* lang: Translate the degree symbol

* lang: Unbreak/cleanup DEBUG_SEC_LANG

* lang: Improve meaning of comment

* lang: Split charset conversions into an aux lib for future use

* lang: Implement lang-map.py to extract the translation symbol map

- Extracts the translatable symbol map for further use
- Computes a stable "language signature" from the map itself
- Optionally patches the binary update the symbols

* lang: Check for translation recoding problems

* lang: Implement a transliteration map to post-process translations

TRANS_CHARS is now used to replace unavailable symbols to the source
encoding, only while producing the language catalog.

* lang: Handle/check character replacements in lang-check

Filter the translation through TRANS_CHARS, so that the preview and
length check are performed correctly for expanding replacements such as
'ß' to 'ss'.

* lang: Implement lang-build.py to generate the final language catalog

* Cleanup .gitignore

* lang: Drop txt language files

* lang: Remove outdated translation scripts and obsolete docs

* lang: Update build scripts for new infrastructure

* lang: [no] Integrate accents from po/new/no.po

We now support accents natively

* lang: Remove redundant directory po/new/

* lang: Fix encoding of LCD characters in PO files

* lang: [hr] Fix wrapping in MSG_CRASH_DET_ONLY_IN_NORMAL

* lang: Sort and reformat PO files for further massaging

* lang: Switch to developer (dot) comments for PO metadata

* lang: Allow the IGNORE annotation to skip extraction

* lang: Fix missing/broken language metadata in sources

* lang: Add update-pot.sh and regenerate po/Firmware.pot

* lang: Add update-po.sh and refresh all PO files

* lang: Add summary documentation about the new translation workflow

* Add more ignored files

* CI: Add new required dependencies to travis

* lang: lang-build: Improve warning message

"referenced" was really meaning that data is being duplicated.

* lang: Respect the language order as defined in config.sh

This correctly splits normal and community-made entries during language
selection.

* lang: More typos in the documentation

* lang: Check for the maximum size of each language

Each table needs to fit within LANG_SIZE_RESERVED

* lang: Properly align _SEC_LANG to page boundaries

... instead of relying on _SEC_LANG_TABLE to calculate the offset

* lang: Build support for dual-language hex files

Detect the printer type by checking the current variant type.

On printers with no xflash (MK2*), generate one hex file for each
additional language file by patching the built-in secondary language
table during the build process

* lang: Mention lang-patchsec.py

* lang: Use color() instead of tput for clarity

* lang: Allow disabling terminal colors with NO_COLOR/TERM=dumb

* lang: Consistent use of redirection in config.sh

* lang: Stricter variant-type check for xflash support

* lang: Output size stats when building double-language hex files

* lang: Respect NO_COLOR in lang-check.py

* lang: Check for repeated/incorrect annotations

Catch errors such as "c=1 c=2"

* lang: Correct MSG_SLIGHT_SKEW/MSG_SEVERE_SKEW annotations

* lang: [it] Improve MSG_*_SKEW translation

* lang: Use INTLHEX instead of OUTHEX_P/S for configuration

We already have OUTHEX which is the compiled firmware.

Use INTLHEX for the final internationalized firmware, which is less
confusing. Also, assume it being a prefix for all generated hex
files, which reduces the number of variables set.

* lang: Move lang_map to lib.io for further use

* lang: lang-check: Accept a firmware map file to suppress unused string warnings

* lang: Use the map file to reduce useless warnings during fw-build

* lang: lang-check: Also suppress unused empty annotations

* lang: Fix MSG_MOVE_CARRIAGE_TO_THE_TOP_Z annotation

Refresh pot file

* lang: lang-check: Do not warn about same-word translations by default

Do not warn when one-word translations such as "No" result in "No" also
in other languages, since this is common in latin languages.

Allow to re-enable the warning with --warn-same

* lang: lang-build: Handle same-source/translation efficiently

* lang: [it] Explicitly add On/Off/Reset/Wizard to suppress warnings

Instead of displaying a warning, supress the warning and explicitly
translate each entry using english (which is the common/acceptable
word in these cases).

* lang: [it] Suppress more warnings

* lang: lang-check: Add intermediate "suggest" warning category

Warnings in the "suggest" category as shown as [S] as based on pure
speculation from the checking tool, such as the translation being
significantly shorter than the original.

As a result, they can be suppressed with --no-suggest

* lang: Return translation status from lang-check

- 0 if the translation only contains suggestions
- 1 if the translation contains warnings or errors

Check for the exit status in fw-build.sh, but do nothing at the moment
except printing a non-fatal error.

* lang: Remove "trim_trailing_whitespace=false" for po files

PO files got cleaned up/rewritten. We can now ensure they stay
consistent.

* lang: [sv] Re-integrate changes from 70c73cb

* lang: [no] Reintegrate changes from @pkg2000
2022-06-16 15:03:30 +02:00

333 lines
13 KiB
Python
Executable File

#!/usr/bin/env python3
#
# Version 1.0.2 - Build 43
#############################################################################
# Change log:
# 7 May 2019, ondratu , Initial
# 13 June 2019, 3d-gussner, Fix length false positives
# 14 Sep. 2019, 3d-gussner, Prepare adding new language
# 18 Sep. 2020, 3d-gussner, Fix execution of lang-check.py
# 2 Apr. 2021, 3d-gussner, Fix and improve text warp
# 22 Apr. 2021, DRracer , add English source to output
# 23 Apr. 2021, wavexx , improve
# 24 Apr. 2021, wavexx , improve
# 26 Apr. 2021, wavexx , add character ruler
# 21 Dec. 2021, 3d-gussner, Prepare more community languages
# Swedish
# Danish
# Slovanian
# Hungarian
# Luxembourgian
# Croatian
# 3 Jan. 2022, 3d-gussner, Prepare Lithuanian
# 7 Jan. 2022, 3d-gussner, Check for Syntax errors and exit with error
# , add Build number 'git rev-list --count HEAD lang-check.py'
# 30 Jan. 2022, 3d-gussner, Add arguments. Requested by @AttilaSVK
# --information == output all source and translated messages
# --import-check == used by `lang-import.sh`to verify
# newly import `lang_en_??.txt` files
# 14 Mar. 2022, 3d-gussner, Check if translation isn't equal to origin
#############################################################################
"""Check PO files for formatting errors."""
from argparse import ArgumentParser
from sys import stdout, stderr, exit
import codecs
import polib
import textwrap
import re
import os
from lib import charset as cs
from lib.io import load_map
COLORIZE = (stdout.isatty() and os.getenv("TERM", "dumb") != "dumb") or os.getenv('NO_COLOR') == "0"
def color_maybe(color_attr, text):
if COLORIZE:
return '\033[0;' + str(color_attr) + 'm' + text + '\033[0m'
else:
return text
red = lambda text: color_maybe(31, text)
green = lambda text: color_maybe(32, text)
yellow = lambda text: color_maybe(33, text)
cyan = lambda text: color_maybe(36, text)
def print_wrapped(wrapped_text, rows, cols):
if type(wrapped_text) == str:
wrapped_text = [wrapped_text]
for r, line in enumerate(wrapped_text):
r_ = str(r + 1).rjust(3)
if r >= rows:
r_ = red(r_)
print((' {} |{:' + str(cols) + 's}|').format(r_, line))
def print_truncated(text, cols):
if len(text) <= cols:
prefix = text.ljust(cols)
suffix = ''
else:
prefix = text[0:cols]
suffix = red(text[cols:])
print(' |' + prefix + '|' + suffix)
def print_ruler(spc, cols):
print(' ' * spc + cyan(('₀₁₂₃₄₅₆₇₈₉'*4)[:cols]))
def print_source_translation(source, translation, wrapped_source, wrapped_translation, rows, cols):
if rows == 1:
print(' source text:')
print_ruler(4, cols);
print_truncated(source, cols)
print(' translated text:')
print_ruler(4, cols);
print_truncated(translation, cols)
else:
print(' source text:')
print_ruler(6, cols);
print_wrapped(wrapped_source, rows, cols)
print(' translated text:')
print_ruler(6, cols);
print_wrapped(wrapped_translation, rows, cols)
print()
def highlight_trailing_white(text):
if type(text) == str:
return re.sub(r' $', '·', text)
else:
ret = text[:]
ret[-1] = highlight_trailing_white(ret[-1])
return ret
def wrap_text(text, cols):
ret = []
for line in text.split('\n'):
# wrap each input line in text individually
tmp = list(textwrap.TextWrapper(width=cols).wrap(line))
if len(ret):
# add back trailing whitespace
tmp[-1] += ' ' * (len(text) - len(text.rstrip()))
ret.extend(tmp)
return ret
def ign_char_first(c):
return c.isalnum() or c in {'%', '?'}
def ign_char_last(c):
return c.isalnum() or c in {'.', "'"}
def check_translation(entry, msgids, is_pot, no_warning, no_suggest, warn_empty, warn_same, information):
"""Check strings to display definition."""
# fetch/decode entry for easy access
meta = entry.comment.split('\n', 1)[0]
source = entry.msgid
translation = entry.msgstr
line = entry.linenum
known_msgid = msgids is None or source in msgids
errors = 0
# Check comment syntax (non-empty and include a MSG id)
if known_msgid or warn_empty:
if len(meta) == 0:
print(red("[E]: Translation doesn't contain any comment metadata on line %d" % line))
return False
if not meta.startswith('MSG'):
print(red("[E]: Critical syntax error: comment doesn't start with MSG on line %d" % line))
print(red(" comment: " + meta))
return False
# Check if columns and rows are defined
tokens = meta.split(' ')
cols = None
rows = None
for item in tokens[1:]:
try:
key, val = item.split('=')
if key == 'c':
cols = int(val)
elif key == 'r':
rows = int(val)
else:
raise ValueError
except ValueError:
print(red("[E]: Invalid display definition on line %d" % line))
print(red(" definition: " + meta))
return False
if cols is None and rows is None:
if not no_warning and known_msgid:
errors += 1
print(yellow("[W]: No usable display definition on line %d" % line))
# probably fullscreen, guess from the message length to continue checking
cols = len(source)
if rows is None:
rows = 1
elif rows > 1 and cols != 20:
errors += 1
print(yellow("[W]: Multiple rows with odd number of columns on line %d" % line))
# Check if translation contains unsupported characters
invalid_char = cs.translation_check(cs.unicode_to_source(translation))
if invalid_char is not None:
print(red('[E]: Critical syntax: Unhandled char %s found on line %d' % (repr(invalid_char), line)))
print(red(' translation: ' + translation))
return False
# Pre-process the translation to translated characters for a correct preview and length check
translation = cs.trans_replace(translation)
wrapped_source = wrap_text(source, cols)
rows_count_source = len(wrapped_source)
wrapped_translation = wrap_text(translation, cols)
rows_count_translation = len(wrapped_translation)
# Incorrect number of rows/cols on the definition
if rows == 1 and (len(source) > cols or rows_count_source > rows):
errors += 1
print(yellow('[W]: Source text longer than %d cols as defined on line %d:' % (cols, line)))
print_ruler(4, cols);
print_truncated(source, cols)
print()
elif rows_count_source > rows:
errors += 1
print(yellow('[W]: Wrapped source text longer than %d rows as defined on line %d:' % (rows, line)))
print_ruler(6, cols);
print_wrapped(wrapped_source, rows, cols)
print()
# All further checks are against the translation
if is_pot:
return (errors == 0)
# Missing translation
if len(translation) == 0 and (known_msgid or warn_empty):
errors += 1
if rows == 1:
print(yellow("[W]: Empty translation for \"%s\" on line %d" % (source, line)))
else:
print(yellow("[W]: Empty translation on line %d" % line))
print_ruler(6, cols);
print_wrapped(wrapped_source, rows, cols)
print()
# Check for translation lenght
if (rows_count_translation > rows) or (rows == 1 and len(translation) > cols):
errors += 1
print(red('[E]: Text is longer than definition on line %d: cols=%d rows=%d (rows diff=%d)'
% (line, cols, rows, rows_count_translation-rows)))
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
# Different count of % sequences
if source.count('%') != translation.count('%') and len(translation) > 0:
errors += 1
print(red('[E]: Unequal count of %% escapes on line %d:' % (line)))
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
# Different first/last character
if not no_suggest and len(source) > 0 and len(translation) > 0:
source_end = source.rstrip()[-1]
translation_end = translation.rstrip()[-1]
start_diff = not (ign_char_first(source[0]) and ign_char_first(translation[0])) and source[0] != translation[0]
end_diff = not (ign_char_last(source_end) and ign_char_last(translation_end)) and source_end != translation_end
if start_diff or end_diff:
if start_diff:
print(yellow('[S]: Differing first punctuation character (%s => %s) on line %d:' % (source[0], translation[0], line)))
if end_diff:
print(yellow('[S]: Differing last punctuation character (%s => %s) on line %d:' % (source[-1], translation[-1], line)))
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
if not no_suggest and source == translation and (warn_same or len(source.split(' ', 1)) > 1):
print(yellow('[S]: Translation same as original on line %d:' %line))
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
# Short translation
if not no_suggest and len(source) > 0 and len(translation) > 0:
if len(translation.rstrip()) < len(source.rstrip()) / 2:
print(yellow('[S]: Short translation on line %d:' % (line)))
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
# Incorrect trailing whitespace in translation
if not no_warning and len(translation) > 0 and \
(source.rstrip() == source or (rows == 1 and len(source) == cols)) and \
translation.rstrip() != translation and \
(rows > 1 or len(translation) != len(source)):
errors += 1
print(yellow('[W]: Incorrect trailing whitespace for translation on line %d:' % (line)))
source = highlight_trailing_white(source)
translation = highlight_trailing_white(translation)
wrapped_translation = highlight_trailing_white(wrapped_translation)
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
# show the information
if information and errors == 0:
print(green('[I]: %s' % (meta)))
print_source_translation(source, translation,
wrapped_source, wrapped_translation,
rows, cols)
return (errors == 0)
def main():
"""Main function."""
parser = ArgumentParser(description=__doc__)
parser.add_argument("po", help="PO file to check")
parser.add_argument(
"--no-warning", action="store_true",
help="Disable warnings")
parser.add_argument(
"--no-suggest", action="store_true",
help="Disable suggestions")
parser.add_argument(
"--pot", action="store_true",
help="Do not check translations")
parser.add_argument(
"--information", action="store_true",
help="Output all translations")
parser.add_argument("--map",
help="Provide a map file to suppress warnings about unused translations")
parser.add_argument(
"--warn-empty", action="store_true",
help="Warn about empty definitions and translations even if unused")
parser.add_argument(
"--warn-same", action="store_true",
help="Warn about one-word translations which are identical to the source")
# load the translations
args = parser.parse_args()
if not os.path.isfile(args.po):
print("{}: file does not exist or is not a regular file".format(args.po), file=stderr)
return 1
# load the symbol map to supress empty (but unused) translation warnings
msgids = None
if args.map:
msgids = set()
for sym in load_map(args.map):
if type(sym['data']) == bytes:
msgid = cs.source_to_unicode(codecs.decode(sym['data'], 'unicode_escape', 'strict'))
msgids.add(msgid)
# check each translation in turn
status = True
for translation in polib.pofile(args.po):
status &= check_translation(translation, msgids, args.pot, args.no_warning, args.no_suggest,
args.warn_empty, args.warn_same, args.information)
return 0 if status else os.EX_DATAERR
if __name__ == "__main__":
exit(main())