-
Notifications
You must be signed in to change notification settings - Fork 78
/
Translation.cs
109 lines (93 loc) · 3.28 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using GTA;
namespace MapEditor
{
public static class Translation
{
public static List<TranslationRoot> Translations;
public static string CurrentTranslation { get; set; }
private static TranslationRoot _currenTranslationFile;
private static List<StringPair> _stringList;
public static void Load(string folder, string translation)
{
Translations = new List<TranslationRoot>();
XmlSerializer serializer = new XmlSerializer(typeof (TranslationRoot));
foreach (var path in Directory.GetFiles(folder, "*.xml"))
{
try
{
using (var stream = System.IO.File.OpenRead(path))
{
var trans = (TranslationRoot) serializer.Deserialize(stream);
if (trans == null) throw new NullReferenceException();
trans.SetPath(path);
Translations.Add(trans);
}
}
catch (Exception) { }
}
SetLanguage(translation);
}
public static void SetLanguage(string newLanguage)
{
if (newLanguage == "Auto")
{
CurrentTranslation = Game.Language.ToString();
_currenTranslationFile = Translations.FirstOrDefault(t => t.Language == newLanguage);
}
else
{
CurrentTranslation = newLanguage;
_currenTranslationFile = Translations.FirstOrDefault(t => t.Language == newLanguage);
}
}
public static void Save()
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof (TranslationRoot));
foreach (var trans in Translations)
{
using (var stream = File.OpenWrite(trans.GetPath()))
serializer.Serialize(stream, trans);
}
}
public static string Translate(string original)
{
if (_currenTranslationFile == null) return original;
if (_currenTranslationFile.Translations.All(ts => ts.Original != original))
{
_currenTranslationFile.Translations.Add(new StringPair()
{
Original = original,
Translation = original,
});
Save();
return original;
}
return _currenTranslationFile.Translations.First(ts => ts.Original == original).Translation.Replace("~n~", "\n");
}
}
public class TranslationRoot
{
private string _path;
internal void SetPath(string path)
{
_path = path;
}
internal string GetPath()
{
return _path;
}
public string Language { get; set; }
public string Translator { get; set; }
public List<StringPair> Translations { get; set; }
}
public class StringPair
{
public string Original { get; set; }
public string Translation { get; set; }
}
}