Menu

Nana i18n Overview

This is a new feature, it will be shown up in next release(0.9)

A new i18n method was introduced into class widget.

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();
}

Screens

In this example, there are 2 languages files.

Language 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 is 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

Posted by Jinhao 2014-11-25

Anonymous
Anonymous

Add attachments
Cancel