In order to distribute games to a wider audience, you might need to translate text to different languages. The localization API provides a way to easily translate your text to different languages.
Before we begin, you need to create a new folder under
Translations. Inside of it, you need to create a file called
translation-base.yaml. This file will act like a layout for your other translations. Example:
origin-locale: "en-US" strings: - "The quick brown fox jumps over the lazy dog" - "Hello, World!"
origin-locale field specifies the initial and default locales. These locales are formatted in the standard
lang_COUNTRY code. A full list of the available locales can be seen here
strings field is a sequence of strings in the origin locale. These strings will then be used as identifiers for translating.
To create a translation, create a new file with its name being one of the languages in our list of supported locales e.g.
bg_BG.yaml and fill it with data like this:
strings: - string: "The quick brown fox jumps over the lazy dog" translation: "Бързата кафява лисица прескача над мързеливото куче" - string: "Hello, World!" translation: "Здравей свят!"
Here we have translated our original English text to Bulgarian. The
strings tag is a list of objects, each of these objects is of type
string, which in its parent data contains the original
en_US text, while under the translation child it holds the Bulgarian text.
These files are automatically indexed, so adding a new translation is as easy as adding a new file with a new locale name
Locale interface contains a bunch of functions to interact with the localization system.
getLocaleNamefunction returns a string containing the short or long name of the provided locale. You can enable short text by setting the last parameter to true and vice-versa
getLocaleIDfunction takes a string formatted in the short locale form and returns the given locale
getLocaleStringfunction takes a string and a locale argument. On call, it fetches the translation of the string for the specified locale and returns it if valid
getFallbackLayoutfunctions are self-explanatory