Page 1 of 1
Best way to localize text?
Posted: Sun Mar 16, 2014 5:32 pm
by Mangl
I'm wondering what the best way to localize text is in love? Say, for a game with potentially
a few hundred strings that need to be localized.
The obvious way to me seems to be to just load all the strings in a function after reading the user's config or setting it in the menu. e.g:
Code: Select all
if lang == "english" then
langset_english()
elseif lang == "french" then
langset_french()
end
function langset_english()
STR1 = "Hello"
STR2 = "Goodbye"
...
end
function langset_french()
STR1 = "Bonjour"
STR2 = "Au revoir"
...
end
Is this the best way? Or is it worth looking into other things like xml parsing?
Re: Best way to localize text?
Posted: Sun Mar 16, 2014 6:04 pm
by Helvecta
It may be better to make a completely independent file holding all the strings, or at least to put the strings in a local table like this:
Code: Select all
function langset_english()
local set = {
"Hello",
"Goodbye",
...
}
end
(Keeping it this local depends on what exactly you're doing though)
Making an independent file would look like this:
main.lua(?)
Code: Select all
local set
if lang == "english" then
set = require('english')
...
end
english.lua
Code: Select all
local s = {}
s[1] = "hello"
s[2] = "goodbye"
...
return s
You could do a similar thing with love.filesystem.load, if you need to load different languages a lot:
Code: Select all
local set
if lang == "english" then
set = love.filesystem.load('english')()
...
end
These are just a few ways off the top of my head you could go about keeping things tidy and local. Hope this helped!
Re: Best way to localize text?
Posted: Sun Mar 16, 2014 6:26 pm
by Mangl
The languages would indeed be in seperate files. But they wouldn't need to be changed often, only via the options menu. But when changed all the strings need to be overwritten.
I'm not sure if making the string table local would be suitable? (Not sure what advantages / disadvantages come with making it local.) I assume it's better not to in my case. I'd be using them in various other libraries that deal with the game UI like the menu and prompts during game. Using love.graphics.print(str1,x,y).
Cheers.
Re: Best way to localize text?
Posted: Sun Mar 16, 2014 6:37 pm
by SneakySnake
How about something like this?
translationSystem.lua
Code: Select all
local languages = {}
local currentLanguage
do
local files = love.filesystem.getDirectoryItems('lang/')
for _, v in ipairs(files) do
v = string.sub(v, 1, -5)
languages[v] = require('lang/' .. v)
end
end
local function setLanguage(l)
if l then
assert(languages[l], string.format('No such language: "%s"', l))
end
currentLanguage = l
end
local function getLanguages()
local langs = {}
for k, _ in pairs(languages) do
table.insert(langs, k)
end
return langs
end
local function tr(_, str)
return currentLanguage and languages[currentLanguage][str] or str
end
return setmetatable({
setLanguage = setLanguage,
getLanguages = getLanguages
}, {__call = tr})
Here is an example with example language files:
main.lua
Code: Select all
local tr
function love.load()
tr = require('translationSystem')
end
function love.draw()
local y = 0
local function printMsg(lang)
tr.setLanguage(lang)
love.graphics.print(tr('I love apples'), 0, y)
love.graphics.print(tr('I hope you love them too!'), 0, y + 16)
y = y + 48
end
-- Source language
printMsg()
-- All supported languages
for _, lang in ipairs(tr.getLanguages()) do
printMsg(lang)
end
end
langs/german.lua
Code: Select all
return {
['I love apples'] = 'Ich liebe Äpfel',
['I hope you love them too!'] = 'Ich hoffe dass Sie sie auch lieben!'
}
langs/hungarian.lua:
Code: Select all
return {
['I love apples'] = 'Szeretem az almákat',
['I hope you love them too!'] = 'Remélem te is szereted őket!'
}
One problem with this solution though:
Some words/phrases can potentially have different translations in different contexts.
This translation system does not account for that.
If you want, I might cook something up, but I wanted to keep it simple.
Re: Best way to localize text?
Posted: Mon Mar 17, 2014 12:55 am
by kikito
Re: Best way to localize text?
Posted: Mon Mar 17, 2014 1:19 am
by Mangl
Sorry if I wasn't clear enough in my original post
, I'm just talking about handling tables of strings in different languages that are already translated. Just bog standard language settings.
For now I'm sticking with calling a load function for each language. Seems to be the simplest way to go about it.