I18n
Pike provides some useful tools to ease internationalization (i18n). Unfortunately, they're not terribly well documented. This page attempts to remedy this shortcoming.Strings and Character SetsPike supports wide strings natively, internally in its string datatype, as well as within pike language code. The internal representation of wide strings is a variation of UTF-16. Most all Pike functions that deal with strings are wide-string aware. Regexp.SimpleRegexp is not wide string aware. Regexp.PCRE is wide string aware, if the PCRE library was compiled with UTF-8 support. Note that this implies a conversion from Pike's string representation to UTF-8, so performance may be negatively impacted.String.width(), ~~utf8_to_string()~~, string_to_utf8(), ~~utf16_to_string() and ~~string_to_utf16() are a few of the useful methods available for converting to and from UTF-8 and UTF-16. Additionally, the Local.Charset module provides encoders and decoders for a vast array (nearly 400 at last count) of alternate character set encodings.String TranslationPike provides 2 options for handling i18n: Gettext and a native Pike translation system. Gettext is located within the Locale module and requires that the iconv and gettext libraries be present at compile time. Use of the Gettext module may be desirable for use in command-line and graphical applications where a single locale is used for the duration of program execution. Additionally, because the popular gettext library is used, a number of ancillary tools are available to make the translation easier.Gettext has a number of drawbacks, including the dependence on one or more external libraries. Gettext is also not suitable for use scenarios where the locale to be used may change, such as in web applications where each request may require the use of a different locale. In situations where Gettext is not useable, we can employ the use of the i18n tools available within the Locale module.The i18n support in the Locale module is very flexible; it enables strings to be localized dynamically, making the localization of web applications possible.This module provides the ability to split all of the translated strings into one or more "projects". A project is a set of translated strings provided together in a set of files, one for each language. A simple application may use one project, while a more complex or modular application may use many projects. Normally, the minimum project size is one pike class file, though it is possible to have more than one project per class.Pike provides some simple tools for assisting in string extraction, such as "pike -x extract_locale".The heart of the whole approach is the use of Locale.translate(). This function is called each time a localized string is to be used, as in the following example: return Locale.translate("MyProject", "en", 21, "Some string"); return Locale.translate("MyProject", id->get_lang(), 21, "Some string"); #define LOCALE(X, Y) Locale.translate("MyProject", id->get_lang(), X, Y) return LOCALE(21, "Some string"); // <locale-token project="my_project">LOCALE</locale-token> // <locale-token project="my_project">LOCALE</locale-token> #define LOCALE(X, Y) Locale.translate("MyProject", id->get_lang(), X, Y)int main() { werror(LOCALE(0, "Hello, World!"); werror(LOCALE(0, "I'm sorry, Dave, I'm afraid I can't do that."); return 0; } pike -x extract_locale test.pike Locale.register_project("MyProject", "/path/to/translations/MyProject/"); Locale.set_default_project_path("/path/to/translations/%P/"); sprintf("My name is %s and I enjoy %s wine.", "John Doe", "red"); Tools.String.named_sprintf("My name is %{name} and I enjoy %{color} wine.", (["name": "John Doe", "color": "red"]) ); #define LOCALE(X,Y,Z) Tools.String.named_sprintf(Locale.translate("MyProject", id->get_lang(), X, Y), Z) return LOCALE(21, "%{name} prefers %{color} wine.", (["name": "John Doe", "color": "red"])); Powered by PikeWiki2 |
|||
gotpike.org | Copyright © 2004 - 2009 | Pike is a trademark of Department of Computer and Information Science, Linköping University |