AlexDerFerri
Member
HOW TO LOCALIZE YOUR GAME USING JSON
GM Version: Studio 1.4 / 2
Target Platform: All
Download: see code below / itch.io link
Links: https://alexder.itch.io/json-localization-game-maker-studio
Summary: This is a pretty simple way to localize your game.
This method uses the useful gamemaker function called json_decode, which turns a Json file into a ds_map.
Here's how to set up the Json localization system:
1) Create a .txt file using your favorite text editor program and name it whatever you want. Ex: lang.txt.
(You can use notepad if you are on Windows or TextEdit if you are on MacOs).
This is how the text file should look like:
Code:
{
"eng": {
"pause0": "Resume",
"pause1": "Options",
"pause2": "Exit"
},
"ita": {
"pause0": "Riprendi",
"pause1": "Opzioni",
"pause2": "Esci"
},
"fra": {
"pause0": "Reprendre",
"pause1": "Options",
"pause2": "Quitter"
}
}
2) Open your Game Maker project and load the .txt file into the Included Files folder.
3) Create a persistent object that will store the current language.
Then open its Create Event and create a global variable named "LANGUAGE" set equal to whatever language you want the game to be on.
I would recommend putting it in the oGame object if you have one.
Code:
global.LANGUAGE = "eng";
4) Create the script that will load the string from the text file
Code:
/// load_string_json(language, keyword)
var LANGUAGE = argument0;
var KEYWORD = argument1;
// Open the Json file
var JsonFile = file_text_open_read("lang.txt");
var Data = "";
// Read through the Json file and save the text in the Data variable
while (!file_text_eof(JsonFile)) {
Data += file_text_read_string(JsonFile);
file_text_readln(JsonFile);
}
// Close the Json file
file_text_close(JsonFile);
// Store the data in a temporary map
var temp_map = json_decode(Data);
// Find the current game language data
var lang_map = ds_map_find_value(temp_map, LANGUAGE);
// If the keyword you typed in as argument is not a string, convert it to a string, just in case you don't get any errors
if (!is_string(KEYWORD)) KEYWORD = string(KEYWORD);
// Store the output string in a local variable before destroying the data structures
var output_string = lang_map[? KEYWORD];
// Destroy the maps you've created to prevent memory leaks
ds_map_destroy(temp_map);
ds_map_destroy(lang_map);
// Return the output string
return output_string;
Example:
Code:
var CX = room_width/2;
var CY = room_height/2;
draw_text(CX, CY - 32, load_string_json(global.LANGUAGE, "pause0");
draw_text(CX, CY, load_string_json(global.LANGUAGE, "pause1");
draw_text(CX, CY + 32, load_string_json(global.LANGUAGE, "pause2");
Last edited: