Skip to main content

Internationalization and localization setup and support utilities.

Project description

i18n_core: Internationalization Utilities

i18n_core provides a set of utilities to simplify the process of internationalization (i18n) and localization (l10n) for Python applications. It builds upon the standard locale module and the powerful babel library to offer a streamlined way to manage translations.

Features

  • Global & Module Translations: Easily load translations for your main application and individual component libraries or modules.
  • System Locale Detection: Automatically detects the user's system locale on Windows, macOS, and Linux.
  • Babel Integration: Leverages babel for handling .mo translation files and locale data.
  • WxPython Support: Includes helpers specifically for integrating translations with WxPython applications (i18n_core.gui).
  • Frozen Environment Support: Patches babel to correctly locate locale data when running in frozen environments (e.g., PyInstaller).
  • Context Manager: Provides a reset_locale context manager to temporarily change the locale.

Installation

pip install i18n_core

(Assuming the package is available on PyPI. If it's local, adjust accordingly)

Dependencies

Usage

Initializing Global Translation

Typically, in your application's entry point, you'll install the global translation. This sets up the primary language and translation domain for your app.

import i18n_core
import os

# Assuming your locale files are in a 'locale' subdirectory
locale_dir = os.path.join(os.path.dirname(__file__), 'locale')
app_domain = 'my_app' # Corresponds to my_app.mo files

# Detect system locale or specify one, e.g., 'es_ES'
# Loads translations from locale_dir/<locale_id>/LC_MESSAGES/my_app.mo
current_locale = i18n_core.install_global_translation(
    domain=app_domain,
    locale_path=locale_dir
    # locale_id='fr_FR' # Optionally force a locale
)

print(f"Application locale set to: {current_locale}")

# Now you can use the standard gettext functions globally
print(_("Hello, world!"))

Loading Module Translations

If you have separate libraries or modules with their own translations, you can merge them into the active global translation.

import i18n_core
import my_module
import os

# Assuming my_module has its own 'locale' subdirectory
module_locale_dir = os.path.join(os.path.dirname(my_module.__file__), 'locale')
module_domain = 'my_module' # Corresponds to my_module.mo

i18n_core.install_module_translation(
    domain=module_domain,
    locale_path=module_locale_dir,
    module=my_module # Or provide module name as string 'my_module'
    # Uses the currently active global locale by default
)

# Strings from my_module's domain are now also available via _()
print(_("Module-specific string"))

WxPython Integration

The i18n_core.gui module helps initialize WxPython's internal translation mechanisms.

import wx
import i18n_core
import i18n_core.gui
import os

app = wx.App()

# After installing global translation with i18n_core.install_global_translation...
locale_dir = i18n_core.application_locale_path # Get path used by global install
current_locale = i18n_core.CURRENT_LOCALE

# Initialize wx.Locale
wx_locale = i18n_core.gui.set_wx_locale(locale_dir, current_locale)

if not wx_locale:
    print("Failed to set Wx locale.")

# ... proceed with your WxPython application setup ...

app.MainLoop()

Locale Management

  • get_system_locale(): Detects the OS locale.
  • set_locale(locale_id): Sets the locale for the current process/thread.
  • get_available_locales(domain, locale_path): Lists available babel.Locale objects based on found translation files.
  • reset_locale(): A context manager to temporarily change the locale and restore it afterwards.
from i18n_core import reset_locale, set_locale

print(f"Current locale: {i18n_core.CURRENT_LOCALE}")

with reset_locale():
    set_locale('fr_FR')
    print(f"Locale inside context: {i18n_core.CURRENT_LOCALE}")
    # Perform operations requiring French locale

print(f"Locale after context: {i18n_core.CURRENT_LOCALE}")

Contributing

Please refer to CONTRIBUTING.md for details. (Optional: Create this file if needed)

License

This project is licensed under the terms of the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

i18n_core-1.5.3.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

i18n_core-1.5.3-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file i18n_core-1.5.3.tar.gz.

File metadata

  • Download URL: i18n_core-1.5.3.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.15

File hashes

Hashes for i18n_core-1.5.3.tar.gz
Algorithm Hash digest
SHA256 a8c5fa380f9a0a04e1018ab610c013cab8763713b3c177db96a758041d610f06
MD5 815734c8efa90d0a742099ddf9a1fa16
BLAKE2b-256 49d45f663e0a33450f21aff37193f7c5f67f7117b6a50e96e281a46f87c77172

See more details on using hashes here.

File details

Details for the file i18n_core-1.5.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for i18n_core-1.5.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 593dc41496dc856ee81d91566a3368050ec983efe035feb23cfbfd8da04121ae
MD5 9cc0fbd7dcec8bda385ecdc2fb8bab98
BLAKE2b-256 ec9fb231b58876ba6a471c1d1c291aba2fb8529ea9e1672fc8b64e4f8cee4c96

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page