Skip to content

Commit

Permalink
Avalonia - Add source generator for locale items (#3999)
Browse files Browse the repository at this point in the history
* Add source generator for locale keys

* use locale keys in Ui subdir
  • Loading branch information
emmauss authored Jan 3, 2023
1 parent 09c9686 commit 02714a1
Show file tree
Hide file tree
Showing 40 changed files with 337 additions and 271 deletions.
2 changes: 2 additions & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,7 @@
<PackageVersion Include="System.Net.NameResolution" Version="4.3.0" />
<PackageVersion Include="System.Threading.ThreadPool" Version="4.3.0" />
<PackageVersion Include="XamlNameReferenceGenerator" Version="1.5.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3"/>
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
</ItemGroup>
</Project>
10 changes: 5 additions & 5 deletions Ryujinx.Ava/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ private void ShowRestartDialog()
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var result = await ContentDialogHelper.CreateConfirmationDialog(
LocaleManager.Instance["DialogThemeRestartMessage"],
LocaleManager.Instance["DialogThemeRestartSubMessage"],
LocaleManager.Instance["InputDialogYes"],
LocaleManager.Instance["InputDialogNo"],
LocaleManager.Instance["DialogRestartRequiredMessage"]);
LocaleManager.Instance[LocaleKeys.DialogThemeRestartMessage],
LocaleManager.Instance[LocaleKeys.DialogThemeRestartSubMessage],
LocaleManager.Instance[LocaleKeys.InputDialogYes],
LocaleManager.Instance[LocaleKeys.InputDialogNo],
LocaleManager.Instance[LocaleKeys.DialogRestartRequiredMessage]);

if (result == UserResult.Yes)
{
Expand Down
22 changes: 11 additions & 11 deletions Ryujinx.Ava/AppHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -432,10 +432,10 @@ public async Task<bool> LoadGuestApplication()
if (userError == UserError.NoFirmware)
{
UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
LocaleManager.Instance["DialogFirmwareNoFirmwareInstalledMessage"],
string.Format(LocaleManager.Instance["DialogFirmwareInstallEmbeddedMessage"], firmwareVersion.VersionString),
LocaleManager.Instance["InputDialogYes"],
LocaleManager.Instance["InputDialogNo"],
LocaleManager.Instance[LocaleKeys.DialogFirmwareNoFirmwareInstalledMessage],
string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallEmbeddedMessage], firmwareVersion.VersionString),
LocaleManager.Instance[LocaleKeys.InputDialogYes],
LocaleManager.Instance[LocaleKeys.InputDialogNo],
"");

if (result != UserResult.Yes)
Expand Down Expand Up @@ -463,11 +463,11 @@ public async Task<bool> LoadGuestApplication()
_parent.RefreshFirmwareStatus();

await ContentDialogHelper.CreateInfoDialog(
string.Format(LocaleManager.Instance["DialogFirmwareInstalledMessage"], firmwareVersion.VersionString),
string.Format(LocaleManager.Instance["DialogFirmwareInstallEmbeddedSuccessMessage"], firmwareVersion.VersionString),
LocaleManager.Instance["InputDialogOk"],
string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstalledMessage], firmwareVersion.VersionString),
string.Format(LocaleManager.Instance[LocaleKeys.DialogFirmwareInstallEmbeddedSuccessMessage], firmwareVersion.VersionString),
LocaleManager.Instance[LocaleKeys.InputDialogOk],
"",
LocaleManager.Instance["RyujinxInfo"]);
LocaleManager.Instance[LocaleKeys.RyujinxInfo]);
}
}
else
Expand Down Expand Up @@ -869,7 +869,7 @@ private unsafe void RenderLoop()
public void UpdateStatus()
{
// Run a status update only when a frame is to be drawn. This prevents from updating the ui and wasting a render when no frame is queued
string dockedMode = ConfigurationState.Instance.System.EnableDockedMode ? LocaleManager.Instance["Docked"] : LocaleManager.Instance["Handheld"];
string dockedMode = ConfigurationState.Instance.System.EnableDockedMode ? LocaleManager.Instance[LocaleKeys.Docked] : LocaleManager.Instance[LocaleKeys.Handheld];
float scale = GraphicsConfig.ResScale;

if (scale != 1)
Expand All @@ -879,11 +879,11 @@ public void UpdateStatus()

StatusUpdatedEvent?.Invoke(this, new StatusUpdatedEventArgs(
Device.EnableDeviceVsync,
LocaleManager.Instance["VolumeShort"] + $": {(int)(Device.GetVolume() * 100)}%",
LocaleManager.Instance[LocaleKeys.VolumeShort] + $": {(int)(Device.GetVolume() * 100)}%",
Renderer.IsVulkan ? "Vulkan" : "OpenGL",
dockedMode,
ConfigurationState.Instance.Graphics.AspectRatio.Value.ToText(),
LocaleManager.Instance["Game"] + $": {Device.Statistics.GetGameFrameRate():00.00} FPS ({Device.Statistics.GetGameFrameTime():00.00} ms)",
LocaleManager.Instance[LocaleKeys.Game] + $": {Device.Statistics.GetGameFrameRate():00.00} FPS ({Device.Statistics.GetGameFrameTime():00.00} ms)",
$"FIFO: {Device.Statistics.GetFifoPercent():00.00} %",
$"GPU: {_renderer.GetHardwareInfo().GpuVendor}"));
}
Expand Down
22 changes: 11 additions & 11 deletions Ryujinx.Ava/Common/ApplicationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static bool TryFindSaveData(string titleName, ulong titleId,
Dispatcher.UIThread.Post(async () =>
{
await ContentDialogHelper.CreateErrorDialog(
string.Format(LocaleManager.Instance["DialogMessageCreateSaveErrorMessage"], result.ToStringWithName()));
string.Format(LocaleManager.Instance[LocaleKeys.DialogMessageCreateSaveErrorMessage], result.ToStringWithName()));
});

return false;
Expand All @@ -100,7 +100,7 @@ await ContentDialogHelper.CreateErrorDialog(

Dispatcher.UIThread.Post(async () =>
{
await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance["DialogMessageFindSaveErrorMessage"], result.ToStringWithName()));
await ContentDialogHelper.CreateErrorDialog(string.Format(LocaleManager.Instance[LocaleKeys.DialogMessageFindSaveErrorMessage], result.ToStringWithName()));
});

return false;
Expand Down Expand Up @@ -151,7 +151,7 @@ public static void OpenSaveDir(ulong saveDataId)
public static async Task ExtractSection(NcaSectionType ncaSectionType, string titleFilePath,
int programIndex = 0)
{
OpenFolderDialog folderDialog = new() { Title = LocaleManager.Instance["FolderDialogExtractTitle"] };
OpenFolderDialog folderDialog = new() { Title = LocaleManager.Instance[LocaleKeys.FolderDialogExtractTitle] };

string destination = await folderDialog.ShowAsync(_owner);

Expand All @@ -164,11 +164,11 @@ public static async Task ExtractSection(NcaSectionType ncaSectionType, string ti
Dispatcher.UIThread.Post(async () =>
{
UserResult result = await ContentDialogHelper.CreateConfirmationDialog(
string.Format(LocaleManager.Instance["DialogNcaExtractionMessage"], ncaSectionType, Path.GetFileName(titleFilePath)),
string.Format(LocaleManager.Instance[LocaleKeys.DialogNcaExtractionMessage], ncaSectionType, Path.GetFileName(titleFilePath)),
"",
"",
LocaleManager.Instance["InputDialogCancel"],
LocaleManager.Instance["DialogNcaExtractionTitle"]);
LocaleManager.Instance[LocaleKeys.InputDialogCancel],
LocaleManager.Instance[LocaleKeys.DialogNcaExtractionTitle]);

if (result == UserResult.Cancel)
{
Expand Down Expand Up @@ -234,7 +234,7 @@ public static async Task ExtractSection(NcaSectionType ncaSectionType, string ti
"Extraction failure. The main NCA was not present in the selected file");
Dispatcher.UIThread.InvokeAsync(async () =>
{
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogNcaExtractionMainNcaNotFoundErrorMessage"]);
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogNcaExtractionMainNcaNotFoundErrorMessage]);
});
return;
}
Expand Down Expand Up @@ -275,19 +275,19 @@ public static async Task ExtractSection(NcaSectionType ncaSectionType, string ti
$"LibHac returned error code: {resultCode.Value.ErrorCode}");
Dispatcher.UIThread.InvokeAsync(async () =>
{
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance["DialogNcaExtractionCheckLogErrorMessage"]);
await ContentDialogHelper.CreateErrorDialog(LocaleManager.Instance[LocaleKeys.DialogNcaExtractionCheckLogErrorMessage]);
});
}
else if (resultCode.Value.IsSuccess())
{
Dispatcher.UIThread.InvokeAsync(async () =>
{
await ContentDialogHelper.CreateInfoDialog(
LocaleManager.Instance["DialogNcaExtractionSuccessMessage"],
LocaleManager.Instance[LocaleKeys.DialogNcaExtractionSuccessMessage],
"",
LocaleManager.Instance["InputDialogOk"],
LocaleManager.Instance[LocaleKeys.InputDialogOk],
"",
LocaleManager.Instance["DialogNcaExtractionTitle"]);
LocaleManager.Instance[LocaleKeys.DialogNcaExtractionTitle]);
});
}
}
Expand Down
6 changes: 3 additions & 3 deletions Ryujinx.Ava/Common/Locale/LocaleExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ namespace Ryujinx.Ava.Common.Locale
{
internal class LocaleExtension : MarkupExtension
{
public LocaleExtension(string key)
public LocaleExtension(LocaleKeys key)
{
Key = key;
}

public string Key { get; }
public LocaleKeys Key { get; }

public override object ProvideValue(IServiceProvider serviceProvider)
{
string keyToUse = Key;
LocaleKeys keyToUse = Key;

ReflectionBindingExtension binding = new($"[{keyToUse}]")
{
Expand Down
22 changes: 13 additions & 9 deletions Ryujinx.Ava/Common/Locale/LocaleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Ryujinx.Common;
using Ryujinx.Common.Utilities;
using Ryujinx.Ui.Common.Configuration;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
Expand All @@ -13,17 +14,17 @@ class LocaleManager : BaseModel
{
private const string DefaultLanguageCode = "en_US";

private Dictionary<string, string> _localeStrings;
private ConcurrentDictionary<string, object[]> _dynamicValues;
private Dictionary<LocaleKeys, string> _localeStrings;
private ConcurrentDictionary<LocaleKeys, object[]> _dynamicValues;

public static LocaleManager Instance { get; } = new LocaleManager();
public Dictionary<string, string> LocaleStrings { get => _localeStrings; set => _localeStrings = value; }
public Dictionary<LocaleKeys, string> LocaleStrings { get => _localeStrings; set => _localeStrings = value; }


public LocaleManager()
{
_localeStrings = new Dictionary<string, string>();
_dynamicValues = new ConcurrentDictionary<string, object[]>();
_localeStrings = new Dictionary<LocaleKeys, string>();
_dynamicValues = new ConcurrentDictionary<LocaleKeys, object[]>();

Load();
}
Expand All @@ -49,7 +50,7 @@ public void Load()
}
}

public string this[string key]
public string this[LocaleKeys key]
{
get
{
Expand All @@ -63,7 +64,7 @@ public string this[string key]
return value;
}

return key;
return key.ToString();
}
set
{
Expand All @@ -73,7 +74,7 @@ public string this[string key]
}
}

public void UpdateDynamicValue(string key, params object[] values)
public void UpdateDynamicValue(LocaleKeys key, params object[] values)
{
_dynamicValues[key] = values;

Expand All @@ -98,7 +99,10 @@ public void LoadLanguage(string languageCode)

foreach (var item in strings)
{
this[item.Key] = item.Value;
if (Enum.TryParse<LocaleKeys>(item.Key, out var key))
{
this[key] = item.Value;
}
}

if (Program.PreviewerDetached)
Expand Down
2 changes: 1 addition & 1 deletion Ryujinx.Ava/Input/AvaloniaKeyboardDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public IGamepad GetGamepad(string id)
return null;
}

return new AvaloniaKeyboard(this, _keyboardIdentifers[0], LocaleManager.Instance["AllKeyboards"]);
return new AvaloniaKeyboard(this, _keyboardIdentifers[0], LocaleManager.Instance[LocaleKeys.AllKeyboards]);
}

protected virtual void Dispose(bool disposing)
Expand Down
Loading

0 comments on commit 02714a1

Please sign in to comment.