LibreTranslatePlus/Python/Application/LibreTranslatePlus.I18N.py

281 lines
11 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
if "LibreTranslatePlus" not in globals():
class LibreTranslatePlus:pass
class Anonymous(LibreTranslatePlus.Abstracts.Base): # LibreTranslatePlus.I18N
def __init__(self, ltp, inputs = None):
super().__init__(ltp)
self.__sentences = self.settings("default_texts", inputs)
if not isinstance(self.__sentences, dict):
self.__sentences = {}
else:
for_delete = []
for language, sentences in self.__sentences.items():
if not isinstance(sentences, dict):
for_delete.append(language)
for key in for_delete:
del self.__sentences[key]
self._print("info", "ltp_i18n_building")
self.__started = False
self.__language = None
self.__default_language = None
self.__show_exception = None
self.__key_to_code = {}
self.__code_to_key = {}
self.__set_default_variables()
self._print("ok", "ltp_i18n_built")
def start(self, callback = None):
self._print("info", "ltp_i18n_starting")
end = lambda status:callable(callback) and callback(status)
if self.__started:
self._print("warn", "ltp_i18n_already_started")
end(False)
return False
self.__started = True
self.__set_default_variables()
for key in ("default_i18n_files", "i18n_files"):
self.set(self.settings(key), True)
self.__language = self.settings(("default_language", "language"))
self.__default_language = self.__language
if not isinstance(self.__default_language, str) or not self.__default_language or self.__default_language not in self.__sentences:
languages = tuple(self.__sentences.keys())
self.__default_language = languages[0] if len(languages) else self.settings("unknown_language") or "UNKNOWN"
for key in ("default_codes_keys", "codes_keys"):
self.set_code_key(self.settings(key), True)
self.__language = self.__default_language
end(True)
self._print("ok", "ltp_i18n_started", {"language" : self.__language})
return True
def __set_default_variables(self):
self.__show_exception = self.settings(("i18n_show_exception", "show_exception"))
self.__language = self.settings(("default_language", "language"))
self.__default_language = self.__language
self.__show_set_code_key_exception = self.settings(("i18n_show_code_key_exception_message", "show_exception_message"))
self.__show_set_code_key_error = self.settings(("i18n_show_code_key_error_message", "show_error_message"))
self.__show_set_code_key_ok = self.settings(("i18n_show_code_key_ok_message", "show_ok_message"))
self.__show_get_code_error = self.settings(("i18n_show_code_error_message", "show_error_message"))
self.__show_get_code_ok = self.settings(("i18n_show_code_ok_message", "show_ok_message"))
self.__show_get_key_error = self.settings(("i18n_show_key_error_message", "show_error_message"))
self.__show_get_key_ok = self.settings(("i18n_show_key_ok_message", "show_ok_message"))
self.__show_validate_language_error = self.settings(("i18n_show_validate_language_error_message", "show_error_message"))
self.__show_validate_language_ok = self.settings(("i18n_show_validate_language_ok_message", "show_ok_message"))
def default_text(self, default = None):
return default if default != None else self.settings("default_text")
def __get(self, keys, default = None):
keys = tuple([key for key in (keys if isinstance(keys, (list, tuple)) else [keys]) if isinstance(key, str) and key])
if len(keys):
languages_used = []
for language in tuple([self.__language, self.__default_language] + list(self.__sentences.keys())):
if language not in languages_used:
languages_used.append(language)
if language in self.__sentences:
keys_used = []
for key in keys:
if key not in keys_used:
keys_used.append(key)
if key in self.__sentences[language]:
return self.__sentences[language][key]
return keys[0]
return self.default_text(default)
def get(self, keys, variables = None, _default = None):
string = self.__get(keys, _default)
return LibreTranslatePlus.string_variables("".join(string) if isinstance(string, (list, tuple)) else string, variables, _default)
def set(self, inputs, overwrite = None):
if isinstance(inputs, (list, tuple)):
for subinputs in inputs:
self.set(subinputs, overwrite)
elif isinstance(inputs, dict):
if not isinstance(overwrite, bool):
overwrite = self.settings(("i18n_set_overwrite", "i18n_overwrite", "overwrite"))
for language, sentences in inputs.items():
if isinstance(sentences, dict):
if language not in self.__sentences:
self.__sentences[language] = {}
for key, sentence in sentences.items():
if sentence != None and (overwrite or key not in self.__sentences[language]):
self.__sentences[language][key] = sentence
elif isinstance(inputs, str):
self.set(self.ltp.load_json(inputs, self.__show_exception)[0], overwrite)
def set_code_key(self, inputs, overwrite = None):
if isinstance(inputs, (list, tuple)):
for subinputs in inputs:
self.set_code_key(subinputs, overwrite)
elif isinstance(inputs, dict):
if not isinstance(overwrite, bool):
overwrite = self.settings(("i18n_code_key_overwrite", "i18n_overwrite", "overwrite"))
for code, key in inputs.items():
error = 0
try:
error = (
((
1 << 0 if code == None else
1 << 1 if not isinstance(code, str) else
1 << 2 if not code else
1 << 3 if not overwrite and code in self.__code_to_key else
0) << 0) |
((
1 << 0 if key == None else
1 << 1 if not isinstance(key, str) else
1 << 2 if not key else
1 << 3 if not overwrite and key in self.__key_to_code else
0) << 4) |
0) << 1
if not error:
self.__code_to_key[code] = key
self.__key_to_code[key] = code
except Exception as exception:
error |= 1 << 0
self.exception(exception, self.__show_set_code_key_exception and "ltp_i18n_set_code_key_exception", {
"code_language" : code,
"key_language" : key
})
self.validate(
error,
(
"exception"
"code_null",
"code_not_string",
"code_empty",
"code_exists",
"key_null",
"key_not_string",
"key_empty",
"key_exists"
),
{
"code_language" : code,
"key_language" : key
},
self.__show_set_code_key_error and "ltp_i18n_set_code_key_error",
self.__show_set_code_key_ok and "ltp_i18n_set_code_key_ok"
)
elif isinstance(inputs, str):
self.set_code_key(self.ltp.load_json(inputs, self.__show_exception), overwrite)
def get_code(self, key, show_errors = None):
error = (
1 << 0 if key == None else
1 << 1 if not isinstance(key, str) else
1 << 2 if not key else
1 << 3 if len(key) != 2 and key not in self.__key_to_code else
0)
has_show_errors = isinstance(show_errors, bool)
self.validate(
error,
(
"key_null",
"key_not_string",
"key_empty",
"key_invalid"
),
{
"key" : key
},
(show_errors if has_show_errors else self.__show_get_code_error) and "ltp_i18n_get_code_error",
(show_errors if has_show_errors else True) and self.__show_get_code_ok and "ltp_i18n_get_code_ok"
)
return (None if error else (self.__key_to_code[key] if key in self.__key_to_code else key), error)
def get_key(self, key, show_errors = None):
error = (
1 << 0 if key == None else
1 << 1 if not isinstance(key, str) else
1 << 2 if not key else
1 << 3 if len(key) == 2 and key not in self.__code_to_key else
0)
has_show_errors = isinstance(show_errors, bool)
self.validate(
error,
(
"key_null",
"key_not_string",
"key_empty",
"key_invalid"
),
{
"key" : key
},
(show_errors if has_show_errors else self.__show_get_key_error) and "ltp_i18n_get_key_error",
(show_errors if has_show_errors else True) and self.__show_get_key_ok and "ltp_i18n_get_key_ok"
)
return (None if error else (self.__code_to_key[key] if key in self.__code_to_key else key), error)
def validate_language(self, key, show_error = None):
error = (
1 << 0 if key == None else
1 << 1 if not isinstance(key, str) else
1 << 2 if not key else
1 << 3 if key not in self.__key_to_code and key not in self.__code_to_key else
0)
return (self.__key_to_code[key] if key in self.__key_to_code else key, error, self.validate(
error,
(
"language_key_null",
"language_key_not_string",
"language_key_empty",
"language_key_not_in_codes"
),
{
"key" : key
},
(show_error if isinstance(show_error, bool) else self.__show_validate_language_error) and "ltp_i18n_validate_language_error",
(show_error if isinstance(show_error, bool) else True) and self.__show_validate_language_ok and "ltp_i18n_validate_language_ok"
))
def get_keys(self):
return tuple(self.__key_to_code.keys())
LibreTranslatePlus.I18N = Anonymous
del globals()["Anonymous"]