Sep

18

Corona SDK: Localization made easy

Andre Biasi

Are you trying to reach international markets? Here's a handy tip to make the whole process easier with Corona SDK.

First we start by creating a file called translations.lua, which we’ll use to store all the localized text strings used in the app. Here’s an example of how it should look like:

-- translations.lua

local translations =
{
	["Hello"] =
	{
		["en"] = "Hello",
		["fr"] = "Salut",
		["de"] = "Hallo",
		["es"] = "Hola",
		["it"] = "Ciao"
	},
	["World"] =
	{
		["en"] = "world",
		["fr"] = "monde",
		["de"] = "Welt",
		["es"] = "mundo",
		["it"] = "mondo"
	},
	["Hello world"] =
	{
		["en"] = "Hello world",
		["fr"] = "Bonjour tout le monde",
		["de"] = "Hallo Welt",
		["es"] = "Hola mundo",
		["it"] = "Ciao a tutti"
	}
}

return translations

As you can see, it’s a LUA table using the brackets syntax with a list of words or sentences that are also tables containing an entry per language supported. Brackets syntax is a must if the words or sentences contain special characters, which is 99.9% of the time when dealing with multiple languages. Let translators work on this file and the app gets localized automatically.

Here’s how to get it working inside your main.lua:

-- main.lua

-- Load translations module locally
local translations = require("translations")

-- Get the user's defined language (if any) or the device's default language
local language =  userDefinedLanguage or system.getPreference("ui", "language")

-- Other localization-related system properties:
-- system.getPreference("locale", "country")
-- system.getPreference("locale", "identifier")
-- system.getPreference("locale", "language")

-- Create our text object
local myText = display.newText(translations["Hello world"][language], 0, 0, native.systemFont, 16)

The function system.getPreference(“ui”, “language”) returns the ISO 639-1 language designation code and the ISO 3166-1 regional designation code if there’s any. For example “en” for English and “en-GB” for English (Great Britain). Make sure you use those codes in the table or it will fail.

When using the Director class, load the module globally in main.lua (without the “local” word) or locally in each Director scene (inside the new() function).

This method is not limited to 1 file or to that particular table structure. Multiple files or a more advanced table structure could be used to store the strings from different areas of the app like the main menu, help screen and so on.

One last thing that’s worth mentioning from the iOS Developer Library is the following key that can be added to the build.settings file:

CFBundleLocalizations (Array – iOS, Mac OS X) identifies the localizations handled manually by your application. If your executable is unbundled or does not use the existing bundle localization mechanism, you can include this key to specify the localizations your application does handle. Each entry in this property’s array is a string identifying the language name or ISO language designator of the supported localization.

Basically, this key will enable the iTunes App Store to advertise the languages supported by your Corona SDK app in the info panel. Here’s an example:

-- build.settings

settings = 
 {
	iphone =
	{
		plist=
		{
			CFBundleLocalizations = 
			{
				"English",
				"French",
				"German",
				"Italian",
				"Spanish"
			},
		},
	},
}