From abd23045bc3bc06fcd6475e3e616056c1870ab0c Mon Sep 17 00:00:00 2001 From: rebortg Date: Mon, 31 Jul 2023 21:13:58 +0200 Subject: add translation feature --- .gitignore | 1 + docs/conf.py | 6 +++--- languagechecker.py | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ localazy.json | 7 ++++--- 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 languagechecker.py diff --git a/.gitignore b/.gitignore index 77f66e38..b613d4f4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Sphinx _build/ +localazy_private.json # python virtualenv venv/ diff --git a/docs/conf.py b/docs/conf.py index 23b595aa..4807472e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -72,12 +72,12 @@ master_doc = 'index' # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. -language = None +language = "en" # https://docs.readthedocs.io/en/stable/guides/manage-translations-sphinx.html#create-translatable-files locale_dirs = ['_locale/'] -gettext_compact = False -gettext_uuid = True +gettext_compact = True +gettext_uuid = False # List of patterns, relative to source directory, that match files and diff --git a/languagechecker.py b/languagechecker.py new file mode 100644 index 00000000..0305b05b --- /dev/null +++ b/languagechecker.py @@ -0,0 +1,59 @@ +''' +Parse gettext pot files and extract path:line and msgid information +compare this with downloaded files from localazy +the output are the elements which are downloaded but not needed anymore. +TODO: better output +''' + +import os +from babel.messages.pofile import read_po + + +def extract_content(file): + content = [] + with open(file) as f: + data = read_po(f) + for message in data: + if message.id: + content.append(message) + return content + + +gettext_dir = "docs/_build/gettext" +gettext_ext = ".pot" +original_content = list() + +language_dir = "docs/_locale" +language_ext = ".pot" +language_content = dict() + +# get gettext filepath +for (dirpath, dirnames, filenames) in os.walk(gettext_dir): + for file in filenames: + if gettext_ext in file: + original_content.extend(extract_content(f"{dirpath}/{file}")) + +# get filepath per language +languages = next(os.walk(language_dir))[1] +for language in languages: + + language_content[language] = list() + for (dirpath, dirnames, filenames) in os.walk(f"{language_dir}/{language}"): + for file in filenames: + if language_ext in file: + language_content[language].extend(extract_content(f"{dirpath}/{file}")) + + + +for lang in language_content.keys(): + for message in language_content[lang]: + found = False + for ori_message in original_content: + if ori_message.id == message.id: + found = True + if not found: + print() + print(f"{lang}: {message.id}") + for loc in message.locations: + print(f"{loc[0]}:{loc[1]}") + diff --git a/localazy.json b/localazy.json index eb6b430a..03191f49 100644 --- a/localazy.json +++ b/localazy.json @@ -1,11 +1,12 @@ { - "upload": { + "upload": { "type": "pot", - "folder": "docs/_build/gettext", + "folder": "docs/_build/gettext", "files": { "pattern": "**.pot", - "excludes": [ + "excludes": [ "changelog/*.pot", + "changelog.pot", "coverage.pot" ], "path": "${path}" -- cgit v1.2.3