-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLocalization.cs
More file actions
50 lines (46 loc) · 1.69 KB
/
Localization.cs
File metadata and controls
50 lines (46 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using ColossalFramework.Globalization;
using ColossalFramework.Plugins;
using UnityEngine;
namespace CargoInfoMod
{
public class Localization
{
private static readonly Dictionary<string, Locale> localeStore = new Dictionary<string, Locale>();
private static Locale LocaleFromFile(string file)
{
var locale = new Locale();
using (var reader = new StreamReader(File.OpenRead(file)))
{
string line;
while ((line = reader.ReadLine()) != null)
{
var rows = line.Split('\t');
if (rows.Length < 2)
{
Debug.LogErrorFormat("Not enough tabs in locale string from {0}:\n'{1}'", file, line);
continue;
}
locale.AddLocalizedString(new Locale.Key { m_Identifier = rows[0] }, rows[1]);
}
}
return locale;
}
private static string LocalePath(string lang)
{
var modPath = PluginManager.instance.FindPluginInfo(Assembly.GetExecutingAssembly()).modPath;
return Path.Combine(modPath, $"Locales/{lang}.txt");
}
public static string Get(string id)
{
var lang = LocaleManager.instance.language ?? "en";
if (!localeStore.ContainsKey(lang))
{
localeStore.Add(lang, LocaleFromFile(LocalePath(File.Exists(LocalePath(lang))? lang: "en")));
}
return localeStore[lang].Get(new Locale.Key { m_Identifier = id });
}
}
}