I’m working on my first localization system for the game, but there is a problem I can’t resolve. I’m always getting an “illegal characters in path” exception. Here is my main localization class logic. I’ve commented out the error down the lines in the LoadLocalization method. That’s the only problem left with my system! I’m using simple .txt files sitting inside the StreamingAssets folder for the localization.
Please, help me understand what’s wrong!
Best regards, Vyacheslav.
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class TextLocalizer : MonoBehaviour
{
private Dictionary<string, string> _localization;
private readonly string _missingTextString = “Localized text not found!”;
public static TextLocalizer Instance { get; private set; }
public bool IsReady { get; private set; }
private void Awake()
{
IsReady = false;
if (Instance == null) Instance = this;
else if (Instance != this) Destroy(gameObject);
DontDestroyOnLoad(gameObject);
LoadLocalization("English.txt");
IsReady = true;
}
public void LoadLocalization(string fileName)
{
_localization = new Dictionary<string, string>();
string filePath = Path.Combine(Application.streamingAssetsPath, fileName); // <= here's the error!
if (File.Exists(filePath))
{
StreamReader reader = new StreamReader(filePath);
string line;
while (true)
{
line = reader.ReadLine();
if (line == null) break;
string[] words = line.Split('=');
_localization.Add(words[0], words[1]);
}
reader.Close();
}
}
public string GetLocalizedText(string key)
{
string result = _missingTextString;
if (_localization.TryGetValue(key, out string value)) result = value;
return result;
}
}