Skip to content

Commit

Permalink
Support multiple languages
Browse files Browse the repository at this point in the history
  • Loading branch information
cooolbros committed Jan 3, 2024
1 parent bb2901b commit 7ec933a
Showing 1 changed file with 62 additions and 36 deletions.
98 changes: 62 additions & 36 deletions src/HUDMerger/Models/HUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Windows;
using HUDAnimations.Models;
using HUDAnimations.Models.Animations;
using HUDMerger.Exceptions;
Expand Down Expand Up @@ -961,14 +962,29 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
return null;
}

reader.Require([
List<(HUD hud, string relativePath, FileType type)> required = [
(source, "resource\\chat_english.txt", FileType.VDF),
(target, "resource\\chat_english.txt", FileType.VDF)
]);
(target, "resource\\chat_english.txt", FileType.VDF),
];

List<string> languages = ["english"];

string settingsLanguage = ((App)Application.Current).Settings.Value.Language;

if (settingsLanguage != "english")
{
required.AddRange([
(source, $"resource\\chat_{settingsLanguage}.txt", FileType.VDF),
(target, $"resource\\chat_{settingsLanguage}.txt", FileType.VDF),
]);
languages.Add(settingsLanguage);
}

reader.Require(required);

static (KeyValues Root, KeyValues Tokens) LoadLanguageFile(HUDFileReaderService reader, HUD hud)
static (KeyValues Root, KeyValues Tokens) LoadLanguageFile(HUDFileReaderService reader, HUD hud, string language)
{
KeyValues keyValues = reader.ReadKeyValues(hud, "resource\\chat_english.txt");
KeyValues keyValues = reader.ReadKeyValues(hud, $"resource\\chat_{language}.txt");
KeyValues languageFileHeader = keyValues.Header("lang");
KeyValues languageTokens;

Expand Down Expand Up @@ -1000,55 +1016,65 @@ void RemoveBaseSchemeValues(string folderPath, IEnumerable<string> baseFiles)
return (keyValues, languageTokens);
}

KeyValues sourceLanguageTokens = LoadLanguageFile(reader, source).Tokens;
(KeyValues targetKeyValues, KeyValues targetLanguageTokens) = LoadLanguageFile(reader, target);
List<(string Language, KeyValues KeyValues)> languageFiles = [];

foreach (string token in dependencies.LanguageTokens)
foreach (string language in languages)
{
// Remove '#' from token
string key = token[1..];

List<KeyValue> targetTokens = targetLanguageTokens
.Where((token) => token.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
.ToList();
KeyValues sourceLanguageTokens = LoadLanguageFile(reader, source, language).Tokens;
(KeyValues targetKeyValues, KeyValues targetLanguageTokens) = LoadLanguageFile(reader, target, language);

foreach (KeyValue keyValue in sourceLanguageTokens.Where((kv) => kv.Key.Equals(key, StringComparison.OrdinalIgnoreCase)))
foreach (string token in dependencies.LanguageTokens)
{
KeyValue targetToken = targetTokens.FirstOrDefault((kv) => KeyValueComparer.KeyComparer.Equals(kv, keyValue));
// Remove '#' from token
string key = token[1..];

int tokenIndex = targetLanguageTokens.IndexOf(targetToken);
List<KeyValue> targetTokens = targetLanguageTokens
.Where((token) => token.Key.Equals(key, StringComparison.OrdinalIgnoreCase))
.ToList();

if (tokenIndex != -1)
foreach (KeyValue keyValue in sourceLanguageTokens.Where((kv) => kv.Key.Equals(key, StringComparison.OrdinalIgnoreCase)))
{
targetTokens.Remove(targetToken);
int index = tokenIndex;
KeyValue targetToken = targetTokens.FirstOrDefault((kv) => KeyValueComparer.KeyComparer.Equals(kv, keyValue));

int tokenIndex = targetLanguageTokens.IndexOf(targetToken);

targetLanguageTokens[index] = new KeyValue
if (tokenIndex != -1)
{
Key = targetLanguageTokens[index].Key,
Value = keyValue.Value,
Conditional = targetLanguageTokens[index].Conditional,
};
targetTokens.Remove(targetToken);
int index = tokenIndex;

targetLanguageTokens[index] = new KeyValue
{
Key = targetLanguageTokens[index].Key,
Value = keyValue.Value,
Conditional = targetLanguageTokens[index].Conditional,
};
}
else
{
targetLanguageTokens.Add(keyValue);
}
}
else

foreach (KeyValue t in targetTokens)
{
targetLanguageTokens.Add(keyValue);
targetLanguageTokens.Remove(t);
}
}

foreach (KeyValue t in targetTokens)
{
targetLanguageTokens.Remove(t);
}
languageFiles.Add((language, targetKeyValues));
}

return (writer) =>
{
writer.Write(
"resource\\chat_english.txt",
targetKeyValues,
new UnicodeEncoding(false, true) // LE BOM
);
foreach ((string Language, KeyValues KeyValues) in languageFiles)
{
writer.Write(
$"resource\\chat_{Language}.txt",
KeyValues,
new UnicodeEncoding(false, true) // LE BOM
);
}
};
}

Expand Down

0 comments on commit 7ec933a

Please sign in to comment.