A simple I18n solution featuring fallback locale, YAML locale file, synonym locale file, and flexible locale file path.
Tested working with:
- Python: 2.6+, 3.0+
Run
pip install AoikI18n
or
pip install git+https://github.com/AoiKuiyuyou/AoikI18n
Class I18n contains static methods.
Class I18nObj contains instance methods.
from aoiki18n import I18n
from aoiki18n import I18nObj
Example in code.
Force PyYAML to return Unicode values if you prefer so.
I18n.yaml_force_unicode()
Example in code.
Example in code.
Example in code.
AoikI18n does not prescribe any scheme for mapping from locale name to file path.
It's up to the path function to implement the mapping.
This makes locale file path flexible.
Simple things should be simple, complex things should be possible.
i18n_dir = os.path.join(os.path.dirname(__file__), 'i18n')
def path_func(locale):
path = os.path.join(
i18n_dir,
locale + I18n.TT_FILE_EXT_STXT
)
return path
Example in code.
locale is the main locale.
locale2 is the fallback locale.
path_func is defined here.
i18n = I18nObj(
locale='zh',
locale2='en',
path_func=path_func,
file_encoding='utf-8',
load_file=True,
)
Example in code.
tt means text transformation.
It's boring to write i18n.tt('key')
everywhere.
Instead, create a shorthand function.
tt = i18n.tt
Example in code.
Call tt to map a key to a value in the main locale.
If key is not found in the main locale, fallback will be used.
If key is not found in the fallback locale either, return default or raise KeyError.
tt('key')
tt('key', default=None)
Example in code.
That's it.