Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move message formatter in LanguageEngine, add locale for message formatter #2326

Merged
merged 3 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/UniGetUI.Core.Language.Tests/LanguageEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void LocalFallbackTest()
{
LanguageEngine engine = new();
engine.LoadLanguage("random-nonexistent-language");
Assert.Equal("en", engine.Translate("locale"));
Assert.Equal("en", engine.Locale);
}

[Fact]
Expand Down
39 changes: 22 additions & 17 deletions src/UniGetUI.Core.LanguageEngine/LanguageEngine.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json.Nodes;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json.Nodes;
using Jeffijoe.MessageFormat;
using UniGetUI.Core.Data;
using UniGetUI.Core.Logging;
using UniGetUI.Core.SettingsEngine;
Expand All @@ -10,6 +12,11 @@ public class LanguageEngine
{
private Dictionary<string, string> MainLangDict = new();

[NotNull]
public string? Locale { get; private set; }

private MessageFormatter? Formatter;

public LanguageEngine(string ForceLanguage = "")
{
string LangName = Settings.GetValue("PreferredLanguage");
Expand All @@ -26,23 +33,16 @@ public LanguageEngine(string ForceLanguage = "")
/// <param name="lang">the language code</param>
public void LoadLanguage(string lang)
{
if (LanguageData.LanguageReference.ContainsKey(lang))
{
MainLangDict = LoadLanguageFile(lang);
MainLangDict.TryAdd("locale", lang);
}
else if (LanguageData.LanguageReference.ContainsKey(lang[0..2]))
{
MainLangDict = LoadLanguageFile(lang[0..2]);
MainLangDict.TryAdd("locale", lang[0..2]);
}
else
{
MainLangDict = LoadLanguageFile("en");
MainLangDict.TryAdd("locale", "en");
}
Locale = LanguageData.LanguageReference.ContainsKey(lang)
? lang
: LanguageData.LanguageReference.ContainsKey(lang[0..2])
? lang[0..2]
: "en";
MainLangDict = LoadLanguageFile(lang);
Formatter = new() { Locale = Locale.Replace('_', '-') };

LoadStaticTranslation();
Logger.Info("Loaded language locale: " + MainLangDict["locale"]);
Logger.Info("Loaded language locale: " + Locale);
}

public Dictionary<string, string> LoadLanguageFile(string LangKey, bool ForceBundled = false)
Expand Down Expand Up @@ -144,5 +144,10 @@ public string Translate(string key)
else
return key.Replace("WingetUI", "UniGetUI");
}

public string Translate(string key, Dictionary<string, object?> dict)
{
return Formatter!.FormatMessage(Translate(key), dict);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MessageFormat" Version="7.1.0" />
</ItemGroup>

<ItemGroup>
<None Remove="Assets\Data\LanguagesReference.json" />
<None Remove="Assets\Data\TranslatedPercentages.json" />
Expand Down
14 changes: 6 additions & 8 deletions src/UniGetUI.Core.Tools/Tools.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Jeffijoe.MessageFormat;
using System.Diagnostics;
using System.Diagnostics;
using System.Globalization;
using System.Net;
using System.Security.Cryptography;
Expand All @@ -19,31 +18,30 @@ public static class CoreTools
AutomaticDecompression = DecompressionMethods.All
};

private static LanguageEngine? LanguageEngine;
private static LanguageEngine LanguageEngine = new();

/// <summary>
/// Translate a string to the current language
/// </summary>
/// <param name="text">The string to translate</param>
/// <returns>The translated string if available, the original string otherwise</returns>
public static string Translate(string text) {
if(LanguageEngine == null) LanguageEngine = new LanguageEngine();
return LanguageEngine.Translate(text);
}

public static string Translate(string text, Dictionary<string, object?> dict)
{
return MessageFormatter.Format(Translate(text), dict);
return LanguageEngine.Translate(text, dict);
}

public static string Translate(string text, params object[] values)
{
Dictionary<string, object?> dict = new();
Dictionary<string, object?> dict = [];
foreach ((object item, int index) in values.Select((item, index) => (item, index)))
{
dict.Add(index.ToString(), item);
}
return MessageFormatter.Format(Translate(text), dict);
return Translate(text, dict);
}

public static void ReloadLanguageEngineInstance(string ForceLanguage = "")
Expand Down Expand Up @@ -152,7 +150,7 @@ public static void ReportFatalException(Exception e)
string LangName = "Unknown";
try
{
LangName = Translate("langName");
LangName = LanguageEngine.Locale;
}
catch { }

Expand Down
4 changes: 0 additions & 4 deletions src/UniGetUI.Core.Tools/UniGetUI.Core.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MessageFormat" Version="7.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UniGetUI.Core.Data\UniGetUI.Core.Data.csproj" />
<ProjectReference Include="..\UniGetUI.Core.LanguageEngine\UniGetUI.Core.LanguageEngine.csproj" />
Expand Down