Get Started with Translations
This guide will walk you through the process of getting started with translations.
Installation
# you can use a Conda environment
pip install --extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" python-dotenv gllm-intl# you can use a Conda environment
pip install --extra-index-url "https://oauth2accesstoken:$(gcloud auth print-access-token)@glsdk.gdplabs.id/gen-ai-internal/simple/" python-dotenv gllm-intlFOR /F "tokens=*" %T IN ('gcloud auth print-access-token') DO pip install --extra-index-url "https://oauth2accesstoken:%T@glsdk.gdplabs.id/gen-ai-internal/simple/" python-dotenv gllm-intlQuickstart
1
Prepare your project
Set up your project folder as follows.
You can use your own translation files, or use our pre-prepared translation files below.
<project-name>/
βββ locales/
β βββ en_US/LC_MESSAGES/messages.mo # English (US) locale
β βββ id_ID/LC_MESSAGES/messages.mo # Indonesian locale
β βββ [other locales...]
βββ main.py2
Implementation
In your main.py, configure the provider to point at your locale folder, and set the locale. That's literally it! π
from gllm_intl import _, configure_i18n, set_locale
from gllm_intl.translation.providers import FileSystemLocaleProvider
configure_i18n(FileSystemLocaleProvider(locales_dir="./locales"))
set_locale("id_ID")
print(_("greeting")) # "Halo"What's next?
Need explicit locale control? Use the provider directly:
provider.get_translation("greeting", locale="en_US")Building a web app? Set locale per request:
@app.before_request def setup_locale(): set_locale(request.headers.get("Accept-Language", "en_US"))Temporary locale switch? Use a context manager:
with locale_context("id_ID"): print(_("greeting")) # "Halo"
For more information, visit Internationalization.
Last updated