Skip to content

Instantly share code, notes, and snippets.

@Kobusvdwalt
Created June 28, 2015 17:19
Show Gist options
  • Save Kobusvdwalt/c14796f04d5486ab93e9 to your computer and use it in GitHub Desktop.
Save Kobusvdwalt/c14796f04d5486ab93e9 to your computer and use it in GitHub Desktop.
Localization Made Super Simple
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[ExecuteInEditMode]
[RequireComponent (typeof (Text))]
public class Localization_scr : MonoBehaviour {
// LOCALIZATION SETTINGS
static string selectedLanguageName = "Afrikaans";
static string indexLanguageName = "English";
static string languageFilesDirectory = "Localization Text"; // relative to resources folder
char deliminator = '\n';
public string index;
string indexBuffer = "";
string selectedLanguageNameBuffer = "";
void Update () {
if (index != indexBuffer) // Only update when index changes
{
indexBuffer = index;
GetComponent<Text>().text = getWord(index);
}
if (selectedLanguageName != selectedLanguageNameBuffer)
{
selectedLanguageNameBuffer = selectedLanguageName;
GetComponent<Text>().text = getWord(index);
}
}
public static void ChangeLanguage (string newLanguage)
{
selectedLanguageName = newLanguage;
}
static List<TextAsset> languageFiles = new List<TextAsset>();
static List<List<string>> languageCache = new List<List<string>>();
static List<string> mainIndex = new List<string>();
static long frameBuffer;
static string getWord (string strIndex)
{
Refresh();
int selectedLanguageNum = 0;
for (int i=0; i < languageFiles.Count; i ++)
{
if (languageFiles[i].name == selectedLanguageName)
{
selectedLanguageNum = i;
}
}
string s = "<Fault>";
if (mainIndex.Contains(strIndex))
{
s = languageCache[selectedLanguageNum][mainIndex.IndexOf(strIndex)];
}
return s;
}
static void Refresh () {
if (System.DateTime.Now.Ticks != frameBuffer) // Prevents unnecessary exection;
{
frameBuffer = System.DateTime.Now.Ticks;
// Reset the static variables
languageFiles.Clear();
languageCache.Clear();
mainIndex.Clear();
// Load language files
Object[] objects = Resources.LoadAll(languageFilesDirectory);
languageFiles = new List<TextAsset>();
for (int i=0; i < objects.Length; i ++)
{
if (objects[i] as TextAsset)
{
languageFiles.Add(objects[i] as TextAsset);
}
}
// Push words into static 2d list
for (int i =0; i < languageFiles.Count; i ++)
{
string[] words = languageFiles[i].text.Split('\n');
List<string> tempList = new List<string>();
for (int y=0; y < words.Length; y++)
{
tempList.Add(words[y]);
}
languageCache.Add(tempList);
if (languageFiles[i].name == indexLanguageName)
{
mainIndex = tempList;
}
}
}
}
}
@Kobusvdwalt
Copy link
Author

To use this file in unity simply create a c# file called Localization_scr, paste the code into it and attach the file to a GameObject with a text component.

Next you need to create a directory to keep the language files. This directory needs to be inside of the Resources folder. For example : "Assets/Resources/Localization Text/"

The next step is to fill this directory with the language files. A language file is simply a text file with a bunch of words or phrases separated by some sort of deliminator. The words in the files are relative so they need to be in the same order to match.

A example of two language files would be :
English.txt {Left#Right#Up}
Afrikaans.txt {Links#Regs#Op}

You then need to open the Localization_scr script and edit 3 values to match your project :

selectedLanguageName > This refers to the name of the language file that is active.
indexLanguageName > This refers to the name of the language file that is used as the index of the project.
languageFilesDirectory > This refers to the directory where the language files are located. The language files have to be somewhere inside the Resources folder.
deliminator > You can also change the delimiter if you want to use test that stretches across multiple lines. By default the delimiter is "\n" which is the char for a new line.

You still need to set the local index of each object with the script attached. Using the files example from above you can set the local index to "Left". Lets assume indexLanguageName is "English" When the selectedLanguageName is equal to "English" the text will be set to "Left" and when the selectedLanguageName is equal to "Afrikaans" the text will be set to "Links".

After you have set up the index you are good to go. To switch between languages you simply need to call the public static function ChangeLanguage() like this : Localization_scr.ChangeLanguage("English")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment