Skip to content

Node++ multi language support

Jurek Muszyński edited this page Dec 19, 2021 · 2 revisions

Overview

Node++ provides a multi-language support for application's error messages and strings.

Whenever possible, MSG() and STR() macros try to retrieve message/string version that matches user session language. In case when no session is required, preferred user agent'a language of the current request is used.

Language and country codes' comparisons are always case-insensitive.

The first attempt tries to match the language with country code (i.e. "fr-ch"). If exact match can't be found, the next attempt uses only the language code (i.e. "fr"). If still not found, MSG() will return "en-us" message version; STR() will return the string it had been given.

Error messages

Node++ engine initializes messages for all error codes in "en-us". To add error description in a new language, in npp_app_init() or npp_svc_init() use npp_add_message():

npp_add_message(ERR_NOT_FOUND, "PL-PL", "Nie znaleziono");

To retrieve the message use MSG():

OUT("<p>%s</p>", MSG(ERR_NOT_FOUND));

Strings

STR() macro provides a convenient way to render pages in client's preferred language with minimal overhead. It also allowes to gradually enhance the list of supported languages without recompiling the code.

To add strings in a new language, strings.LL-CC file needs to be present in $NPP_DIR/bin.

The file needs to contain the pairs, each pair in a separate line, divided by separator. Default separator is | (pipe) and can be changed with NPP_STRINGS_SEP macro. The first string needs to be in NPP_STRINGS_LANG language (default: "en-us"), the second as specified by the file extension.

Here is the sample strings.pl-pl file:

User|Użytkownik
Password|Hasło
Welcome to Node++ Website|Witaj na stronie Node++
Please, log in|Proszę, zaloguj się

And on the login page:

OUT("<h2>%s</h2>", STR("Welcome to Node++ Website"));
OUT("<h3>%s</h3>", STR("Please, log in"));
OUT("<form action=\"do_login\", method=\"post\">");
OUT("<p>%s: <input name=\"user\"></p>", STR("User"));
OUT("<p>%s: <input name=\"password\", type=\"password\"></p>", STR("Password"));
OUT("</form>");

Strings lookup is case-insensitive. There's no character conversion so the file needs to be in the same code page that the rendered page, typically UTF-8.

Notes

Limits

Maximum message and string length is 256 bytes, including terminating 0. NPP_MAX_MESSAGES and NPP_MAX_STRINGS define maximum capacity for all languages. Default value for both is 1000.

Performance

Both messages and strings are internally indexed by language and country code, so in the typical website with a couple of hundreds strings, performance overhead is negligible.

Clone this wiki locally