public static void LoadTranslation(string filename)
{
Debugger.Assert(
() =>
{
Debugger.WriteLine($"Loading translation: {filename}");
string filePath = Path.Combine(MaidFiddler.DATA_PATH, $@"{TRANSLATIONS_PATH}\{filename}.txt");
string version = string.Empty;
Debugger.WriteLine(LogLevel.Info, $"File path: {filePath}");
if (!File.Exists(filePath))
{
Debugger.WriteLine(LogLevel.Error, "Failed to find such translation file.");
filename = string.Empty;
}
else
{
Debugger.WriteLine(LogLevel.Info, "Loading translation labels.");
Dictionary<string, string> newTranslationDictionary = new Dictionary<string, string>();
using (TextReader reader = File.OpenText(filePath))
{
string line = reader.ReadLine();
if (line != null)
{
Match match = TagPattern.Match(line);
if (match.Success)
{
version = match.Groups["ver"].Value;
Debugger.WriteLine(
LogLevel.Info,
$"Found translation tag! Language: '{match.Groups["lang"]}', Version: '{match.Groups["ver"]}', Author(s): '{match.Groups["auth"]}'");
}
else
Debugger.WriteLine(LogLevel.Warning, "Did not find any translation tags!");
}
while ((line = reader.ReadLine()) != null)
{
line = line.Trim();
if (line == string.Empty || line.StartsWith(";"))
continue;
string[] parts = line.Split(new[] {'\t'}, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2)
continue;
string text = parts[1].Replace(@"\n", "\n").Replace(@"\r", "\r");
if (text.Trim() == string.Empty)
{
Debugger.WriteLine(
LogLevel.Warning,
$"Translation for {parts[0]} is empty! Skipping...");
continue;
}
if (!newTranslationDictionary.ContainsKey(parts[0]))
newTranslationDictionary.Add(parts[0], text);
else
{
Debugger.WriteLine(
LogLevel.Warning,
$"Translation for {parts[0]} already exists! Replacing with a newer version...");
newTranslationDictionary[parts[0]] = text;
}
}
}
translationDictionary.Clear();
translationDictionary = newTranslationDictionary;
}
Debugger.WriteLine(LogLevel.Info, "Texts loaded");
CurrentTranslationFile = filename;
CurrentTranslationVersion = version;
ApplyTranslation();
},
"Failed to load texts");
}