Skip to content

Instantly share code, notes, and snippets.

@mattiemonster
Last active August 30, 2018 17:23
Show Gist options
  • Save mattiemonster/fd8b295e9364d3ceb237b0282d3a1703 to your computer and use it in GitHub Desktop.
Save mattiemonster/fd8b295e9364d3ceb237b0282d3a1703 to your computer and use it in GitHub Desktop.
´╗┐using System;
using System.Collections.Generic;
using UnityEngine;
using System.Text.RegularExpressions;
namespace Vortex.TextLoader
{
[Serializable]
public enum Language
{
fallback,
english,
english_us,
spanish,
french
}
public class TextLoaderManager : MonoBehaviour
{
public Language currentLang;
private List<TranslatableText> text;
Dictionary<string, string> fallbackValues;
Dictionary<string, string> englishValues;
Dictionary<string, string> englishUSValues;
Dictionary<string, string> spanishValues;
Dictionary<string, string> frenchValues;
// Use this for initialization
void Start()
{
text = new List<TranslatableText>();
fallbackValues = new Dictionary<string, string>(); // Initialise dictionaries
englishValues = new Dictionary<string, string>();
englishUSValues = new Dictionary<string, string>();
spanishValues = new Dictionary<string, string>();
frenchValues = new Dictionary<string, string>();
LoadLangFile("fallback", fallbackValues); // Load values from fallback file into fallbackValues
LoadLangFile("english", englishValues); // etc, etc...
LoadLangFile("english_us", englishUSValues);
LoadLangFile("spanish", spanishValues);
LoadLangFile("french", frenchValues);
// Load text objects from scene
int count = 0;
foreach (TranslatableText go in FindObjectsOfType(typeof(TranslatableText)))
{
text.Add(go);
count++;
}
Debug.Log("Found " + count + " text objects in scene");
if (GetDictionary() == null)
Debug.LogError("Dictionary null!");
foreach (TranslatableText textObj in text)
textObj.Load(GetDictionary(), fallbackValues);
}
Dictionary<string, string> GetDictionary()
{
switch (currentLang)
{
case Language.fallback:
return fallbackValues;
case Language.english:
return englishValues;
case Language.english_us:
return englishUSValues;
case Language.french:
return frenchValues;
case Language.spanish:
return spanishValues;
default:
return fallbackValues;
}
}
public void AddText(TranslatableText textObj)
{
text.Add(textObj);
textObj.Load(GetDictionary(), fallbackValues);
}
public void ReloadText()
{
foreach (TranslatableText textObj in text)
textObj.Load(GetDictionary(), fallbackValues);
}
void LoadLangFile(string fileName, Dictionary<string, string> dictionaryToLoadInto)
{
Debug.Log("Loading text values from " + fileName);
TextAsset fallback = Resources.Load(fileName) as TextAsset;
if (!fallback)
{
Debug.LogError("Fallback Language File not loaded!");
return;
}
string[] fallbackLines = Regex.Split(fallback.text, "\n|\r|\r\n");
foreach (string line in fallbackLines)
{
if (string.IsNullOrEmpty(line))
continue;
string[] values = Regex.Split(line, "=");
string key = values[0];
string value = values[1];
dictionaryToLoadInto.Add(key, value);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment