Localization

Creating Translations

First, create an i18n folder where you will store JSON files containing your translations.

As an example, let's create en.json and ru.json files, which will correspond to the English and Russian languages.

Now, let's fill our files with some content:

{
  "game": {
    "title": "Cool Game"
  },
  "button": "Button"
}
{
  "game": {
    "title": "Крутая игра"
  },
  "button": "Кнопка"
}

Retrieving Translations

To get the value for a specific key, you can use the method i18n.localize(key).

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 UI node.

With i18n.gui(), we can shorten:

gui.set_text(gui.get_node("Title Label"), i18n.key("game.title"))

Before:

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 Translation Language

Note

The i18n module automatically initializes the language to match the user's environment variables.

It is recommended to use this method only if you want to manually change the language according to the user's request.

To change the translation language, you can use the i18n.set_language(code: string) method.

function settings.change_language(code)
    i18n.set_language(code)
end

Repository