API Reference¶
API reference for flufl.i18n
:
- flufl.i18n.initialize(domain: str) flufl.i18n.types.RuntimeTranslator ¶
A convenience function for setting up translation.
- Parameters
domain – The application’s name.
- Returns
The translation function, typically bound to _()
- class flufl.i18n.Application(strategy: flufl.i18n.types.TranslationStrategy)¶
Manage all the catalogs for a particular application.
You can ask the application for a specific catalog based on the language code. The Application requires a strategy for finding catalog files.
Attributes:
dedent (default True) - controls whether translated strings are dedented or not. This is passed through to the underlying Translator instance.
depth (default 2) - The number of stack frames to call sys._getframe() with in the underlying Translator instance. Passed through to that class’s constructor.
- property code: Optional[str]¶
Return the current language code.
- Returns
The current language code, or None if there isn’t one.
- property current: flufl.i18n._translator.Translator¶
Return the current translator.
- Returns
The current translator.
- property default: Optional[str]¶
The default language code, if there is one.
- Returns
The default language code or None.
- defer() None ¶
Push a deferred (i.e. null) translation context onto the stack.
This is primarily used to support the
_.defer_translation()
context manager.
- get(language_code: str) gettext.NullTranslations ¶
Get the catalog associated with the language code.
- Parameters
language_code – The language code.
- Returns
A gettext catalog.
- property name: str¶
The application name.
- Returns
The application name.
- pop() None ¶
Pop the current catalog off the translation stack.
No exception is raised for under-runs. In that case, pop() just no-ops and the null translation becomes the current translation context.
- push(language_code: str) None ¶
Push a new catalog onto the stack.
The translation catalog associated with the language code now becomes the currently active translation context.
- Parameters
language_code – The language code for the translation context.
- class flufl.i18n.PackageStrategy(name: str, package: module)¶
A strategy that finds catalogs based on package paths.
- class flufl.i18n.SimpleStrategy(name: str)¶
A simpler strategy for getting translations.
- flufl.i18n.expand(template: str, substitutions: typing.Dict[str, str], template_class: type = <class 'string.Template'>) str ¶
Expand string template with substitutions.
- Parameters
template – A PEP 292 $-string template.
substitutions – The substitutions dictionary.
template_class – The template class to use.
- Returns
The substituted string.
- flufl.i18n.registry()¶
A registry of application translation lookup strategies.
Types:
- class flufl.i18n.RuntimeTranslator¶
- property code: Optional[str]¶
The language code currently in effect, if there is one.
- property default: Optional[str]¶
Return the default language code.
- Returns
The default language code, or None if there is no default language.
- abstract defer_translation() flufl.i18n.types.TranslationContextManager ¶
Push a NullTranslations onto the stack.
This is useful for when you want to mark strings statically for extraction but you want to defer translation of the string until later.
- Returns
The NULLTranslations context.
- abstract pop() None ¶
Pop the current catalog off the translation stack.
No exception is raised for under-runs. In that case, pop() just no-ops and the null translation becomes the current translation context.
- abstract push(language_code: str) None ¶
Push a new catalog onto the stack.
The translation catalog associated with the language code now becomes the currently active translation context.
- Parameters
language_code – The language code for the translation context.
- abstract using(language_code: str) flufl.i18n.types.TranslationContextManager ¶
Create a context manager for temporary translation.
While in this context manager, translations use the given language code. When the with statement exits, the original language is restored. These are nestable.
- Parameters
language_code – The language code for the translation context.
- Returns
The new translation context.
- class flufl.i18n.TranslationContextManager¶
Context manager for translations in a particular language.