MaixCDK i18n(Internationalization), multi language support

You can use any i18n library you like, gettext for example, they are supported in Python and C++.

And we provide a simple i18n library, for simple use case.

For MaixPy

See MaixPy i18n documentation.

For MaixCDK:

The same as MaixPy's, there's two ways:

Simple translation dict

Ensure your source file if UTF-8 encoded first.

#include "maix_i18n.hpp"

const std::map<string, string> locale_zh_dict = {
    {"out", "输出"},
    {"hello", "你好"}
};

const std::map<string, string> locale_ja_dict = {
    // {"out", "出力"},
    {"hello", "こんにちは"}
};

const std::map<string, const std::map<string, string>> locales_dict = {
    {"zh", locale_zh_dict},
    {"ja", locale_ja_dict}
};


i18n::Trans trans(locales_dict);

int main()
{
    log::info("system locale: %s\n", i18n::get_locale().c_str());
    log::info("%s: %s\n", trans.tr("out").c_str(), trans.tr("hello").c_str());

    trans.set_locale("zh");
    printf("%s: %s\n", trans.tr("out").c_str(), trans.tr("hello").c_str());

    trans.set_locale("en");
    printf("%s: %s\n", trans.tr("out").c_str(), trans.tr("hello").c_str());

    trans.set_locale("ja");
    printf("%s: %s\n", trans.tr("out").c_str(), trans.tr("hello").c_str());
    return 0;
}

Seperated translation files

The upper demo is more simple for little translation strings, but if you have many strings need to translate, this way is recommended, use this way:

  • We don't need to change source code when we want to change translation, translation strings in seperate yaml files.
  • It's easier to find translation strings, support auto scan strings which need to be translated and auto generate yaml files.
err::Err e = trans.load("locales"); // load from locales directory
err::check_raise(e, "load translation yamls failed");

log::info("system locale: %s\n", i18n::get_locale().c_str());
log::info("%s: %s, %s\n", i18n::get_locale().c_str(), trans.tr("out").c_str(), trans.tr("hello").c_str());

trans.set_locale("zh");
log::info("zh: %s, %s\n", trans.tr("out").c_str(), trans.tr("hello").c_str());

trans.set_locale("en");
log::info("en: %s, %s\n", trans.tr("out").c_str(), trans.tr("hello").c_str());

trans.set_locale("ja");
log::info("ja: %s, %s\n", trans.tr("out").c_str(), trans.tr("hello").c_str());

Full demo see examples/i18n for example.

Then we exexute maixtool i18n -d . r, this will scan all the strings translate by tr() or _() function and generate locales directory and translation files.
Manually translate the yaml files in locales, then run program on device, put locales directory besides program.

Show in LVGL APP

See how to show custom font at https://neucrack.com/p/514 .

Then use this piece of code:


LV_FONT_DECLARE(zh_fonts);

static const std::map<string, void*> fonts = {
    {"zh", (void*)&zh_fonts}
};

const lv_font_t *get_font_by_locale(const string &locale)
{
    const std::map<string, void*>::const_iterator iter = fonts.find(locale);
    if (iter == fonts.end())
    {
        return &zh_fonts;
    }
    return (lv_font_t *)iter->second;
}

Finally you can use i18n font by

std::string locale = i18n::get_locale();

lv_obj_set_style_text_font(lv_scr_act() , get_font_by_locale(locale), LV_PART_MAIN);

lv_obj_t *label = lv_label_create(lv_scr_act());
lv_label_set_text(label, trans.tr("hello").c_str());