Skip to content

Commit

Permalink
Merge pull request #2045 from flatron4eg/icu-i18n
Browse files Browse the repository at this point in the history
ICU message format for translations
  • Loading branch information
marticliment authored May 29, 2024
2 parents 5dd85b5 + e3046f8 commit 3744dee
Show file tree
Hide file tree
Showing 26 changed files with 176 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"pt_PT": "Portuguese (Portugal)",
"ro": "Romanian - Română",
"ru": "Russian - Русский",
"sk": "Slovak - Slovenčina",
"sr": "Serbian - Srpski",
"sq": "Albanian - Shqip",
"si": "Sinhala - සිංහල",
Expand Down
13 changes: 4 additions & 9 deletions src/UniGetUI.Core.LanguageEngine/LanguageEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public Dictionary<string, string> LoadLanguageFile(string LangKey, bool ForceBun
/// <param name="LangKey">The Id of the language to download</param>
/// <param name="UseOldUrl">Use the new or the old Url (should not be used manually)</param>
/// <returns></returns>
public async Task DownloadUpdatedLanguageFile(string LangKey, bool UseOldUrl = false)
public async Task DownloadUpdatedLanguageFile(string LangKey)
{
try
{
Uri NewFile = new("https://raw.githubusercontent.com/marticliment/WingetUI/main/src/" + (UseOldUrl ? "wingetui" : "UniGetUI") + "/Assets/Languages/lang_" + LangKey + ".json");
Uri NewFile = new("https://raw.githubusercontent.com/marticliment/WingetUI/main/src/UniGetUI.Core.LanguageEngine/Assets/Languages/lang_" + LangKey + ".json");

HttpClient client = new();
string fileContents = await client.GetStringAsync(NewFile);
Expand All @@ -106,13 +106,8 @@ public async Task DownloadUpdatedLanguageFile(string LangKey, bool UseOldUrl = f
}
catch (Exception e)
{
if (e is HttpRequestException && !UseOldUrl)
await DownloadUpdatedLanguageFile(LangKey, true);
else
{
Logger.Warn("Could not download updated translations from GitHub");
Logger.Warn(e);
}
Logger.Warn("Could not download updated translations from GitHub");
Logger.Warn(e);
}
}

Expand Down
19 changes: 17 additions & 2 deletions src/UniGetUI.Core.Tools/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using UniGetUI.Core.Data;
using UniGetUI.Core.Language;
using UniGetUI.Core.Logging;
using Jeffijoe.MessageFormat;

namespace UniGetUI.Core.Tools
{
Expand All @@ -24,12 +25,26 @@ public static class CoreTools
/// </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)
{
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);
}

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

public static void ReloadLanguageEngineInstance(string ForceLanguage = "")
{
LanguageEngine = new LanguageEngine(ForceLanguage);
Expand Down
4 changes: 4 additions & 0 deletions src/UniGetUI.Core.Tools/UniGetUI.Core.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
<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
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected sealed override async Task<Package[]> FindPackages_UnSafe(string query
if (AlreadyProcessedPackages.ContainsKey(id) && AlreadyProcessedPackages[id].version_float >= float_version)
continue;

AlreadyProcessedPackages[id] = new SearchResult() { id = id, version = version, version_float = float_version };
AlreadyProcessedPackages[id] = new SearchResult { id = id, version = version, version_float = float_version };
}
foreach(var package in AlreadyProcessedPackages.Values)
Packages.Add(new Package(CoreTools.FormatAsName(package.id), package.id, package.version, source, this));
Expand Down
4 changes: 2 additions & 2 deletions src/UniGetUI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ private async void UpdateUniGetUIIfPossible(int round = 0)
Logger.Info("Latest version : " + LatestVersion.ToString(CultureInfo.InvariantCulture));

banner = MainWindow.UpdatesBanner;
banner.Title = CoreTools.Translate("WingetUI version {0} is being downloaded.").Replace("{0}", LatestVersion.ToString(CultureInfo.InvariantCulture));
banner.Title = CoreTools.Translate("WingetUI version {0} is being downloaded.", LatestVersion.ToString(CultureInfo.InvariantCulture));
banner.Message = CoreTools.Translate("This may take a minute or two");
banner.Severity = InfoBarSeverity.Informational;
banner.IsOpen = true;
Expand All @@ -416,7 +416,7 @@ private async void UpdateUniGetUIIfPossible(int round = 0)
if (Hash == InstallerHash)
{

banner.Title = CoreTools.Translate("WingetUI {0} is ready to be installed.").Replace("{0}", LatestVersion.ToString(CultureInfo.InvariantCulture));
banner.Title = CoreTools.Translate("WingetUI {0} is ready to be installed.", LatestVersion.ToString(CultureInfo.InvariantCulture));
banner.Message = CoreTools.Translate("The update will be installed upon closing WingetUI");
banner.ActionButton = new Button();
banner.ActionButton.Content = CoreTools.Translate("Update now");
Expand Down
2 changes: 1 addition & 1 deletion src/UniGetUI/Interface/BackgroundApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task Start()
NancyHost host;
try
{
host = new NancyHost(new HostConfiguration() { RewriteLocalhost = false, }, new Uri("http://localhost:7058/"));
host = new NancyHost(new HostConfiguration { RewriteLocalhost = false, }, new Uri("http://localhost:7058/"));
host.Start();
}
catch
Expand Down
2 changes: 1 addition & 1 deletion src/UniGetUI/Interface/Dialogs/AboutUniGetUI.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void SelectorBar_SelectionChanged(SelectorBar sender, SelectorBarSelecti

SlideNavigationTransitionEffect slideNavigationTransitionEffect = currentSelectedIndex - previousSelectedIndex > 0 ? SlideNavigationTransitionEffect.FromRight : SlideNavigationTransitionEffect.FromLeft;

ContentFrame.Navigate(pageType, null, new SlideNavigationTransitionInfo() { Effect = slideNavigationTransitionEffect });
ContentFrame.Navigate(pageType, null, new SlideNavigationTransitionInfo { Effect = slideNavigationTransitionEffect });

previousSelectedIndex = currentSelectedIndex;

Expand Down
8 changes: 4 additions & 4 deletions src/UniGetUI/Interface/MainView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ private void MoreNavButton_Click(object sender, NavButton.NavButtonEventArgs e)
button.ToggleButton.IsChecked = false;
MoreNavButton.ToggleButton.IsChecked = true;

(VersionMenuItem as MenuFlyoutItem).Text = CoreTools.Translate("WingetUI Version {0}").Replace("{0}", CoreData.VersionName);
MoreNavButtonMenu.ShowAt(MoreNavButton, new FlyoutShowOptions() { ShowMode = FlyoutShowMode.Standard });
(VersionMenuItem as MenuFlyoutItem).Text = CoreTools.Translate("WingetUI Version {0}", CoreData.VersionName);
MoreNavButtonMenu.ShowAt(MoreNavButton, new FlyoutShowOptions { ShowMode = FlyoutShowMode.Standard });

MoreNavButtonMenu.Closed += (s, e) =>
{
Expand Down Expand Up @@ -246,7 +246,7 @@ public async Task<bool> ShowInstallationSettingsForPackageAndContinue(Package pa
OptionsDialog.SecondaryButtonText = "";
OptionsDialog.PrimaryButtonText = CoreTools.Translate("Save and close");
OptionsDialog.DefaultButton = ContentDialogButton.Secondary;
OptionsDialog.Title = CoreTools.Translate("{0} installation options").Replace("{0}", package.Name);
OptionsDialog.Title = CoreTools.Translate("{0} installation options", package.Name);
OptionsDialog.Content = OptionsPage;
OptionsPage.Close += (s, e) => { OptionsDialog.Hide(); };

Expand All @@ -272,7 +272,7 @@ public async Task<InstallationOptions> UpdateInstallationSettings(Package packag
OptionsDialog.SecondaryButtonText = "";
OptionsDialog.PrimaryButtonText = CoreTools.Translate("Save and close");
OptionsDialog.DefaultButton = ContentDialogButton.Secondary;
OptionsDialog.Title = CoreTools.Translate("{0} installation options").Replace("{0}", package.Name);
OptionsDialog.Title = CoreTools.Translate("{0} installation options", package.Name);
OptionsDialog.Content = OptionsPage;
OptionsPage.Close += (s, e) => { OptionsDialog.Hide(); };

Expand Down
18 changes: 9 additions & 9 deletions src/UniGetUI/Interface/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public MainWindow()
LoadingSthDalog = new ContentDialog();
LoadingSthDalog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
LoadingSthDalog.Title = CoreTools.Translate("Please wait");
LoadingSthDalog.Content = new ProgressBar() { IsIndeterminate = true, Width = 300 };
LoadingSthDalog.Content = new ProgressBar { IsIndeterminate = true, Width = 300 };
}
#pragma warning restore CS8618
public void HandleNotificationActivation(ToastArguments args, ValueSet input)
Expand Down Expand Up @@ -199,20 +199,20 @@ private void LoadTrayMenu()
DiscoverPackages.ExecuteRequested += (s, e) => { NavigationPage.DiscoverNavButton.ForceClick(); Activate(); };
AvailableUpdates.ExecuteRequested += (s, e) => { NavigationPage.UpdatesNavButton.ForceClick(); Activate(); };
InstalledPackages.ExecuteRequested += (s, e) => { NavigationPage.InstalledNavButton.ForceClick(); Activate(); };
AboutUniGetUI.Label = CoreTools.Translate("WingetUI Version {0}").Replace("{0}", CoreData.VersionName);
AboutUniGetUI.Label = CoreTools.Translate("WingetUI Version {0}", CoreData.VersionName);
ShowUniGetUI.ExecuteRequested += (s, e) => { Activate(); };
QuitUniGetUI.ExecuteRequested += (s, e) => { MainApp.Instance.DisposeAndQuit(); };

TrayMenu.Items.Add(new MenuFlyoutItem() { Command = DiscoverPackages });
TrayMenu.Items.Add(new MenuFlyoutItem() { Command = AvailableUpdates });
TrayMenu.Items.Add(new MenuFlyoutItem() { Command = InstalledPackages });
TrayMenu.Items.Add(new MenuFlyoutItem { Command = DiscoverPackages });
TrayMenu.Items.Add(new MenuFlyoutItem { Command = AvailableUpdates });
TrayMenu.Items.Add(new MenuFlyoutItem { Command = InstalledPackages });
TrayMenu.Items.Add(new MenuFlyoutSeparator());
MenuFlyoutItem _about = new() { Command = AboutUniGetUI };
_about.IsEnabled = false;
TrayMenu.Items.Add(_about);
TrayMenu.Items.Add(new MenuFlyoutSeparator());
TrayMenu.Items.Add(new MenuFlyoutItem() { Command = ShowUniGetUI });
TrayMenu.Items.Add(new MenuFlyoutItem() { Command = QuitUniGetUI });
TrayMenu.Items.Add(new MenuFlyoutItem { Command = ShowUniGetUI });
TrayMenu.Items.Add(new MenuFlyoutItem { Command = QuitUniGetUI });


TrayMenu.AreOpenCloseAnimationsEnabled = false;
Expand Down Expand Up @@ -261,7 +261,7 @@ public void UpdateSystemTrayStatus()
if (MainApp.Instance.TooltipStatus.AvailableUpdates == 1)
tooltip = CoreTools.Translate("1 update is available") + " - " + Title;
else
tooltip = CoreTools.Translate("{0} updates are available").Replace("{0}", MainApp.Instance.TooltipStatus.AvailableUpdates.ToString()) + " - " + Title;
tooltip = CoreTools.Translate("{0} updates are available", MainApp.Instance.TooltipStatus.AvailableUpdates) + " - " + Title;
}
if(TrayIcon == null)
{
Expand Down Expand Up @@ -289,7 +289,7 @@ public void UpdateSystemTrayStatus()

string FullIconPath = Path.Join(CoreData.UniGetUIExecutableDirectory, "\\Assets\\Images\\tray" + modifier + ".ico");

TrayIcon.SetValue(TaskbarIcon.IconSourceProperty, new BitmapImage() { UriSource = new Uri(FullIconPath) });
TrayIcon.SetValue(TaskbarIcon.IconSourceProperty, new BitmapImage { UriSource = new Uri(FullIconPath) });
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public sealed partial class AboutUniGetUI : Page
public AboutUniGetUI()
{
InitializeComponent();
VersionText.Text = CoreTools.Translate("You have installed WingetUI Version {0}").Replace("{0}", CoreData.VersionName);
VersionText.Text = CoreTools.Translate("You have installed WingetUI Version {0}", CoreData.VersionName);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public ThirdPartyLicenses()
License = LicenseData.LicenseNames[license],
LicenseURL = LicenseData.LicenseURLs[license],
HomepageUrl = LicenseData.HomepageUrls[license],
HomepageText = CoreTools.Translate("{0} homepage").Replace("{0}", license)
HomepageText = CoreTools.Translate("{0} homepage", license)
});
}

Expand Down
18 changes: 9 additions & 9 deletions src/UniGetUI/Interface/Pages/LogPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void SetText(string body)
{
if (line.Replace("\r", "").Replace("\n", "").Trim() == "")
continue;
paragraph.Inlines.Add(new Run() { Text = line.Replace("\r", "").Replace("\n", "") });
paragraph.Inlines.Add(new Run { Text = line.Replace("\r", "").Replace("\n", "") });
paragraph.Inlines.Add(new LineBreak());
}
LogTextBox.Blocks.Clear();
Expand Down Expand Up @@ -114,22 +114,22 @@ public void LoadLog()
switch (log_entry.Severity)
{
case LogEntry.SeverityLevel.Debug:
color = new SolidColorBrush() { Color = IS_DARK? DARK_GREY: LIGHT_GREY };
color = new SolidColorBrush { Color = IS_DARK? DARK_GREY: LIGHT_GREY };
break;
case LogEntry.SeverityLevel.Info:
color = new SolidColorBrush() { Color = IS_DARK ? DARK_BLUE : LIGHT_BLUE };
color = new SolidColorBrush { Color = IS_DARK ? DARK_BLUE : LIGHT_BLUE };
break;
case LogEntry.SeverityLevel.Success:
color = new SolidColorBrush() { Color = IS_DARK ? DARK_WHITE : LIGHT_WHITE};
color = new SolidColorBrush { Color = IS_DARK ? DARK_WHITE : LIGHT_WHITE};
break;
case LogEntry.SeverityLevel.Warning:
color = new SolidColorBrush() { Color = IS_DARK ? DARK_YELLOW : LIGHT_YELLOW };
color = new SolidColorBrush { Color = IS_DARK ? DARK_YELLOW : LIGHT_YELLOW };
break;
case LogEntry.SeverityLevel.Error:
color = new SolidColorBrush() { Color = IS_DARK ? DARK_RED : LIGHT_RED };
color = new SolidColorBrush { Color = IS_DARK ? DARK_RED : LIGHT_RED };
break;
default:
color = new SolidColorBrush() { Color = IS_DARK ? DARK_GREY : LIGHT_GREY };
color = new SolidColorBrush { Color = IS_DARK ? DARK_GREY : LIGHT_GREY };
break;
}

Expand All @@ -138,12 +138,12 @@ public void LoadLog()
foreach(var line in lines)
if (date_length == -1)
{
p.Inlines.Add(new Run() { Text = $"[{log_entry.Time}] {line}\n", Foreground = color });
p.Inlines.Add(new Run { Text = $"[{log_entry.Time}] {line}\n", Foreground = color });
date_length = $"[{log_entry.Time}] ".Length;
}
else
{
p.Inlines.Add(new Run() { Text = new string(' ', date_length) + line + "\n", Foreground = color });
p.Inlines.Add(new Run { Text = new string(' ', date_length) + line + "\n", Foreground = color });
}
((Run)p.Inlines[^1]).Text = ((Run)p.Inlines[^1]).Text.TrimEnd();
LogTextBox.Blocks.Add(p);
Expand Down
Loading

0 comments on commit 3744dee

Please sign in to comment.