localizer package - Localizer 1.3 documentation

Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# localizer package

## Subpackages

- localizer.supported_file_types package
  - Submodules
  - localizer.supported_file_types.dot_csv module
    - `FHandler`
      - `FHandler.export()`
      - `FHandler.parse()`
  - localizer.supported_file_types.dot_json module
    - `FHandler`
      - `FHandler.export()`
      - `FHandler.parse()`
  - Module contents

## Submodules

## localizer.FileHandler module

class localizer.FileHandler.FileHandler

  Bases: `object`

  static export(file: str, texts: dict[str, str] | set[str]) → None

    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]

    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()

  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()

  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()

  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.examples.translation_using_APIs()

  In the source code of this function you will find information on how to use translate texts using web APIs.

localizer.examples.using_a_language_pack()

  In the source code of this function you will find information on how to use a language pack object.

## localizer.language_pack module

class localizer.language_pack.LanguagePack

  Bases: `object`

  add_translation(original: str, translated: str | None = None) → None

    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

    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

    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

    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

    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])

    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

    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.

  translate_all()

    Asynchronously translate all new_texts and store them in the pack.

    NOTE: The options are the LanguagePack’s attributes self.translat*

localizer.language_pack.count_spaces(text: str) → int

## localizer.print_replace module

localizer.print_replace.input(_prompt: object = '') → str

  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

  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.

## Module contents