Skip to content

Commit

Permalink
FEATURE: Add user interface translation support (internationalization)
Browse files Browse the repository at this point in the history
Add scripts to create and update translations
Allow UI language to be defined as a command line argument
  • Loading branch information
amilcarlucas committed Oct 30, 2024
1 parent 1955214 commit ae4e303
Show file tree
Hide file tree
Showing 7 changed files with 1,526 additions and 11 deletions.
5 changes: 3 additions & 2 deletions MethodicConfigurator/ardupilot_methodic_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

from MethodicConfigurator.frontend_tkinter_parameter_editor import ParameterEditorWindow

from MethodicConfigurator.common_arguments import add_common_arguments_and_parse
from MethodicConfigurator.common_arguments import add_common_arguments_and_parse, LANGUAGE_CHOICES

from MethodicConfigurator.internationalization import _
from MethodicConfigurator.internationalization import _, load_translation

from MethodicConfigurator.version import VERSION

Expand Down Expand Up @@ -117,6 +117,7 @@ def component_editor(args, flight_controller, vehicle_type, local_filesystem, ve


def main():
load_translation()
args = argument_parser()

logging_basicConfig(level=logging_getLevelName(args.loglevel), format='%(asctime)s - %(levelname)s - %(message)s')
Expand Down
11 changes: 9 additions & 2 deletions MethodicConfigurator/common_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@
'''

from MethodicConfigurator.version import VERSION
from MethodicConfigurator.internationalization import _

from MethodicConfigurator.internationalization import _, LANGUAGE_CHOICES


def add_common_arguments_and_parse(parser):
parser.add_argument('--loglevel',
type=str,
default='INFO',
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help=_('Logging level (default is INFO).'))
help=_('Logging level (default is %(default)s).'))
parser.add_argument('-v', '--version',
action='version',
version=f'%(prog)s {VERSION}',
help=_('Display version information and exit.'))
parser.add_argument('--language',
type=str,
default='en',
choices=LANGUAGE_CHOICES,
help=_('User interface language (default is %(default)s).'))
return parser.parse_args()
34 changes: 27 additions & 7 deletions MethodicConfigurator/internationalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,35 @@
SPDX-License-Identifier: GPL-3.0-or-later
'''

import argparse
import gettext

LANGUAGE_CHOICES = ['en']

# Setup language
locale_path = 'locale' # directory of locale file
language = 'zh_CN' # select language

# create translation
translation = gettext.translation('messages', localedir=locale_path, languages=[language], fallback=True)
def identity_function(s):
return s

# set translation object as _()
_ = translation.gettext

def load_translation() -> None:
# First, pre-parse to find the --language argument
pre_parser = argparse.ArgumentParser(add_help=False)
pre_parser.add_argument('--language', type=str, default='en', choices=LANGUAGE_CHOICES)
pre_args, _list_str = pre_parser.parse_known_args()

# modify the global _() function
global _ # pylint: disable=global-statement

# Load the correct language ASAP based on the command line argument
try:
translation = gettext.translation('MethodicConfigurator', localedir='locale',
languages=[pre_args.language], fallback=False)
_ = translation.gettext
except FileNotFoundError:
_ = identity_function # Return identity function
print(_("Translation files not found for the selected language. Falling back to default."))


# Default to identity function if _ is not already defined
if '_' not in globals():
_ = identity_function
Loading

0 comments on commit ae4e303

Please sign in to comment.