Skip to main content

ResourceBundle is a module that manages internationalization of string resources

Project description

ResourceBundle

PyPI-Versions PyPI version License

ResourceBundle is a module that manages internationalization of string resources. It is inspired by javas ResourceBundle and accepts the same format as a java PropertyResourceBundle.


Installation

The ResourceBundle module can be downloaded from PyPI:

$  python -m pip install ResourceBundle

Usage

Each translation file you provide should have key-value pairs inside:

# This is a comment

key=value
another_key=Another value

Save the files of your ResourceBundle in the following structure and file name format:

./
├── BundleName.properties  # Recommended as a fallback
├── BundleName_languageCode_countryCode_variant.properties
└── ...

For example:

./
├── Strings.properties
├── Strings_en.properties
├── Strings_en_US.properties
└── ...

The recommended way to get a ResourceBundle instance is by using ResourceBundle.get_bundle(name, locale). This function also provides support for pythons builtin locale moudule.

import locale
import ResourceBundle

bundle = ResourceBundle.get_bundle("Strings", "en")
bundle = ResourceBundle.get_bundle("Strings", locale.getlocale())

# It is now possible to get a resource with the get() method
bundle.get("key")

If the key could not be found in the ResourceBundle the parent ResourceBundles will be searched until a matching key was found, or it is determined that the key is not present in any parent ResourceBundle.


Accessing the available key-value items in your code:

ResourceBundles can be converted into dict objects with dict(bundle). If you want to include the whole chain to get every accessible key and value, just iterate over the bundles parent.

import ResourceBundle

bundle = ResourceBundle.get_bundle("Strings")

everything = dict(bundle)
while bundle.parent is not None:
    bundle = bundle.parent
    everything.update(dict(bundle))

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

ResourceBundle-2.0.3.tar.gz (5.4 kB view hashes)

Uploaded Source

Built Distribution

ResourceBundle-2.0.3-py3-none-any.whl (5.7 kB view hashes)

Uploaded Python 3

Supported by

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