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
babelfor handling.motranslation files and locale data. - WxPython Support: Includes helpers specifically for integrating translations with WxPython applications (
i18n_core.gui). - Frozen Environment Support: Patches
babelto correctly locate locale data when running in frozen environments (e.g., PyInstaller). - Context Manager: Provides a
reset_localecontext 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
- Babel
- platform_utils (Assuming this is another internal or specific library)
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 availablebabel.Localeobjects 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file i18n_core-1.5.2.tar.gz.
File metadata
- Download URL: i18n_core-1.5.2.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb76de3a61b072d61bd1f37b67cd2743f8e1a41bf6e043ac580d5abec5316def
|
|
| MD5 |
e76a0409764337c381cbcaac223416b2
|
|
| BLAKE2b-256 |
f722b9a98224153aed9fcfab8477fb5b2f25328ad2808625522f4416aea3c40a
|
File details
Details for the file i18n_core-1.5.2-py2.py3-none-any.whl.
File metadata
- Download URL: i18n_core-1.5.2-py2.py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.8.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f3f1d58d2be3d0be696469904dc6177705fde7280efa8d3aed7a1c9d4fcb9d7f
|
|
| MD5 |
bfff5d2d27699ffd744376a6f46e3293
|
|
| BLAKE2b-256 |
dc4d3899310a8cfb5f4096bcb61d122e7c83f69f751e5bd7a2375577581615f8
|