Internalisation made easy (Support different languages)

1 reply [Last post]
OderWat
User offline. Last seen 28 weeks 1 day ago. Offline
Joined: 4 Jun 2010

Well I am about to define the last cornerstones for my app and just decided to go with an english and a german version. I want the app to be able to switch between both... and the code should support even more languages in the future.

For "historical reasons" I was about to implement _"German" for getting "German" or "Deutsch" and this worked surprisingly well!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- main.lua
 
local langtab={}
if true then
        langtab=require "langtab_de".langtab
end
 
local _ = function (v)
        return langtab[v] or v
end
 
print(_"I am from Berlin")
print(_"Apple")
print(_"Bannana")

1
2
3
4
5
6
7
8
-- langtab_de
 
module(...)
 
langtab = {
        ["I am from Berlin"] => "Ich bin ein Berliner",
        ["Apple"] => "Apfel"
}

The above code will print the german variants of the first both strings but shows "Bannana" for the undefined third string.

If you change the if statement to "false" or just don't require a different language table it stays in its native form.

I came to this idea as I accidentally found out that _ is a variable and that you can omit the () for a function :)

Dunno if that is well known... did not look up what others do :)

Replies

myfogview
User offline. Last seen 2 weeks 5 days ago. Offline
Joined: 14 Apr 2010

Interesting concepts. Thanks for sharing your code.

Tom

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.