Posted by
| Nick Gammon
Australia (23,062 posts) Bio
Forum Administrator |
Message
| The next interesting problem is messages with imbedded variables. For example, this message:
"The %s contains %i line%s, %i word%s, %i character%s"
The first %s can be either "document" or "selection". The %i items are counts. The other %s items are either the letter "s" for plural, or the empty string, for singular.
A number of problems arise here. For a start, the word order may be different. For example, a translated version might look like this:
There are 4 lines, 5 words, 22 characters in the document.
In this example the "document" word has moved to the back.
Also the pluralization (is that a word?) might be different. The plural of "line" in German is probably not obtained by adding an "s".
To assist in the process of making a correct translation, formatted strings are handled differently, namely by calling a Lua function. Here is how that message might be handled:
formatted = {
-- TextView.cpp:589
["The %s contains %i line%s, %i word%s, %i character%s"] =
function (a, b, c, d, e, f, g)
return ""
end, -- function
-- ... and so on ..
}
Formatted messages are in a separate table. This time the item value is an unnamed function that will be called at runtime.
The function is supplied with the arguments that the original one (in the source) had.
Let us take an example:
The selection contains 1 line, 3 words, 20 characters
There are really 7 variables here, and they are automatically named a to g. Their values in this particular case would be:
- selection
- 1
- (empty)
- 3
- s
- 20
- s
The translator can now feel free to use those arguments as s/he feels fit. For example, the "s" arguments (items 3, 5 and 7) could be ignored.
The word "selection" could be converted into the equivalent.
The pluralization can be handled by examining the actual numbers and generating appropriate code. The converted function might look like this:
-- TextView.cpp:589
["The %s contains %i line%s, %i word%s, %i character%s"] =
function (a, b, c, d, e, f, g)
local line = "line"
if b ~= 1 then
line = "lines"
end -- plural lines
local word = "word"
if d ~= 1 then
word = "words"
end -- plural words
local character = "character"
if f ~= 1 then
character = "characters"
end -- plural characters
return string.format ("There are %i %s, %i %s, %i %s in the %s",
b, line, d, word, f, character, a)
end, -- function
Although this is still English, this illustrates how I have moved the word "document" or "selection" to the end of the message (that is, argument 'a'), and re-evaluted whether to make the word plural by testing the number of each one (arguments 'b', 'd' and 'f').
The nice thing about using Lua, is that things like making numbers plural can be handled by a shared function, which you could put at the start of the translation file, and which can then be used by every function that needs it.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|