maix.i18n

maix.i18n module

You can use maix.i18n to access this module with MaixPy
This module is generated from MaixPy and MaixCDK

Module

No module

Enum

Variable

locales

i18n locales list

item description
value {
"en",
"zh",
"zh-tw",
"ja"}
readonly False

C++ defination code:

static std::vector<std::string> locales = {
        "en",
        "zh",
        "zh-tw",
        "ja"}

names

i18n language names list

item description
value {
"English",
"简体中文",
"繁體中文",
"日本語"}
readonly True

C++ defination code:

const static std::vector<std::string> names = {
        "English",
        "简体中文",
        "繁體中文",
        "日本語"}

Function

get_locale

def get_locale() -> str

Get system config of locale.

item description
return language locale, e.g. en, zh, zh_CN, zh_TW, etc.

C++ defination code:

string get_locale()

get_language_name

def get_language_name() -> str

Get system config of language name.

item description
return language name, e.g. English, 简体中文, 繁體中文, etc.

C++ defination code:

string get_language_name()

load_trans_yaml

def load_trans_yaml(locales_dir: str) -> dict[str, dict[str, str]]

Load translations from yaml files.

item description
param locales_dir: translation yaml files directory.
return A dict contains all translations, e.g. {"zh":{"hello": "你好"}, "en":{"hello": "hello"}}, you should delete it after use in C++.

C++ defination code:

const std::map<string, std::map<string, string>> *load_trans_yaml(const std::string &locales_dir)

Class

Trans

Translate helper class.

C++ defination code:

class Trans

__init__

def __init__(self, locales_dict: dict[str, dict[str, str]] = {}) -> None

Translate helper class constructor.\nBy default locale is get by i18n.get_locale() function which set by system settings.\nBut you can also manually set by set_locale function temporarily.

item description
type func
param locales_dict: locales dict, e.g. {"zh": {"Confirm": "确认", "OK": "好的"}, "en": {"Confirm": "Confirm", "OK": "OK"}}
static False

C++ defination code:

Trans(const std::map<string, const std::map<string, string>> &locales_dict = std::map<string, const std::map<string, string>>())

load

def load(self, locales_dir: str) -> maix.err.Err

Load translation from yaml files generated by maixtool i18n command.

item description
type func
param locales_dir: the translation files directory.
return err.Err type, no error will return err.Err.ERR_NONE.
static False

C++ defination code:

err::Err load(const std::string &locales_dir)

update_dict

def update_dict(self, dict: dict[str, dict[str, str]]) -> maix.err.Err

Update translation dict.

item description
type func
param dict: the new translation dict.
return err.Err type, no error will return err.Err.ERR_NONE.
static False

C++ defination code:

err::Err update_dict(const std::map<std::string, const std::map<std::string, std::string>> &dict)

tr

def tr(self, key: str, locale: str = '') -> str

Translate string by key.

item description
type func
param key: string key, e.g. "Confirm"
locale: locale name, if not assign, use default locale set by system settings or set_locale function.
return translated string, if find translation, return it, or return key, e.g. "确认", "Confirm", etc.
static False

C++ defination code:

string tr(const string &key, const string locale = "")

set_locale

def set_locale(self, locale: str) -> None

Set locale temporarily, will not affect system settings.

item description
type func
param locale: locale name, e.g. "zh", "en", etc. @see maix.i18n.locales
static False

C++ defination code:

void set_locale(const string &locale)

get_locale

def get_locale(self) -> str

Get current locale.

item description
type func
return locale name, e.g. "zh", "en", etc. @see maix.i18n.locales
static False

C++ defination code:

string get_locale()