Localisation in CoronaSDK

Someone recently asked how to make a localised app, this is an interesting question as there may be two situations that can arise,
1. The app needs to support various languages
2. The app needs to adapt to the locale where it is being run.

In option #1, the app language is changed by the user and can be set to any by choosing, where as in option #2, the language changes automatically to the base language, so a French device will see French text, a German device, German and so on.

The first step in this quest is to create a series of assets/text that represent the multi-lingual text.

so we create a table to hold out text.

local translations = 
{
 ["en"] = "Hello, world!",
 ["fr"] = "Bonjour, le monde!",
 ["pt"] = "Olá mundo!",
 ["zh-Hant"] = "世界你好!",
 ["zh-Hans"] = "世界你好!",
 ["de"] = "Hallo, Welt!",
 ["it"] = "Salve, mondo!",
 ["ja"] = "世界よ、こんにちは!",
 ["es"] = "¡Hola mundo!",
}

and the way to print our localised translation would be using
print ( translations["en"]
 print ( translations["de"]
 print ( translations["es"]

so you get the idea.

automating it, we would use a variable to hold the language as defeault
local defaultLanguage = "en"

 print(translations[defaultLanguage])
Now our task is to get the locale language to have the automatic language change. We can get the language locale only on iOS device for the moment, by using
local language = system.getPreference( "ui", "language")

 print(translations[language])

I hope you get the idea on how to use the localisation and locale information. In fact in the latest daily build available for download from the Corona Site has an example under the SampleCode/GettingStarted/ folder called HelloWorldLocalized.

Hope this was helpful.

Updated code for Gideros Studio


local translations = 
{
 ["en"] = "Hello, world!",
 ["fr"] = "Bonjour, le monde!",
 ["pt"] = "Olá mundo!",
 ["zh-Hant"] = "世界你好!",
 ["zh-Hans"] = "世界你好!",
 ["de"] = "Hallo, Welt!",
 ["it"] = "Salve, mondo!",
 ["ja"] = "世界よ、こんにちは!",
 ["es"] = "¡Hola mundo!",
}

local language = application:getLanguage()

print(translations[language])

With just the one line change local language = application:getLanguage() we can have the entire code working with Gideros Studio. Have fun...

Comments

Popular Posts