GML How to localize your game using Json

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"
                   }
 
}
BE CAREFUL TO USE THE RIGHT JSON FORMAT OR YOU MIGHT GET ERRORS LATER ON!

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;
5) That's it! Now whenever you will need to load a "localized string", just use the script, passing the global.LANGUAGE variable as argument0 and the keyword of the string as argument1;

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:

gnysek

Member
Loading every step isn't the best way. The better way is to load strings at start, then make:

global.LANGUAGE = lang_map[? "eng"];

and then in draw

draw_text(0,0, global.LANGUAGE[? "pause0"];


or something like this. Loading text file in every step/frame and in every line will kill game framerate.
 
Loading every step isn't the best way. The better way is to load strings at start, then make:

global.LANGUAGE = lang_map[? "eng"];

and then in draw

draw_text(0,0, global.LANGUAGE[? "pause0"];


or something like this. Loading text file in every step/frame and in every line will kill game framerate.
Hi! Thank you for replying. Yes, I know this is not fully optimized, I've just tried it in a small project of mine and did not encounter any framerate issue, so I did not worry about it too much.
A quick fix might be: instead of loading the file each step of the game, you could create a keyword variable in the Create Event and reference it in the Draw Event:
Code:
/// CREATE EVENT
pause0 = load_json_string(global.LANGUAGE, "pause0");

// DRAW EVENT
draw_text(x, y, pause0);

But your method is a little bit more efficient, especially when projects get bigger!
I didn't think about it, thank you for your suggestion!
 

RizbIT

Member
i want to use hindi language in the app, but the app doesnt draw the hindi text to the screen, im assuming the font file does not included hindi characters....

so how do you serve hindi text in the app?

is there like a special font that can accomadate multiple langauges?
 

RizbIT

Member
Please see https://forum.yoyogames.com/index.php?threads/cyrillic-font-doesnt-work.44179/#post-272127 . This is for Russian language, but for Hindi should be same.

"Did you tried to use "add range" so you'll put there all letters from cyrylic alphabeth ? (probably: АаБбВвГгДдЕеЁёЖжЗзИиЙйКкЛлМмНнОоПпРрСсТтУуФфХхЦцЧчШшЩщЪъЫыЬьЭэЮюЯя)"

How does that work, you click add range and enter a number.. or do you have to copy the letters from the font file into the dialog box

sorry ive never messed with fonts
 

Mert

Member
i want to use hindi language in the app, but the app doesnt draw the hindi text to the screen, im assuming the font file does not included hindi characters....

so how do you serve hindi text in the app?

is there like a special font that can accomadate multiple langauges?
I have localised my app to Indian language(Thanks to a friend). I used different font than my regular font to print Indian letters.
So the answer is, Yes, your font does not include indian letters.
 
Hey @AlexDerFerri, thank you for the tutorial. This is a really good starting point for beginners.
One issue may arise for people playing around with your JSON file though: the value in map[? "ita"][? "pause1"] is missing a quotation mark(").
 
Top