localizer package#
Subpackages#
Submodules#
localizer.FileHandler module#
- class localizer.FileHandler.FileHandler[source]#
Bases:
object
- static export(file: str, texts: dict[str, str] | set[str]) None [source]#
A function to export the texts to a file.
- Args:
file (str): The filepath/filename to the file which will hold the data. (The file is created if it doesn’t exist).
texts (dict[str, str] | set[str]): A dictionary with key: value pairs where key is the original text and value is the translated text, or a set where each value is an original text where there is no translation for it.
- Raises:
NotImplementedError: This is always raised when calling FileHandle.export, you should call the export method of the child classes.
- static parse(file: str) dict[str, str] | set[str] [source]#
A function to parse a file and extract it’s data.
- Args:
file (str): The filepath/filename of the file to process.
- Raises:
NotImplementedError: This is always raised when calling FileHandler.parse, you should call the parse method of the child classes.
- Returns:
dict[str, str]: A dictionary with key: value pairs where key is the original text and value is the translated text, or a set where each value is an original text where there is no translation for it.
localizer.examples module#
- localizer.examples.creating_language_packs()[source]#
You can use the localizer_pack_fill.py script to generate language packs. Run localizer_pack_fill.py -h for the help menu.
- localizer.examples.easy_mode()[source]#
In the source code of this function you will find information on how to use this library in easy mode.
- localizer.examples.supported_file_extensions()[source]#
In the source code of this function you will find how to view all supported_file_extensions as well as how to add support for other file_extensions.
localizer.language_pack module#
- class localizer.language_pack.LanguagePack[source]#
Bases:
object
- add_translation(original: str, translated: str | None = None) None [source]#
Add a translation for a sentence in the language pack. If the translated text is empty, original is added to new_texts.
If self.auto_translate is True, an attempt to automatically translate the original using a translator API is made.
- Args:
original (str): The original sentence.
translated (Optional[str], optional): The translated sentence. Defaults to None
- export_file(file: str) None [source]#
Exports the data of the language pack to the specified file. The data is processed and stored using the FileHandler for the specific file format.
- Args:
file (str): A string with the filepath/filename of the file. (The file will be created if it doesn’t exist).
- get_file_extension(filename: str) str | None [source]#
Finds and returns the extension of the file ONLY if that extension is supported.
- Args:
filename (str): The name of the file.
- Returns:
Optional[str]: The extension of the file (if the extension is supported), or None (if the extension is NOT supported).
- gettext(text: str) str [source]#
Get the translation of text if it exists, otherwise fallback to the original translation.
- Args:
text (str): The text to be translated.
- Returns:
str: The translated text, or the original text (If no translation was found).
- parse_file(file: str) None [source]#
Parses the data of file and stores them in the language pack. The data is processed and stored using the FileHandler for the specific file format.
- Args:
file (str): A string with the filepath/filename of the file.
- Raises:
TypeError: If there is no handler available to process this file.
- set_file_extension(extension: str, fhandler: type[localizer.FileHandler.FileHandler])[source]#
Creates a connection between the specified extension and the specified FileHandler which allows to later process files of that extension with the correct FileHandler.
- Args:
extension (str): The extension of the file that the Handler can process (Example, .json, .csv).
fhandler (type[FileHandler]): A reference to the class holding the static methods for parsing and exporting the files.
- translate(query_text: str, to_language: str | None = None, from_language: str | None = None, translators: list[str] | None = None, tries: int | None = None, auto_add: bool = True) str | None [source]#
Translate query_text from from_language to to_language using one of the translators (starting from the first and using the others as fallbacks). You also have the option to automatically add the translation to the pack (if the translation succeeds) using auto_add=True
- Args:
query_text (str): The original text to translate. to_language (Optional[str], optional): The code of the language for the translated text. Defaults to None. from_language (Optional[str], optional): The code of the language for the original text (or auto). Defaults to None. translators (Optional[list[str]], optional): A list of translators to use (first is primary and the others are fallbacks). Defaults to None. tries (Optional[int], optional): How many tries before going to the next translator. Defaults to None. auto_add (bool, optional): Wether to add the translation to the pack or return it to the program. Defaults to True.
- Returns:
Optional[str]: If auto_add is True the return value is None, if auto_add is False the return value is the translated text.
localizer.print_replace module#
- localizer.print_replace.input(_prompt: object = '') str [source]#
An emulated version of the default input. Allows localizer to intercept the _prompt and provide an easy way to translate it to different languages.
- Args:
_prompt (object, optional): Any object that implements repr or str. Defaults to “”.
- Returns:
str: A string that’s typed to the console (the string is read by the default input function).
- localizer.print_replace.print(*values: object, sep: str | None = ' ', end: str | None = '\n', file: IO | None = None, flush: bool = False) None [source]#
An emulated version of the default print. Allows localizer module to intercept the values passed to print and provide an easy way to translate them to different languages.
- Args:
sep (Optional[str], optional): The separator, a value that’s placed between the values. Defaults to ‘ ‘.
end (Optional[str], optional): The ending value, a value that’s placed at the end of the whole value list. Defaults to ‘\n’.
file (Optional[IO], optional): The file object in which to write the buffer, None is replaced with STDOUT. Defaults to None.
flush (bool, optional): Wether to flush the buffer or not. Defaults to False.
- Raises:
TypeError: If sep is not of type str and not None. TypeError: If end is not of type str and not None.