Internationalization - qPCR4vir/nana-docs GitHub Wiki
Nana i18n Overview
A new i18n method was introduced into class widget in Nana.release(0.9).
int main()
{
using namespace nana;
form fm(API::make_center(300, 44));
button btn(fm, rectangle{10, 10, 280, 24});
nana::internationalization i18n;
i18n.load("en.txt"); //Load the language file.
auto reset_caption = [&]{
btn.i18n("Greetings", i18n("Chongqing"), i18n("China"));
//is equal to
//btn.caption(i18n("Greetings", i18n("Chongqing"), i18n("China")));
};
reset_caption();
btn.events().click([&]
{
static bool is_en = true;
is_en = !is_en;
i18n.load(is_en ? "en.txt" : "zh.txt"); //Change the language
reset_caption();
fm.caption(is_en ? L"English" : L"Chinese");
});
fm.show();
exec();
}
In this example, there are 2 languages files:
%arg0
and %arg1
in language files refers to arguments. These arguments are specified by the call of i18n()
.
In Chinese, the address starts with the largest geographical unit. By changing the order of %arg0 and %arg1, it is easy to conform to language habits.
nana::i18n_eval
class i18n_eval
have a lazy evaluation for the msgid
. For example:
nana::internationalization i18n;
i18n.load("en.txt"); //English
//It returns the msgstr immediately by the specified msgid "Chongqing"
auto str1 = i18n("Chongqing");
nana::i18n_eval eval("Chongqing");
//It returns the msgstr by the explicit call. The str2 is "Chongqing".
auto str2 = eval();
i18n.load("zh.txt"); //Changes the language
auto str3 = eval(); //str3 now should be "Chongqing" in Chinese.
i18n_eval is useful, it's convenient to write a multi-language program. For example.
int main()
{
using namespace nana;
form fm(API::make_center(300, 44));
button btn(fm, rectangle{10, 10, 280, 24});
nana::internationalization().load("en.txt"); //Load the language file.
btn.i18n(i18n_eval("Greetings", i18n_eval("Chongqing"), i18n_eval("China")));
btn.events().click([&]
{
static bool is_en = true;
is_en = !is_en;
//Change the language
nana::internationalization().load(is_en ? "en.txt" : "zh.txt");
fm.caption(is_en ? L"English" : L"Chinese");
});
fm.show();
exec();
}
When the language file is changed, the button will automatically update the caption.
Any feedback is welcomed. Originally posted by Jinhao at 2014-11-25 :books: