-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathinternationalization.py
54 lines (41 loc) · 2.16 KB
/
internationalization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Translations to other written languages.
This file is part of Ardupilot methodic configurator. https://github.com/ArduPilot/MethodicConfigurator
SPDX-FileCopyrightText: 2024-2025 Amilcar do Carmo Lucas <amilcar.lucas@iav.de>
SPDX-License-Identifier: GPL-3.0-or-later
"""
import argparse
import gettext
from os import path as os_path
from typing import Callable
# 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", "pt", "de", "it", "ja"]
def identity_function(s: str) -> str:
return s
def load_translation() -> Callable[[str], str]:
default_language = LANGUAGE_CHOICES[0]
# First, pre-parse to find the --language argument
pre_parser = argparse.ArgumentParser(add_help=False)
pre_parser.add_argument("--language", type=str, default=default_language, choices=LANGUAGE_CHOICES)
pre_args, _list_str = pre_parser.parse_known_args()
if pre_args.language == default_language:
return identity_function
# Load the correct language ASAP based on the command line argument
try:
script_dir = os_path.dirname(os_path.abspath(__file__))
locale_dir = os_path.join(script_dir, "locale")
print(f"Searching for translation files on {locale_dir}") # noqa: T201
translation = gettext.translation(
"ardupilot_methodic_configurator", 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(f"Loaded {pre_args.language} translation files.") # noqa: T201
return translation.gettext
except FileNotFoundError:
# 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(f"{pre_args.language} translation files not found. Using default {default_language} language.") # noqa: T201
return identity_function # Return identity function on error