lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I also looked at existing ones when I wanted to add i18n support for
ZeroBrane Studio, but decided to go with a very simple mechanism that
still works well for my purposes:

local messages, lang, counter
function TR(msg, count)
  lang = lang or ide.config.language
  messages = messages or ide.config.messages
  counter = counter or (messages[lang] and messages[lang][0])
  local message = messages[lang] and messages[lang][msg]
  return count and counter and message and type(message) == 'table'
    and message[counter(count)] or message or msg
end

The translation file looks like this:

return {
  [0] = function(c) return c == 1 and 1 or 2 end, -- plural
  ["traced %d instruction"] = {"traced %d instruction", "traced %d
instructions"}, -- src\editor\debugger.lua
}

If may accept one or multiple values (to handle pluralization forms).
[0] element described a function that returns an appropriate index
based on the number given. You can then do:

TR("traced %d instruction", debugger.stats.line):format(debugger.stats.line)

and it will be correctly translated using different forms (for
example, in Russian you may have 3 forms instead of 2).

I also have a script that goes through my source code and extracts all
those messages with TR() and then populates the language file with all
the messages to be translated (it will also keep the messages that are
already in the file and will indicate if something got renamed):
https://github.com/pkulchenko/ZeroBraneStudio/blob/master/build/messages.lua.
Unfortunately it relies on wxlua functions to find source files (which
works well for ZBS as it already includes wxlua); this can probably be
redone using lfs (or using command line parameters to pass file
names).

This implementation is supposed to be really lightweight as it doesn't
require anything in addition to the TR function and the script to
generate/update the files. The pluralization rules are stored in the
language file itself and can be tweaked in any way you prefer.

The translated files for those curious are here:
https://github.com/pkulchenko/ZeroBraneStudio/tree/master/cfg/i18n

Paul.

On Thu, Feb 7, 2013 at 5:26 AM, Thijs Schreijer <thijs@thijsschreijer.nl> wrote:
>> Attached are three files (not very elaborate code, just meant as proof of
>> concept): i18n.lua, a "library" that translates messages, and i18n.de,
>> i18n.fr two sample message catalogs for german and french.
>>
>> So far it can translate messages and change the order of parameters.  Is
>> that enough?
>
> Have you checked existing implementations?
> By Kikito; https://github.com/kikito/i18n.lua
> By OlivineLabs; https://github.com/Olivine-Labs/say
>
> What I don't like about Say is that it requires ordered arguments, not named ones. Luassert uses it and it's been a pita. Probably nicest would be if you could also include formatting in the placeholders, but then it would almost become a template engine...
>
> Thijs
>