/
Translation.cs
92 lines (82 loc) · 3.21 KB
/
Translation.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Linq;
using UnityEngine;
using UnityInjector;
using UnityInjector.Attributes;
namespace CM3D2.SceneCapture.Plugin
{
public static class Translation
{
private readonly static string TRANSLATION_FILE = ConstantValues.ConfigDir + @"\translations.xml";
private readonly static string TRANSLATION_FILE_SYBARIS = ConstantValues.ConfigDirSybaris + @"\translations.xml";
private static Dictionary<string, Dictionary<string, string>> translations;
private static string _currentTranslation;
public static string CurrentTranslation
{
get {
return _currentTranslation;
}
set {
_currentTranslation = value;
}
}
public static void Initialize( string language )
{
CurrentTranslation = language;
translations = new Dictionary<string, Dictionary<string, string>>();
XDocument xml = XDocument.Load(TRANSLATION_FILE);
if( xml == null )
{
xml = XDocument.Load(TRANSLATION_FILE_SYBARIS);
if( xml == null )
{
Debug.LogError("Failed to load translation file from SceneCapture Config directory. 翻訳ファイルの読み込みに失敗しました。 " + TRANSLATION_FILE);
return;
}
}
foreach( XElement translation in xml.Element("Translations").Elements())
{
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach( XElement category in translation.Elements())
{
foreach( XElement field in category.Elements())
{
string key = category.Name + "_" + field.Name;
dict.Add(key, field.Value.ToString());
}
}
string translationLanguage = translation.Attribute("language").Value.ToString();
translations[ translationLanguage ] = dict;
IEnumerator<string> enumerator = translations.Keys.GetEnumerator();
enumerator.MoveNext();
}
}
public static string[] GetTranslations()
{
return translations.Keys.ToArray();
}
public static bool HasTranslation( string translation )
{
return translations.ContainsKey(translation);
}
public static string GetText(string category, string field)
{
if(!translations.ContainsKey(CurrentTranslation))
{
Debug.LogError("Language " + CurrentTranslation + " not defined!");
return "ERROR";
}
string key = category + "_" + field;
if( !translations[ CurrentTranslation ].ContainsKey(key) )
{
Debug.LogError("Translation " + key + " not found for language " + CurrentTranslation + "!");
return "ERROR " + key;
}
return translations[ CurrentTranslation ][ key ];
}
}
}