Locale Switching
I've set some available locales in the game, but how do I switch between them? Let's see how to do it with different methods!
How It Works
Just call the i18n_set_locale()
function with the locale code you want to switch to. It's that simple!
Functions
i18n_set_locale
Syntax
i18n_set_locale(code, [update_refs], [i18n]);
Description
Change the current locale used by the GM-I18n system.
Parameters
Name | Type | Default | Description |
---|---|---|---|
code | String | The locale code to switch to. | |
update_refs | Boolean | true | Update all references (message and asset) to the new locale. |
i18n | Boolean | I18n | false | The I18n struct reference, or leave it empty to use the global i18n struct. |
Returns
Void
Examples
// assume the system is initialized on global variable
i18n_set_locale("id"); // switch to "id" locale, and update all references
i18n_set_locale("en", false); // switch to "en" locale, but don't update the references
That's it! Really simple, right?
Locale Switcher
You can use the i18n_set_locale()
function to create a simple locale switcher.
Before we're jumping into the examples, let's make the message data first for this example! We will use the en
, id
, ja
, and zh_CN
locales.
{
"locale": {
"name": "Language",
"en": "English",
"id": "Indonesian",
"ja": "Japanese"
},
"switch": "Switch",
"hello": "Hello World!",
"bye": "Bye World!",
"welcome": "Welcome to {0}, {1}!",
"dialog": {
"npc_1" : {
"greeting": "How are you today, {name}?",
"farewell": "See you later, {0}!"
}
}
}
Now let's create some locale switchers!
Cycling Stepper
The most basic switcher is just a simple button that changes the locale when clicked. This method is called Cycling Stepper.
Change the available locale with one way cycle, for example:
en -> id -> ja -> zh_CN -> en
.
objLocaleStepper
This object will be used to show the current locale name and change the locale when clicked.
// assume the system is initialized on global variable,
// the data has been loaded, and you've created the drawing preset
// create the message reference for the locale name
ref_locale = i18n_create_ref_message("ref_locale", "locale.name");
How simple, right? You can use the i18n_get_locale()
function to get the current locale, and then use it to change the locale as you want.
Stepper
This method is similar to the Cycling Stepper, but it can cycle the locale in both ways.
Change the available locale with two way cycle, for example:
en <-> id <-> ja <-> zh_CN
.
objLocaleStepper
This object will be used to show the current locale name and become the locale controller.
// assume the system is initialized on global variable,
// the data has been loaded, and you've created the drawing preset
// create the message reference for the locale name
ref_locale = i18n_create_ref_message("ref_locale", "locale.name");
// create the locale controller
locale_codes = i18n_get_locales_code(); // get all locale codes
locale_index = 0; // current locale index
locale_count = array_length(locale_codes); // total locale count
// create the stepper button
step_next = instance_create_depth(x + (sprite_width / 2) + (sprite_get_width(objLocaleNext) / 2), y, depth, objLocaleNext, {
target : id
});
step_prev = instance_create_depth(x - (sprite_width / 2) - (sprite_get_width(objLocalePrev) / 2), y, depth, objLocalePrev, {
target : id
});
objLocalePrev
This object will be used to change the locale to the previous one.
// change the locale index to the previous one
if (target.locale_index > 0) {
target.locale_index--;
} else {
target.locale_index = target.locale_count - 1;
}
// change the locale
i18n_set_locale(target.locale_codes[target.locale_index]);
objLocaleNext
This object will be used to change the locale to the next one.
// change the locale index to the next one
if (target.locale_index < target.locale_count - 1) {
target.locale_index++;
} else {
target.locale_index = 0;
}
// change the locale
i18n_set_locale(target.locale_codes[target.locale_index]);
A bit complicated, but it's still simple, right?
Option Group
This method is used in GM-I18n demo. It's a bit more complicated than the previous methods, but it's more flexible.
Change the available locale with option group, for example:
en
,id
,ja
,zh_CN
.
objLocaleGroup
This object will be used to show the current locale name and become the locale controller.
// assume the system is initialized on global variable,
// the data has been loaded, and you've created the drawing preset
// create the locale options
locale_codes = i18n_get_locales_code(); // get all locale codes
for (var i = 0; i < array_length(locale_codes); i++) {
// create the locale option in vertical line
instance_create_depth(x, y + (i * (sprite_get_height(objLocaleOption) + 10)), depth, objLocaleOption, {
code : locale_codes[i]
});
}
objLocaleOption
This object will be used to change the locale when clicked.
// assume the system is initialized on global variable,
// the data has been loaded, and you've created the drawing preset
// get a static message for this locale
name = i18n_get_messages("locale.name", , code);
This one is the most flexible one. You can improve this example to fit your needs. For example, you can add a highlight effect when the locale is active, or you can make a real dropdown menu for the locale options.
Custom
The example above are just some basic examples. Of course, you can create your own locale switcher! The possibilities are endless.
Use these functions to help you create your own locale switcher:
i18n_get_locales_code()
to get all available locale codes.i18n_get_locale()
to get the current locale code.i18n_set_locale()
to change the locale.
i18n_get_messages()
function to get a static message for each locale. It's useful if you want to show the locale name in the switcher.