How work internationalization I18n?

I’ve been looking for and blowing my head for several days to make the script work with the Option Dropdown to select a language with the tutorial of this video! " video here "

Need to understand this script to translate the game with a txt file

If I make calls to the function

lang = “en”;

in the I18n script it works!

using System;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.UI;
class I18n
{
     public static Dictionary<String, String> Fields { get; private set; }
     public static string lang;
     /// <summary>
     /// Init on first use
     /// </summary>
     static I18n()
     {
         LoadLanguage();
     }
     /// <summary>
     /// Load language files from ressources
     /// </summary>
     private static void LoadLanguage()
     {
         if (Fields == null)
             Fields = new Dictionary<string, string>();
         Fields.Clear();
          lang = Get2LetterISOCodeFromSystemLanguage().ToLower();
        
             lang = "en";       <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        
         var textAsset = Resources.Load(@"I18n/" + lang); //no .txt needed
         string allTexts = "";
         if (textAsset == null)
             textAsset = Resources.Load(@"I18n/en") as TextAsset; //no .txt needed
         if (textAsset == null)
             Debug.LogError("File not found for I18n: Assets/Resources/I18n/" + lang + ".txt");
         allTexts = (textAsset as TextAsset).text;
         string[] lines = allTexts.Split(new string[] { "\r\n", "\n" },
             StringSplitOptions.None);
         string key, value;
         for (int i = 0; i < lines.Length; i++)
         {
             if (lines[i].IndexOf("=") >= 0 && !lines[i].StartsWith("#"))
             {
                 key = lines[i].Substring(0, lines[i].IndexOf("="));
                 value = lines[i].Substring(lines[i].IndexOf("=") + 1,
                         lines[i].Length - lines[i].IndexOf("=") - 1).Replace("\\n", Environment.NewLine);
                 Fields.Add(key, value);
             }
         }
     }
     public static string GetLanguage()
     {
         return Get2LetterISOCodeFromSystemLanguage().ToLower();
     }
     public static string Get2LetterISOCodeFromSystemLanguage()
     {
         SystemLanguage lang = Application.systemLanguage;
         string res = "EN";
         switch (lang)
         {
             case SystemLanguage.Afrikaans: res = "AF"; break;
             case SystemLanguage.Arabic: res = "AR"; break;
             case SystemLanguage.Basque: res = "EU"; break;
             case SystemLanguage.Belarusian: res = "BY"; break;
             case SystemLanguage.Bulgarian: res = "BG"; break;
             case SystemLanguage.Catalan: res = "CA"; break;
             case SystemLanguage.Chinese: res = "ZH"; break;
             case SystemLanguage.ChineseSimplified: res = "ZH"; break;
             case SystemLanguage.ChineseTraditional: res = "ZH"; break;
             case SystemLanguage.Czech: res = "CS"; break;
             case SystemLanguage.Danish: res = "DA"; break;
             case SystemLanguage.Dutch: res = "NL"; break;
             case SystemLanguage.English: res = "EN"; break;
             case SystemLanguage.Estonian: res = "ET"; break;
             case SystemLanguage.Faroese: res = "FO"; break;
             case SystemLanguage.Finnish: res = "FI"; break;
             case SystemLanguage.French: res = "FR"; break;
             case SystemLanguage.German: res = "DE"; break;
             case SystemLanguage.Greek: res = "EL"; break;
             case SystemLanguage.Hebrew: res = "IW"; break;
             case SystemLanguage.Hungarian: res = "HU"; break;
             case SystemLanguage.Icelandic: res = "IS"; break;
             case SystemLanguage.Indonesian: res = "IN"; break;
             case SystemLanguage.Italian: res = "IT"; break;
             case SystemLanguage.Japanese: res = "JA"; break;
             case SystemLanguage.Korean: res = "KO"; break;
             case SystemLanguage.Latvian: res = "LV"; break;
             case SystemLanguage.Lithuanian: res = "LT"; break;
             case SystemLanguage.Norwegian: res = "NO"; break;
             case SystemLanguage.Polish: res = "PL"; break;
             case SystemLanguage.Portuguese: res = "PT"; break;
             case SystemLanguage.Romanian: res = "RO"; break;
             case SystemLanguage.Russian: res = "RU"; break;
             case SystemLanguage.SerboCroatian: res = "SH"; break;
             case SystemLanguage.Slovak: res = "SK"; break;
             case SystemLanguage.Slovenian: res = "SL"; break;
             case SystemLanguage.Spanish: res = "ES"; break;
             case SystemLanguage.Swedish: res = "SV"; break;
             case SystemLanguage.Thai: res = "TH"; break;
             case SystemLanguage.Turkish: res = "TR"; break;
             case SystemLanguage.Ukrainian: res = "UK"; break;
             case SystemLanguage.Unknown: res = "EN"; break;
             case SystemLanguage.Vietnamese: res = "VI"; break;
         }
         //        Debug.Log ("Lang: " + res);
         return res;
     }
}

But if I create another script to make calls for a dropdown where the player selects his language,

it doesn’t work!

using System;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.UI;
public class Options : MonoBehaviour {
     public Dropdown DropLangue;
     public GameObject DropDown;
     // Use this for initialization
     void Awake()
     {
         DropLangue = DropDown.GetComponent<Dropdown>();
     }
     public void Start()
     {
     }
     public void Update()
     {
    
         if (DropLangue.value == 0)
         {
             I18n.lang = "en";
         }
         if (DropLangue.value == 1)
         {
    
         }
     }
}

Why !?

Thank you in advance for helping me and explaining to me!

You code will never load any language other than “en” for several reasons:

  • The language loading only happens at the very start of your application running because LoadLanguage() is being called in the static initializer for I18N and nowhere else.
  • The “lang” static variable is essentially ignored and overwritten in LoadLanguage(), so setting it from outside the class will have no effect.
  • In the LoadLanguage() method, it is getting the current system language by calling Get2LetterISOCodeFromSystemLanguage(), but then you are immediatelly setting lang = “en” on the very next line

Thank you for your help @PraetorBlue
That’s what I thought. I don’t see what this method is for if it is impossible to change the language with a button and while playing the game! Do you have another solution or a well-explained tutorial to create multiple languages?

I managed to find another tutorial that explains much better with always working the translation with a TXT file! If other coveters are interested, tell me here.