### Install l10n with CLI support Source: https://l10n.orsinium.dev/quickstart Install the l10n library with the necessary components for command-line interface usage. ```bash python3 -m pip install 'l10n[cli]' ``` -------------------------------- ### Install Project with Flit Source: https://l10n.orsinium.dev/intro Installs the project and its development dependencies using the flit build tool. ```bash python3 -m pip install -U flit flit install --deps=develop --symlink ``` -------------------------------- ### Create Main Entry Point Source: https://l10n.orsinium.dev/intro Defines the main entry point for the package, importing and running the main function from the example module. ```python from .example import main main() ``` -------------------------------- ### Define l10n Locales and Say Hello Function Source: https://l10n.orsinium.dev/intro Sets up the Locales class and a function to retrieve and print a localized greeting. Requires 'l10n' to be installed. ```python from argparse import ArgumentParser from l10n import Locales locales = Locales() def say_hello(lang: str = 'en') -> None: loc = locales[lang] msg = loc.get('Hello, world!') print(msg) def main() -> None: parser = ArgumentParser() parser.add_argument('--lang', default='en') args = parser.parse_args() say_hello(lang=args.lang) ``` -------------------------------- ### Initialize l10n and get localized strings Source: https://l10n.orsinium.dev/quickstart Initialize the Locales object and retrieve localized strings within your Python code. Ensure the Locales object is initialized once. ```python from l10n import Locales locales = Locales() def say_hello(lang='en'): loc = locales[lang] msg = loc.get('Hello, world!') print(msg) ``` -------------------------------- ### Create Project Directory Source: https://l10n.orsinium.dev/intro Initializes a new project directory for the l10n playground. ```bash mkdir l10n-playground cd l10n-playground ``` -------------------------------- ### Configure Project with pyproject.toml Source: https://l10n.orsinium.dev/intro Sets up project metadata, build system, and dependencies using pyproject.toml, including l10n and its development dependencies. ```toml [build-system] requires = ["flit_core >=3.2,<4"] build-backend = "flit_core.buildapi" [project] name = "l10n_playground" dynamic = ["version", "description"] requires-python = ">=3.7" dependencies = ["l10n"] [project.optional-dependencies] dev = ["l10n[cli]"] ``` -------------------------------- ### Initialize Package Version Source: https://l10n.orsinium.dev/intro Defines the package version in the main package file. ```python """Experimenting with l10n""" __version__ = "1.0.0" ``` -------------------------------- ### Run Application with Specified Language Source: https://l10n.orsinium.dev/_sources/intro.md.txt Run the application and specify a language code (e.g., 'ru' for Russian) using the `--lang` flag to display messages in that locale. ```bash $ python3 -m l10n_playground --lang=ru Привет, мир! ``` -------------------------------- ### Run Application with Default Language Source: https://l10n.orsinium.dev/_sources/intro.md.txt Execute the application using the default language settings. The output will display messages in the default locale. ```bash $ python3 -m l10n_playground Hello, world! ``` -------------------------------- ### Run l10n Playground App Source: https://l10n.orsinium.dev/intro Execute the l10n playground application to see default and translated messages. Use the --lang option to specify a language. ```bash $ python3 -m l10n_playground Hello, world! $ python3 -m l10n_playground --lang=ru Привет, мир! ``` -------------------------------- ### Compile Translations with l10n Source: https://l10n.orsinium.dev/intro Use the `l10n compile` command to generate MO files from your PO translation files. ```bash l10n compile ``` -------------------------------- ### Compile translations Source: https://l10n.orsinium.dev/quickstart Compile the translated string files into a binary format for efficient runtime access. This step is necessary after extracting or translating strings. ```bash python3 -m l10n compile ``` -------------------------------- ### Format Strings with Placeholders Source: https://l10n.orsinium.dev/advanced Use `str.format` with placeholders for dynamic content in translations. This ensures placeholders are not translated by localization tools. ```python loc = locales[lang] msg = loc.get('Hello, {user_name}!').format(user_name=user.name) ``` -------------------------------- ### Translate Messages with l10n Source: https://l10n.orsinium.dev/intro Run the `l10n translate` command to automatically translate all PO files using a translation service. ```bash l10n translate ``` -------------------------------- ### Locales Class Source: https://l10n.orsinium.dev/api Manages collections of compiled locale (.mo) files. ```APIDOC ## Locales Class ### Description Class allowing you to work with compiled (.mo) files collection. ### Parameters * **path** (Path | None) - where compiled locales are located. * **format** (str) - file name template for compiled locales. Default: '{language}.mo' ### Methods #### get(_language : str_) Find locale for the given language. #### get_cached(_language : str_) The same as get but caches the returned Locale. ### Properties * **languages** (frozenset[str]) - List all languages for which a locale is available in the catalog. * **locales** (tuple[Locale, ...]) - List all locales available in the catalog. * **system_locale** (Locale | None) - The Locale for the default system language. * **system_language** (str | None) - The current system language detected from env vars. ``` -------------------------------- ### Extract Messages for Localization Source: https://l10n.orsinium.dev/intro Generates PO translation files for specified languages from the project's source code. ```bash python3 -m l10n extract --lang=en python3 -m l10n extract --lang=ru ``` -------------------------------- ### Translate extracted strings Source: https://l10n.orsinium.dev/quickstart Automatically translate the extracted strings using a service like Google Translate. This command processes the extracted PO files. ```bash python3 -m l10n translate ``` -------------------------------- ### Extract strings for translation Source: https://l10n.orsinium.dev/quickstart Use the l10n CLI to extract all strings marked for translation from your codebase for a specified language. ```bash python3 -m l10n extract --lang uk ``` -------------------------------- ### Use translated strings in code Source: https://l10n.orsinium.dev/quickstart Import and use your translated function after the l10n compilation step. The output will now be in the target language. ```python from example import say_hello say_hello(lang='uk') # Привіт Світ! ``` -------------------------------- ### Locale Class Source: https://l10n.orsinium.dev/api Represents a specific locale and provides methods for translation and formatting. ```APIDOC ## Locale Class ### Methods #### reset_cache() -> None Reset all the cached values for the locale object. Use it if you need to reload the mo file. #### get(_message : str_, _context : str | None = None_, _plural : str | None = None_, _n : int | None = None_, _comment : str = ''_) -> str Get translation for the message from the catalog (mo file). If no translation found, the message itself will be used. ### Parameters * **message** (str) - the message to translate. Represented as msgid in PO files. * **context** (str | None) - kind of namespace for translations. Represented as msgctxt in PO files. * **plural** (str | None) - the default value if n > 1 and no translation found. Represented as msgid_plural in PO files. * **n** (int | None) - the number used to pick a plural form for the translation. * **comment** (str) - not used in runtime but included in PO files. Use it to provide additional information for translators. ### Properties * **language** (str) - The language of the Locale. * **currency_symbol** (str) - The symbol used to denote the currency. * **decimal_dot** (str) - The symbol used to separate whole and fractional parts in decimal numbers. * **thousands_separator** (str) - The symbol used to separate groups of digits in big numbers. ### Formatting Methods #### format_date(_date : date_) -> str Format date. You need the locale to be compiled in your OS. #### format_time(_time : time_) -> str Format time. You need the locale to be compiled in your OS. #### format_datetime(_dt : datetime_) -> str Format date and time. You need the locale to be compiled in your OS. #### format_month(_n : int_, _abbreviate : bool = False_) -> str Format the month number into its name. #### format_dow(_n : int_, _abbreviate : bool = False_, _sunday : int = 0_) -> str Format the day of week. ### Parameters for format_dow * **abbreviate** (bool) - Use the short version of the DoW. * **sunday** (int) - Specify what number is Sunday (0 or 1). Default: 0. #### format_currency(_val : int | float | Decimal_, _symbol : bool = True_, _international : bool = False_, _grouping : bool = False_) -> str Format a price in the locale currency. ### Parameters for format_currency * **val** (int | float | Decimal) - The value to format. * **symbol** (bool) - Include the currency symbol. Default: True. * **international** (bool) - Use international currency symbol. Default: False. * **grouping** (bool) - Add thousands separator. Default: False. #### format_float(_n : int | float | Decimal_, _grouping : bool = False_, _monetary : bool = False_, _precision : int | None = None_, _strip_zeros : bool | None = None_, _exp : bool = False_) -> str Format float or Decimal with the correct decimal dot. ### Parameters for format_float * **n** (int | float | Decimal) - The number to format. * **grouping** (bool) - Add thousands separator. Default: False. * **monetary** (bool) - Use monetary thousands separator. Default: False. * **precision** (int | None) - Number of digits in the decimal part. * **strip_zeros** (bool | None) - Remove trailing zeros from the end. * **exp** (bool) - Format using exponential notation. Default: False. ``` -------------------------------- ### Handling Plural Forms Source: https://l10n.orsinium.dev/advanced Use the `n` argument in `Locale.get` to select the correct plural form for a given number. The `plural` argument provides a default message for non-singular forms when no translation is found. ```python n_msgs = 13 locale = Locales()['ru'] msg = locale.get('{n} message(s)', n=n_msgs).format(n=n_msgs) ``` ```python msg = locale.get('{n} message', n=n_msgs, plural='{n} messages').format(n=n_msgs) ``` -------------------------------- ### View Automatically Translated Message Source: https://l10n.orsinium.dev/intro Shows the result of automatic translation in the PO file, including a fuzzy flag. ```po #: ./l10n_playground/example.py:9 #, fuzzy msgid "Hello, world!" msgstr "Привет, мир!" ``` -------------------------------- ### format_int Source: https://l10n.orsinium.dev/api Formats an integer with grouping (thousands separator). ```APIDOC ## format_int ### Description Format an integer with grouping (thousands separator). ### Parameters * **_n** (int) - The integer to format. * **_grouping** (bool) - Add thousands separator. Defaults to True. ### Returns str - The formatted integer as a string. ``` -------------------------------- ### Extract Translations with l10n Source: https://l10n.orsinium.dev/intro Use the `l10n extract` command to update PO files with new or changed messages. It automatically updates all existing PO files. ```bash l10n extract --lang=nl ``` ```bash l10n extract ``` -------------------------------- ### format_decimal Source: https://l10n.orsinium.dev/api Formats a float or Decimal with the correct decimal dot, supporting grouping, monetary separators, precision, stripping trailing zeros, and scientific notation. ```APIDOC ## format_decimal ### Description Format float or Decimal with the correct decimal dot. ### Parameters * **_n** (int | float | Decimal_) - The number to format. * **_grouping** (bool) - Add thousands separator. Defaults to False. * **_monetary** (bool) - Use monetary thousands separator. Defaults to False. * **_precision** (int | None) - If specified, include exactly so many digits in the decimal part. Defaults to None. * **_strip_zeros** (bool | None) - Remove trailing zeros from the end. Defaults to True if precision is not specified, otherwise False. * **_exp** (bool) - If True, represent large numbers in scientific exponent notation. Defaults to False. ### Returns str - The formatted number as a string. ``` -------------------------------- ### parse_int Source: https://l10n.orsinium.dev/api Converts a string generated by Locale.format_int back into an integer. ```APIDOC ## parse_int ### Description Convert string generated by Locale.format_int back into int. ### Parameters * **_s** (str) - The string to parse. ### Returns int - The parsed integer value. ``` -------------------------------- ### Include Additional Strings with TYPE_CHECKING Source: https://l10n.orsinium.dev/advanced Use the `TYPE_CHECKING` block to include strings in translations that are not evaluated at runtime. This ensures strings are extracted by `l10n extract` without affecting program execution. ```python from typing import TYPE_CHECKING if TYPE_CHECKING: from l10n import Locales loc = Locales()["en"] loc.get('hello world') loc.get('oh hi mark') ``` -------------------------------- ### Extract Messages for a Specific Language Source: https://l10n.orsinium.dev/_sources/intro.md.txt Use `l10n extract --lang=nl` to generate or update the PO file specifically for the Dutch language. ```bash l10n extract --lang=nl ``` -------------------------------- ### translate_currency Source: https://l10n.orsinium.dev/api Translates a currency name from English to the specified language, following ISO 4217. ```APIDOC ## translate_currency ### Description Translate currency name from English to the given language. Follows ISO 4217. Requires iso-codes package to be installed. ### Parameters * **_currency_name** (str) - The currency name in English. ### Returns str - The translated currency name. ``` -------------------------------- ### Include Additional Strings with TYPE_CHECKING Source: https://l10n.orsinium.dev/_sources/advanced.md.txt Use TYPE_CHECKING to include strings in translations that are not evaluated at runtime. This leverages the static type checker for extraction. ```python from typing import TYPE_CHECKING if TYPE_CHECKING: from l10n import Locales loc = Locales()['en'] loc.get('hello world') loc.get('oh hi mark') ``` -------------------------------- ### parse_float Source: https://l10n.orsinium.dev/api Converts a string generated by Locale.format_float back into a float. ```APIDOC ## parse_float ### Description Convert string generated by Locale.format_float back into float. ### Parameters * **_s** (str) - The string to parse. ### Returns float - The parsed float value. ``` -------------------------------- ### Manually Translate Message Source: https://l10n.orsinium.dev/intro Edits the extracted PO file to provide a manual translation for a message. ```po #: ./l10n_playground/example.py:9 msgid "Hello, world!" msgstr "Hello, world!" ``` -------------------------------- ### translate_country Source: https://l10n.orsinium.dev/api Translates a country name from English to the specified language, following ISO 3166-1. ```APIDOC ## translate_country ### Description Translate country name from English to the given language. Follows ISO 3166-1. Requires iso-codes package to be installed. ### Parameters * **_country_name** (str) - The country name in English. ### Returns str - The translated country name. ``` -------------------------------- ### translate_language Source: https://l10n.orsinium.dev/api Translates a language name from English to the specified language, following ISO 639-2 and ISO 639-3. ```APIDOC ## translate_language ### Description Translate language name from English to the given language. Follows ISO 639-2 and ISO 639-3. Requires iso-codes package to be installed. ### Parameters * **_language_name** (str) - The language name in English. ### Returns str - The translated language name. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.