Difference between revisions of "Internationalization/User guide"
m |
|||
Line 19: | Line 19: | ||
===String translation=== | ===String translation=== | ||
− | In naïvely written software, you can | + | In naïvely written software, you can often spot things like |
<code>[eiffel, N] | <code>[eiffel, N] | ||
io.put_string("My hovercraft is full of eels") | io.put_string("My hovercraft is full of eels") |
Revision as of 12:24, 12 October 2006
Contents
User documentation for the i18n library
Overview
The i18n library is intended to enable localisation of Eiffel programs.
localisation is the process of adapting a piece of software to a specific place - the locale, often expressed as a combination of language and country codes.
This normally means not only displaying strings in the appropriate language, but also adapting number formatting, date and time formatting etc. to use local conventions. The i18n library provides formatting facilities for numbers, currency values and dates, and the ability to identify and load translated strings at run-time.
Interface
The library provides most of it's services through one class: LOCALE. This presents all formatting and translation facilities for a given locale. LOCALE objects can't be created directly: one must go though the LOCALE_MANAGER class. A LOCALE_MANAGER finds out what information for which locales is available, and offers a list to chose from. It will then load the information for the chosen locale into a LOCALE object and give it to you.
Here is how you can use LOCALE objects for internationalisation:
String translation
In naïvely written software, you can often spot things like
io.put_string("My hovercraft is full of eels")
We'll use this example to illustrate the use of the string translation features of the i18n library.
If, as above, there is just one constant string to translate, the solution is very easy: simply use the translate function. The resulting code would look like this
io.put_string(my_locale.translate("My hovercraft is full of eels"))
If the translate function can't find a translation for this string it will simply return the original string - better then nothing!
But life is, of course, not always that simple. What if we have to deal with plurals? The "traditional" way of doing this is something like:
n := get_number_of_hovercraft if n = 1 then io.put_string("My hovercraft is full of eels") else io.put_string("My hovercraft are full of eels") end
This is not so easy to translate as the above. Why can't we just translate both strings?
Depending on the language, there may be up to 4 different types of plural forms, used in strange and exotic ways. Clearly, it is important to know exactly _how_ many hovercraft there are so that we can choose the right plural form. This can be done by the translate_plural function, whoch we can use in this way:
n := get_number_of_hovercraft io.put_string(my_locale.translate_plural("My hovercraft is full of eels","My hovercraft are full of eels",n))
This function will choose and return a translation in the correct plural form. If it can't find one, it will behave like translate and return either the original singular string or the original plural string, following English grammatical rules.
Often even the above is not enough. What if you want to tell the world exactly how many hovercraft you have? You might write something like this:
n := get_number_of_hovercraft if n = 1 then io.put_string("My hovercraft is full of eels") else io.put_string("My "+n.out+" hovercraft are full of eels") end
How can translate_plural handle this? It needs some reinforcements: the solution is to also use string templates. This means that we can embed codes like "$1" in a string and replace them in the translation by the actual values. Let's see how this works:
n := get_number_of_hovercraft plural_string := my_locale.translate_plural("My hovercraft is full of eels","My $1 hovercraft are full of eels",n) io.put_string(my_locale.format_string(plural_string, [n]))
To replace the escape codes, such as $1, $2, we use the function format_string. This replaces all the escape codes it finds by the values in a tuple that you give to it a an argument.