GML How to localize your game using Json

Discussion in 'Tutorials' started by AlexDerFerri, Nov 14, 2018.

  1. AlexDerFerri

    AlexDerFerri Member

    Joined:
    Aug 24, 2017
    Posts:
    93
    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: Nov 17, 2018
    iKiwed likes this.
  2. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,303
    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.
     
  3. AlexDerFerri

    AlexDerFerri Member

    Joined:
    Aug 24, 2017
    Posts:
    93
    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!
     
  4. RizbIT

    RizbIT Member

    Joined:
    Jun 24, 2016
    Posts:
    405
    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?
     
  5. gnysek

    gnysek Member

    Joined:
    Jun 20, 2016
    Posts:
    1,303
    RizbIT likes this.
  6. RizbIT

    RizbIT Member

    Joined:
    Jun 24, 2016
    Posts:
    405

    "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
     
  7. JasonTomLee

    JasonTomLee Member

    Joined:
    Jul 20, 2016
    Posts:
    109
    Awesome tutorial- Will give it a download on itch!
     
    AlexDerFerri likes this.
  8. SilentxxBunny

    SilentxxBunny Member

    Joined:
    Jun 21, 2016
    Posts:
    32
    Simple, and effective. Perfect example for beginners. Thank you for taking the time to post this, AlexDerFerri!
     
    AlexDerFerri likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice