node.js has built-in npx utility to
execute packages "without install":
# run from github masternpx github:littlevgl/lv_i18n -h# run from npm registrynpx lv_i18n -h
Mark up the text in your code
#include "lv_i18n/lv_i18n.h" /*Assuming you have the translations here. (See below)*//* Load translations & default locale (usually, done once) */lv_i18n_init(lv_i18n_language_pack);/* Set active locale (can be switched anytime) */lv_i18n_set_locale("ru-RU");/* The translation of "title1" will be returned according to the selected locale. * ("title1" is only a unique ID of the text.) Example: * en-GB: "Main menu" * ru_RU: "Главное меню" */gui_set_text(label, _("title1"));/* According to `user_cnt` different text can be returned * en-GB `user_cnt == 1` : "One user is logged in" * `user_cnt == 6` : "%d users are logged in" */char buf[64];sprintf(buf, _p("user_logged_in", user_cnt)), user_cnt); /*E.g. buf == "7 users are logged in"*/
_ and _p are normal functions. They just have this short name to enable fast typing of texts.
Rules of getting the translation:
If the translation is not available on the selected locale then the default language will be used instead
If the translation is not available on the default locale the text ID ("title1" in the example) will be returned
Create template yml files for the translations
For each translation, you need to create a yml file with "language code" name. For example:
It will fill the yml files the texts marked with _ and _p.
For example:
en-GB: title1: ~ user_logged_in: one: ~ other: ~
Add the translations into the yml files
The naming conventions in the yml files follow the rules of CLDR so most of the translation offices will know them.
Example:
'en-GB': title1: Main menu user_logged_in: one: One user is logged in other: %d users are logged in
Run compile to convert the yml files to a C and H file
Once you have the translations in the yml files you only need to run the compile to generate a C and H files from the yml files. No other library will be required to get the translation with _() and _p.
Get the plural form of translated text. Use current locale to select plural
algorithm. If not translated, fallback to default locale first, then to input
param.
msg_id - The ID of a text to translate (e.g. "title1")
plural - number of items to decide which plural for to use
return - pointer to the traslation
References:
To understand i18n principles better, you may find useful links below: