Best way to localize text?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

Best way to localize text?

Post 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?
User avatar
Helvecta
Party member
Posts: 167
Joined: Wed Sep 26, 2012 6:35 pm

Re: Best way to localize text?

Post 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!
"Bump." -CMFIend420
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

Re: Best way to localize text?

Post 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.
User avatar
SneakySnake
Citizen
Posts: 94
Joined: Fri May 31, 2013 2:01 pm
Contact:

Re: Best way to localize text?

Post 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.
Attachments
trgame.love
(1.55 KiB) Downloaded 107 times
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Best way to localize text?

Post by kikito »

I did this some time ago:

https://github.com/kikito/i18n.lua
When I write def I mean function.
Mangl
Prole
Posts: 8
Joined: Thu Aug 29, 2013 8:14 pm

Re: Best way to localize text?

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 4 guests