Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: start GUI in chinese if chinese was selected during windows installation #59

Merged
merged 4 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def rename_fc_connection(self, selected_file):
self.local_filesystem.file_parameters[selected_file].pop(old_name)
logging_info(_("Renaming parameter %s to %s"), old_name, new_name)
info_msg = _("The parameter '{old_name}' was renamed to '{new_name}'.\n"
"to follow the flight controller connection defined in the component editor window.")
"to obey the flight controller connection defined in the component editor window.")
messagebox.showinfo(_("Parameter Renamed"), info_msg.format(**locals()))

def __update_table(self, params, fc_parameters):
Expand Down
17 changes: 13 additions & 4 deletions MethodicConfigurator/internationalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@

import argparse
import gettext
from os import path as os_path
import builtins

# Do not import nor use logging functions in this file.
# Logging is not yet configured when these functions are called

LANGUAGE_CHOICES = ['en', 'zh_CN']
LANGUAGE_CHOICES = ['en', 'zh_CN', 'pt']


def identity_function(s):
Expand All @@ -33,8 +36,14 @@ def load_translation() -> callable:

# Load the correct language ASAP based on the command line argument
try:
translation = gettext.translation('MethodicConfigurator', localedir='locale',
script_dir = os_path.dirname(os_path.abspath(__file__))
locale_dir = os_path.join(script_dir, 'locale')
translation = gettext.translation('MethodicConfigurator', localedir=locale_dir,
languages=[pre_args.language], fallback=False)
translation.install()
# Do not use logging functions here the logging system has not been configured yet
# Do not translate this message, the translation will not work here anyways
print("Loaded %s translation.", pre_args.language)
return translation.gettext
except FileNotFoundError:
# Do not use logging functions here the logging system has not been configured yet
Expand All @@ -44,5 +53,5 @@ def load_translation() -> callable:


# Default to identity function if _ is not already defined
if '_' not in globals():
_ = identity_function
if '_' not in globals() and '_' not in locals() and '_' not in builtins.__dict__:
_ = load_translation()
6 changes: 3 additions & 3 deletions MethodicConfigurator/locale/MethodicConfigurator.pot
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR ORGANIZATION
# ArduPilot Methodic Configurator.
# Copyright (C) 2024 Amilcar Lucas, ArduPilot.org
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
msgid ""
Expand Down Expand Up @@ -1629,7 +1629,7 @@ msgstr ""
#: MethodicConfigurator/frontend_tkinter_parameter_editor_table.py:179
msgid ""
"The parameter '{old_name}' was renamed to '{new_name}'.\n"
"to follow the flight controller connection defined in the component editor window."
"to obey the flight controller connection defined in the component editor window."
msgstr ""

#: MethodicConfigurator/frontend_tkinter_parameter_editor_table.py:181
Expand Down
Binary file not shown.
1,891 changes: 1,891 additions & 0 deletions MethodicConfigurator/locale/pt/LC_MESSAGES/MethodicConfigurator.po

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3

'''
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator

SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <amilcar.lucas@iav.de>

SPDX-License-Identifier: GPL-3.0-or-later
'''

import os
import gettext

def extract_missing_translations(po_file, output_file):
# Set up the translation catalog
language = gettext.translation('messages', localedir=os.path.dirname(po_file), languages=['zh_CN'], fallback=True)

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

missing_translations = []

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

if line.startswith('msgid'):
msgid = line.split('"')[1] # Get the msgid string

# Check if the translation exists
if language.gettext(msgid) == msgid: # If translation is the same as msgid, it's missing
missing_translations.append((i, msgid))

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

if __name__ == "__main__":
extract_missing_translations('MethodicConfigurator.po', 'missing_translations.txt')
45 changes: 45 additions & 0 deletions MethodicConfigurator/locale/pt/LC_MESSAGES/insert_translations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python3

'''
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator

SPDX-FileCopyrightText: 2024 Amilcar do Carmo Lucas <amilcar.lucas@iav.de>

SPDX-License-Identifier: GPL-3.0-or-later
'''

def insert_translations(po_file, translations_file, output_file):
with open(po_file, 'r', encoding='utf-8') as f:
lines = f.readlines()

with open(translations_file, 'r', encoding='utf-8') as f:
translations_data = f.read().strip().split('\n')

# Prepare to insert translations
translations = []
for data in translations_data:
index, translation = data.split(':', 1) # Split the index and the translation
translations.append((int(index), translation.strip())) # Store index and translation as tuple

insertion_offset = 0 # To track how many lines we've inserted
# To insert the translations correctly
for index, translation in translations:
# Adjust index accounting for previously inserted lines
adjusted_index = index + insertion_offset

# Check if the next line is an empty msgstr
if (adjusted_index + 1 < len(lines) and
lines[adjusted_index + 1].strip() == 'msgstr ""'):
# Overwrite the empty msgstr line
lines[adjusted_index + 1] = f'msgstr "{translation}"\n'
else:
# Otherwise, insert a new msgstr line
lines.insert(adjusted_index + 1, f'msgstr "{translation}"\n')
insertion_offset += 1 # Increment the offset for each insertion

# Writing back to a new output file
with open(output_file, 'w', encoding='utf-8') as f:
f.writelines(lines)

if __name__ == "__main__":
insert_translations('MethodicConfigurator.po', 'translations.txt', 'updated_MethodicConfigurator.po')
Binary file not shown.
Loading