Localization
Creating translations
First, create a folder i18n
in which JSON files will be stored, which will contain your
translations.
As an example, let's create files en.json
and ru.json
, which correspond to English and
Russian languages.
Now, you should fill our files with some content:
{
"game": {
"title": "Cool Game"
},
"button": "Button"
}
{
"game": {
"title": "Крутая игра"
},
"button": "Кнопка"
}
Getting translations
To get the value of a specific key, you can use the i18n.localize(key)
method.
print( i18n.key("game.title") ) -- "Cool Game"
There is also a helper method i18n.gui(node_id, key)
, which replaces the content of the
selected node in the user interface.
With i18n.gui
we can shorten:
gui.set_text(gui.get_node("Title Label"), i18n.key("game.title"))
To:
i18n.gui("title_label", "game.title")
Example of game localization
local i18n = require('ysdk.i18n')
local function on_localize()
i18n.gui("title_label", "game.title")
i18n.gui("button_label", "game.title")
end
function init(self)
i18n.on(on_localize)
end
function final(self)
i18n.off(on_localize)
end
Changing the language of translations
The i18n
module initializes the language corresponding to the user's language from the
environment variables.
It is recommended to use this method only if you want to manually change the language at the user's request.
To change the language of the translation, you can use the
i18n.set_language(code: string)
method.
function settings.change_language(code)
i18n.set_language(code)
end