### Install Quart-Babel Python Package Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/installation.rst Installs the Quart-Babel extension and its required dependencies (Quart, Babel, pytz) using the pip package manager. Ensure Python 3.7 or higher is installed; Python 3.8+ is recommended to avoid specific error messages. ```Console pip install quart-babel ``` -------------------------------- ### Install Quart-Babel Extension Source: https://github.com/quart-addons/quart-babel/blob/master/README.md This command installs the Quart-Babel extension using pip3, ensuring all necessary dependencies like babel and pytz are also installed. ```Shell $ pip3 install quart-babel ``` -------------------------------- ### Initialize a New Translation Language Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/translating.rst To start a new translation, use `pybabel init` with the generated `messages.pot` file, specifying the desired output directory (e.g., `translations`) and the target language locale (e.g., `de` for German). This creates a `.po` file ready for translation. ```shell $ pybabel init -i messages.pot -d translations -l de ``` -------------------------------- ### Format Datetime with Quart-Babel Examples Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/formatting_dates.rst Illustrates various ways to format a `datetime` object using `format_datetime` from `quart_babel`. Examples include default formatting, 'full', 'short', and custom format strings, showcasing the flexibility of the function. ```python from quart_babel import format_datetime from datetime import datetime # Default format result_default = format_datetime(datetime(1987, 3, 5, 17, 12)) # result_default: u'Mar 5, 1987 5:12:00 PM' # Full format result_full = format_datetime(datetime(1987, 3, 5, 17, 12), 'full') # result_full: u'Thursday, March 5, 1987 5:12:00 PM World (GMT) Time' # Short format result_short = format_datetime(datetime(1987, 3, 5, 17, 12), 'short') # result_short: u'3/5/87 5:12 PM' # Custom format 'dd mm yyy' result_custom1 = format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyy') # result_custom1: u'05 12 1987' # Custom format 'dd mm yyyy' result_custom2 = format_datetime(datetime(1987, 3, 5, 17, 12), 'dd mm yyyy') # result_custom2: u'05 12 1987' ``` -------------------------------- ### Configure Babel Mapping for Python and Jinja2 Files Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/translating.rst Create a `babel.cfg` file to specify which file types (e.g., Python source files, Jinja2 templates) Babel should scan for translatable strings. This configuration guides the string extraction process. ```ini [python: **.py] [jinja2: **/templates/**.html] ``` -------------------------------- ### Get Current Locale (Quart-Babel) Source: https://github.com/quart-addons/quart-babel/blob/master/docs/references/api/locale.rst Retrieves the current active locale for the Quart application. This function typically determines the locale based on request headers or application configuration. ```APIDOC quart_babel.get_locale() Purpose: Retrieves the current active locale for the Quart application. Returns: str: The current locale string (e.g., 'en', 'fr_FR'). ``` -------------------------------- ### Initialize Quart-Babel Extension Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/configuration.rst Demonstrates two methods for initializing the Quart-Babel extension: direct instantiation with the application object and using the factory `init_app` method for deferred initialization. ```python from quart import Quart from quart_babel import Babel app = Quart(__name__) babel = Babel(app) ``` ```python babel.init_app(app) ``` -------------------------------- ### Set up Quart Test Request Context Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/formatting_dates.rst Demonstrates how to push a test request context onto the application stack, which is necessary for testing date formatting functions outside of a live request environment. ```python app.test_request_context().push() ``` -------------------------------- ### Extract Translatable Strings to a POT File Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/translating.rst Use the `pybabel extract` command with your `babel.cfg` mapping file to scan your application's source code and generate a `messages.pot` (Portable Object Template) file. This file contains all marked strings and serves as a template for translations. ```shell $ pybabel extract -F babel.cfg -o messages.pot . ``` -------------------------------- ### Compile Translations for Application Use Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/translating.rst After editing the `.po` translation files, compile them into binary `.mo` files using `pybabel compile`. These compiled files are what your application will use at runtime to display translated strings. ```shell $ pybabel compile -d translations ``` -------------------------------- ### Pushing Quart Test Request Context Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/formatting_numbers.rst Demonstrates how to push a test request context onto the application stack, which is useful for interacting with Quart's features from a console or script. ```python >>> app.test_request_context().push() ``` -------------------------------- ### Initialize Quart-Babel with Quart App Source: https://github.com/quart-addons/quart-babel/blob/master/README.md This snippet demonstrates how to import the Babel class from quart_babel and initialize it by passing a Quart application instance, enabling i18n/l10n features for the application. ```Python from quart import Quart from quart_babel import Babel app = Quart(__name__) babel = Babel(app) ``` -------------------------------- ### Quart-Babel Core Classes API Reference Source: https://github.com/quart-addons/quart-babel/blob/master/docs/references/api/core.rst Documentation for the main classes `quart_babel.BabelConfiguration` and `quart_babel.Babel`, outlining their purpose and available members as generated by Sphinx's `autoclass` directive. ```APIDOC quart_babel.BabelConfiguration: Type: Class Description: Manages configuration settings for Babel within a Quart application, typically used for setting up locales, timezones, and default values. Members: (All public methods and attributes of BabelConfiguration are documented here) quart_babel.Babel: Type: Class Description: The main extension class for integrating Babel with Quart, providing internationalization and localization support by handling locale selection and text translation. Members: (All public methods and attributes of Babel are documented here) ``` -------------------------------- ### Quart-Babel API Module Index Source: https://github.com/quart-addons/quart-babel/blob/master/docs/references/api/index.rst Lists the main modules comprising the Quart-Babel API, including core functionalities, domain-specific features, data formatting, lazy string implementation, locale and timezone handling, and general utilities. ```APIDOC API Reference Modules: core.rst domain.rst formats.rst lazystring.rst locale.rst timezone.rst utils.rst ``` -------------------------------- ### Quart-Babel Utility Functions API Source: https://github.com/quart-addons/quart-babel/blob/master/docs/references/api/utils.rst Provides API documentation for core utility functions within the Quart-Babel extension. ```APIDOC Utilities: - quart_babel.get_babel - quart_babel.refresh ``` -------------------------------- ### Quart-Babel Configuration Variables Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/configuration.rst Details the configuration variables available for customizing Quart-Babel's behavior, including default locale, timezone, translation domain, and translation directories. ```APIDOC BABEL_DEFAULT_LOCALE: Type: str Default: 'en_US' Description: The default locale to use if no locale selector is registered. BABEL_DEFAULT_TIMEZONE: Type: str Default: 'UTC' Description: The timezone to use for user facing dates. BABEL_DOMAIN: Type: str | list[str] Default: "messages" Description: The default translation domain(s). BABEL_TRANSLATION_DIRECTORIES: Type: str | list[str] Default: "translations" Description: The directories where translations can be found. ``` -------------------------------- ### Formatting Numbers with Default English Locale Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/formatting_numbers.rst Illustrates the usage of various `quart-babel` functions for number formatting, including `format_number`, `format_decimal`, `format_currency`, `format_percent`, and `format_scientific`, showing their output with the default English locale. ```python >>> from quart_babel import format_number >>> format_number(1099) '1,099' >>> from quart_babel import format_decimal >>> format_decimal(1.2346) u'1.235' >>> from quart_babel import format_currency >>> format_currency(1099.98, 'USD') '$1,099.98' >>> from quart_babel import format_percent >>> format_percent(0.34) '34%' >>> from quart_babel import format_scientific >>> format_scientific(10000) '1E4' ``` -------------------------------- ### Implement Custom Locale and Timezone Selectors Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/configuration.rst Demonstrates how to implement custom locale and timezone selector functions (`get_locale`, `get_timezone`) for Quart-Babel, allowing dynamic determination of user-specific locale and timezone based on user settings or request headers. ```python from quart import Quart, g, request def get_locale(): # if a user is logged in, use the locale from the user settings user = getattr(g, 'user', None) if user is not None: return user.locale # otherwise try to guess the language from the user accept # header the browser transmits. We support de/fr/en in this # example. The best match wins. return request.accept_languages.best_match(['de', 'fr', 'en']) def get_timezone(): user = getattr(g, 'user', None) if user is not None: return user.timezone app = Quart(__name__) babel = Babel(app, locale_selector=get_locale, timezone_selector=get_timezone) ``` -------------------------------- ### API Reference for quart_babel Internationalization Functions Source: https://github.com/quart-addons/quart-babel/blob/master/docs/references/api/domain.rst Documents various `gettext` functions provided by `quart_babel` for handling string translation. This includes functions for singular/plural forms (`ngettext`), context-aware translation (`pgettext`), and lazy evaluation versions for deferred translation. ```APIDOC quart_babel.gettext quart_babel.ngettext quart_babel.npgettext quart_babel.pgettext quart_babel.lazy_gettext quart_babel.lazy_ngettext quart_babel.lazy_pgettext ``` -------------------------------- ### Extract Strings Including lazy_gettext Calls Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/translating.rst If your application uses `lazy_gettext` for deferred string translation, you must explicitly tell `pybabel` to look for these calls during extraction using the `-k` flag. This ensures all relevant strings are included in the `messages.pot` file. ```shell $ pybabel extract -F babel.cfg -k lazy_gettext -o messages.pot . ``` -------------------------------- ### Configure Quart Development Server for Translation Reloading Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/reloading.rst This snippet demonstrates how to configure the Quart development server to automatically reload translations by watching compiled `.mo` files. It shows two methods: directly passing `--extra-files` to `quart run` or setting the `QUART_RUN_EXTRA_FILES` environment variable before running the server. This ensures that changes to translation files are reflected without manual server restarts. ```Shell $ quart run --extra-files app/translations/en_GB/LC_MESSAGES/messages.mo ``` ```Shell export QUART_RUN_EXTRA_FILES=app/translations/en_GB/LC_MESSAGES/messages.mo quart run ``` -------------------------------- ### API Reference for quart_babel.Domain Class Source: https://github.com/quart-addons/quart-babel/blob/master/docs/references/api/domain.rst Documents the `Domain` class within `quart_babel`, which is responsible for managing translation domains. It typically involves specifying a translation directory and a default language for internationalization. ```APIDOC quart_babel.Domain :members: ``` -------------------------------- ### Jinja Date and Time Formatting Filters Source: https://github.com/quart-addons/quart-babel/blob/master/docs/how_to_guides/jinja_filters.rst Lists Jinja template filters provided by quart-babel for formatting datetime, date, time, and timedelta objects, mapping them to their corresponding Python formatting functions. ```Jinja Jinja Date and Time Filters: - |datetimeformat -> format_datetime - |dateformat -> format_date -