Skip to content

Commit

Permalink
IMPROVEMENT: Portuguse translation
Browse files Browse the repository at this point in the history
  • Loading branch information
amilcarlucas committed Nov 21, 2024
1 parent 1c0912d commit d606483
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def reconnect(self, selected_connection: str = "") -> bool: # defaults to auto-
self.previous_selection = self.flight_controller.comport.device
if self.destroy_parent_on_connect:
self.parent.root.destroy()
if self.download_params_on_connect and hasattr(self.parent, _("download_flight_controller_parameters")):
if self.download_params_on_connect and hasattr(self.parent, "download_flight_controller_parameters"):
self.parent.download_flight_controller_parameters(redownload=False)
return False

Expand Down
6 changes: 1 addition & 5 deletions MethodicConfigurator/locale/MethodicConfigurator.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-11-20 18:33+0100\n"
"POT-Creation-Date: 2024-11-20 21:54+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -821,10 +821,6 @@ msgstr ""
msgid "Connection step {} of {}"
msgstr ""

#: MethodicConfigurator/frontend_tkinter_connection_selection.py:135
msgid "download_flight_controller_parameters"
msgstr ""

#: MethodicConfigurator/frontend_tkinter_connection_selection.py:151
msgid "Flight controller connection"
msgstr ""
Expand Down
Binary file modified MethodicConfigurator/locale/pt/LC_MESSAGES/MethodicConfigurator.mo
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
msgid ""
msgstr ""
"Project-Id-Version: 0.9.9\n"
"POT-Creation-Date: 2024-11-20 18:33+0100\n"
"PO-Revision-Date: 2024-11-20 19:23+0100\n"
"POT-Creation-Date: 2024-11-20 21:54+0100\n"
"PO-Revision-Date: 2024-11-20 21:55+0100\n"
"Last-Translator: Dr.-Ing. Amilcar do Carmo Lucas <[email protected]>\n"
"Language-Team: \n"
"Language: pt\n"
Expand Down Expand Up @@ -997,10 +997,6 @@ msgstr "Conectando com o FC"
msgid "Connection step {} of {}"
msgstr "Passo de conexão {} de {}"

#: MethodicConfigurator/frontend_tkinter_connection_selection.py:135
msgid "download_flight_controller_parameters"
msgstr "download_flight_controller_parameters"

#: MethodicConfigurator/frontend_tkinter_connection_selection.py:151
msgid "Flight controller connection"
msgstr "Conexão do controlador de voo"
Expand Down Expand Up @@ -1573,7 +1569,7 @@ msgstr "Não disponível"

#: MethodicConfigurator/frontend_tkinter_parameter_editor.py:253
msgid "This parameter is not available on the connected flight controller"
msgstr "Este parâmetro não está dísponivel no controlador de voo conectado."
msgstr "Este parâmetro não está dísponivel no controlador de voo conectado"

#: MethodicConfigurator/frontend_tkinter_parameter_editor.py:255
msgid "Not editable"
Expand Down Expand Up @@ -1693,7 +1689,7 @@ msgstr "3. Use "
#: MethodicConfigurator/frontend_tkinter_parameter_editor.py:362
#: MethodicConfigurator/frontend_tkinter_parameter_editor_table.py:238
msgid "Del"
msgstr "Del"
msgstr "Rem"

#: MethodicConfigurator/frontend_tkinter_parameter_editor.py:363
msgid " and "
Expand Down

This file was deleted.

125 changes: 125 additions & 0 deletions extract_missing_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env python3

"""
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator
SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <[email protected]>
SPDX-License-Identifier: GPL-3.0-or-later
"""

import argparse
import gettext
import glob
import os


def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Extract missing translations from a .po (GNU gettext) file.")

# Add argument for language code
parser.add_argument(
"--lang-code",
default="zh_CN",
type=str,
help="The language code for which to extract missing translations (e.g., 'zh_CN', 'pt'). Defaults to %(default)s",
)

# Add argument for output file
parser.add_argument(
"--output-file",
default="missing_translation",
type=str,
help="The base name of the file(s) where the missing translations will be written. "
"This file will contain lines in the format 'index:msgid'. Defaults to %(default)s",
)

# Add argument for maximum number of translations per output file
parser.add_argument(
"--max-translations",
default=80,
type=int,
help="The maximum number of missing translations to write to each output file. Defaults to %(default)s",
)

return parser.parse_args()


def extract_missing_translations(lang_code: str) -> list[tuple[int, str]]:
# Set up the translation catalog
po_file = os.path.join("MethodicConfigurator", "locale", lang_code, "LC_MESSAGES", "MethodicConfigurator.po")
language = gettext.translation(
"MethodicConfigurator", localedir="MethodicConfigurator\\locale", languages=[lang_code], fallback=True
)

# Read the .po file entries
with open(po_file, encoding="utf-8") as f:
lines = f.readlines()

missing_translations: list[tuple[int, str]] = []

# Iterate through lines to find untranslated msgid
msgid = ""
in_msgid = False
for i, f_line in enumerate(lines):
line = f_line.strip()

if line.startswith("msgid "):
msgid = ""
in_msgid = True

if in_msgid and not line.startswith("msgstr "):
line_split = line.split('"')
if len(line_split) > 1:
msgid += '"'.join(line_split[1:-1]) # Get the msgid string
else:
print(f"Error on line {i}")
continue

if in_msgid and line.startswith("msgstr "):
in_msgid = False
# escape \ characters in a string
msgid_escaped = msgid.replace("\\", "\\\\")
# Check if the translation exists
if language.gettext(msgid_escaped) == msgid: # If translation is the same as msgid, it's missing
missing_translations.append((i - 1, msgid))

return missing_translations


def output_to_files(missing_translations: list[tuple[int, str]], output_file_base_name: str, max_translations: int) -> None:
# Remove any existing output files with the same base name
existing_files = glob.glob(f"{output_file_base_name}.txt")
existing_files += glob.glob(f"{output_file_base_name}_*.txt")

for existing_file in existing_files:
os.remove(existing_file)

# Determine the number of files needed
total_missing = len(missing_translations)
num_files = (total_missing // max_translations) + (1 if total_missing % max_translations else 0)

# Write untranslated msgids along with their indices to the output file(s)
for file_index in range(num_files):
start_index = file_index * max_translations
end_index = start_index + max_translations

# Set the name of the output file based on the index
current_output_file = output_file_base_name
current_output_file += f"_{file_index + 1}" if num_files > 1 else ""
current_output_file += ".txt"

# Write untranslated msgids along with their indices to the output file
with open(current_output_file, "w", encoding="utf-8") as f:
for index, item in missing_translations[start_index:end_index]:
f.write(f"{index}:{item}\n")


def main() -> None:
args = parse_arguments()
missing_translations = extract_missing_translations(args.lang_code)
output_to_files(missing_translations, args.output_file, args.max_translations)


if __name__ == "__main__":
main()

0 comments on commit d606483

Please sign in to comment.