Difference between revisions of "Internationalization/translation function"

(small but important detail)
m (Additional things)
Line 113: Line 113:
  
 
[...]
 
[...]
io.put_string(["Remove file %a1?%N",
+
io.put_string(i18n_comp_pl(["Remove file %a1?%N",
 
                 "Delete following files? %a2 there are %a3 files%N"],
 
                 "Delete following files? %a2 there are %a3 files%N"],
                 [file_name, list, list_count], n)
+
                 [file_name, list, list_count], n))
 
--A comment of the author
 
--A comment of the author
 
[...]
 
[...]

Revision as of 06:34, 1 June 2006


Introduction

The problem for the translation is that we need a way to translate three different types of string:

  • normal strings
"A normal string"
  • composed string
"A string composed by "+count.out+" strings, like this or "+a_string+"%N"
  • strings with plural form
if n=1 then
     Result := "a string"
 else
    Result := "strings"
 end

Another problem is that not all languages have the same rules for plural forms, see below and here for more details.

Possible solution

I propose a solution, with four functions (I've got little fantasy for the function names):

i18n (a_string : STRING) : STRING
    --Function for the translation of normal strings
 
i18n_pl (strings : TUPLE[STRING]; form : INTEGER) : STRING
    --Function for the translation of normal strings
    -- with plural form
 
i18n_comp (a_string : STRING; args : TUPLE) : STRING
    --Function for the translation of composit strings
 
i18n_comp_pl (strings : TUPLE[STRING]; args : TUPLE; form : INTEGER) : STRING
    --Function for the translation of composit
    -- strings with plural forms

With this solution, the programmer should write composit strings as (or in a similar way) he would do it for the C printf function. The composit string above would be somthing like this:

i18n_comp("A string composed by %a1 strings, like this or %a2%N", [count, a_string])

where %a1 is the place where the first string in the argument tuple (here count) has to go. I've choosen that the items in the argument tuple are of type ANY, to insert them in the string we have to apply the functon out to all items.

An example

A complete example of how it could work.


Piece of code without internationalization:

[...]
if n = 1
io.put_string("Remove file "+file_name.out+"?%N")
else
io.put_string ("Delete following files?"+list.out+" there are "
                +list.count.out+"files%N")
end
[...]



Piece of code with internationalization:

[...]
io.put_string(i18n_comp_pl(["Remove file %a1?%N",
                "Delete following files? %a2 there are %a3 files%N"],
                [file_name, list, list_count], n))
[...]

the content of the PO file would be:

#: A comment
msgid "Remove file %a1?%N"
msgid_plural "Delete following files? %a2 there are %a3 files%N"
msgstr[0] "Rimuovere il file %a1?"
msgstr[1] "Cancellare i seguenti file? %a2 ce ne sono %a3%N"

You might ask yourself, why a function for all the pluralforms?

The number of plural forms differ between languages. This is somewhat surprising for those who only have experiences with Romanic and Germanic languages since here the number is the same (there are two). But other language families have only one form or many forms. For example,

In Polish the translation of file is plik, and the plural forms are:

2,3,4 pliki
5-21 pliko'w
22-24 pliki
25-31 pliko'w
and so on...

for more information about this topic go here

Additional things

A nice thing whe could do, when parsing the source code of a program to internationalize, is to recognize comments about the string. For example:

[...]
io.put_string(i18n_comp_pl(["Remove file %a1?%N",
                "Delete following files? %a2 there are %a3 files%N"],
                [file_name, list, list_count], n))
--A comment of the author
[...]

woud then look like this in the PO file:

#. A comment of the author
#. Function name, class name, file path
#. %a1 = file_name
#. %a2 = list
#. %a3 = list_count
msgid "Remove file %a1?%N"
msgid_plural "Delete following files? %a2 there are %a3 files%N"
msgstr[0] "Rimuovere il file %a1?"
msgstr[1] "Cancellare i seguenti file? %a2 ce ne sono %a3%N"