Get Started with Translations

This guide will walk you through the process of getting started with translations.

Prerequisites
  1. Complete the Prerequisites.

  2. A compiled translation catalog. We are using the Babel/gettext format, which uses .mo files.

    1. If you don't have one, don't worry! We have already prepared one for you.

View full project code on GitHub

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

Quickstart

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.

3KB
Open
<project-name>/
β”œβ”€β”€ locales/
β”‚   β”œβ”€β”€ en_US/LC_MESSAGES/messages.mo   # English (US) locale
β”‚   β”œβ”€β”€ id_ID/LC_MESSAGES/messages.mo   # Indonesian locale
β”‚   β”œβ”€β”€ [other locales...]
└── main.py
2

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?

  1. Need explicit locale control? Use the provider directly:

    provider.get_translation("greeting", locale="en_US")
  2. Building a web app? Set locale per request:

    @app.before_request
    def setup_locale():
        set_locale(request.headers.get("Accept-Language", "en_US"))
  3. Temporary locale switch? Use a context manager:

    with locale_context("id_ID"):
        print(_("greeting"))  # "Halo"

For more information, visit Internationalization.

Last updated