Skip to main content

an easy way come true i18n for python3

Project description

easy_i18n

an easy way come true i18n for python3

基于 python3 实现语言本地化

HomePage: https://github.com/ALawating-Rex/easy_i18n

install - 安装

pip install easy_i18n

usage - 使用举例

from easy_i18n.t import Ai18n

a_i18n = Ai18n(locales=["en", "zh"], config={})
t = a_i18n.translate

# after import or define a_i18n and t
# add translation dictionary manually.
a_i18n.add(k="hi", message="hello by added")
# print all the translation dictionary
print(a_i18n.tm)
# simple use
print(t("hi"))
# simple use, translate "hi" to zh
print(t("hi", locale="zh"))
# simple use, translate "hi" to default locale and find it in module user
print(t("hi", module="user"))
# use when string need to format
print(t("user {id} is deleted", locale="zh", module="user").format(id=1))

# 版本>=1.2 增加的逻辑:要翻译的str 指定的语言(locale)不存在则认为指定的是默认语言,在当前语言找不到则原样返回(注:不会再去默认语言查找)  
# 要翻译的str 指定的模块(module)不存在则认为指定的是默认模块,在当前模块找不到,如果当前模块是默认模块则原样返回  
# 如果当前模块不是默认模块,那么会再重新去默认模块查找翻译,再找不到则原样返回 
print(t("hi_only_en", locale="zh"))
print(t("only g has this", locale="zh", module="user"))
print(t("both g and user module has this", locale="zh", module="user"))
print(t("both g and user module has this", locale="zh", module="user1"))
print(t("only en and g has this", locale="zh"))
print(t("only en and g has this"))

Ai18n 说明

Ai18n 类接收两个参数:
1.参数 locales 是list形式,指定了语言都使用哪几种。举例:["en", "zh"] 那么对应的会在config指定的目录下找 en.json 和 zh.json加载过来
默认的 east_i18n 有目录 i18n 可供参考
2.参数 config 是dict形式,你可以指定后覆盖默认的 config值:

:load_path - where to find translate files(default is : i18n)
:default_locale - default locale(default is : en)
:default_module - default module(default is : g)
:default_encoding - default encoding(default is : UTF-8)

config 说明

config使用举例:

config = {
    "load_path": "/locales", # 指定在 /locales 下找对应的翻译 json文件
    "default_module": "global", # 指定默认的全局模块,你可以为比如用户模块,订单模块单独设置翻译,如果不指定 module 则会去全局模块查找。
}
a_i18n = Ai18n(locales=["en", "zh"], config=config)

NOTE 注意

**NOTE:** 
for example your locale is en , define load_path as a absolutely path is recommended,
create an en.json in this path. the content for example is: (default_module should equal g)
    {
      "g": {
        "hi": "hello world",
        "test": "test 1"
      },
      "user": {
        "hi": "user:hello world",
        "user {id} is deleted": "user {id} is deleted"
      }
    }

比如上述例子指定 load_path 为 /locales 且 Ai18n(locales=["en", "zh"], config=config)  
那么要提前在 /locales 下 创建 en.json 和 zh.json  
json内容参考上述例子即可  

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

easy_i18n-1.2.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

easy_i18n-1.2.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file easy_i18n-1.2.0.tar.gz.

File metadata

  • Download URL: easy_i18n-1.2.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/2.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for easy_i18n-1.2.0.tar.gz
Algorithm Hash digest
SHA256 0652017bd7f8c2f8902b334189640c1578a276de69383d7499ed81543e856f76
MD5 108f1ea7778f1a48722b7137e1ea3487
BLAKE2b-256 da84282393da627a1ca4b2be0647d6c9d593ba0afb4c28b1fe09861ed8dfa78d

See more details on using hashes here.

File details

Details for the file easy_i18n-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: easy_i18n-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/2.0.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.5

File hashes

Hashes for easy_i18n-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8aa94662dc0460bcd0a4004057643863a88b5dc89ac191950ac8097bada3e999
MD5 7a647c31e8823bcafeea0053176b9c59
BLAKE2b-256 8308392d25d4b8d78b7a70d11983ce79861b18b6fd997e086b17125bdc927016

See more details on using hashes here.

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