diff options
| author | Ac_K <Acoustik666@gmail.com> | 2023-01-11 01:29:22 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-11 01:29:22 +0100 |
| commit | 2355c2af62282ff36e56106495c09ed5cfcc3672 (patch) | |
| tree | 34247874f3676ab6567c198e1a427a9bb3ed72d8 /Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs | |
| parent | 5e0f8e873857ce3ca3f532aff0936beb28e412c8 (diff) | |
ava: Generate Locale menu automatically (#4243)
Currently in `MenuMainBarView.axaml` we list all available languages and hardcode the language name with the language key.
It's a bit bad beause if we want to add a new language, we have to edit the `csproj` and the `axaml` with the translated language name and the language code.
I've put all translations in their respective locale files, add code into `MainMenuBarView` constructor to generate the menu automatically. Now we just have to edit the `csproj` if we want to add a new language.
Diffstat (limited to 'Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs')
| -rw-r--r-- | Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs | 49 |
1 files changed, 45 insertions, 4 deletions
diff --git a/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs b/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs index 788f47a1..31dbb1b7 100644 --- a/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs +++ b/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs @@ -1,13 +1,20 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Interactivity; -using Ryujinx.Ava.UI.ViewModels; -using Ryujinx.Ava.UI.Windows; -using System.Threading.Tasks; using LibHac.FsSystem; using LibHac.Ncm; +using Ryujinx.Ava.UI.Helpers; +using Ryujinx.Ava.UI.ViewModels; +using Ryujinx.Ava.UI.Windows; +using Ryujinx.Common; +using Ryujinx.Common.Utilities; using Ryujinx.HLE.HOS; using Ryujinx.Modules; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; namespace Ryujinx.Ava.UI.Views.Main { @@ -19,13 +26,47 @@ namespace Ryujinx.Ava.UI.Views.Main public MainMenuBarView() { InitializeComponent(); + + List<MenuItem> menuItems = new(); + + string localePath = "Ryujinx.Ava/Assets/Locales"; + string localeExt = ".json"; + + string[] localesPath = EmbeddedResources.GetAllAvailableResources(localePath, localeExt); + + Array.Sort(localesPath); + + foreach (string locale in localesPath) + { + string languageCode = Path.GetFileNameWithoutExtension(locale).Split('.').Last(); + string languageJson = EmbeddedResources.ReadAllText($"{localePath}/{languageCode}{localeExt}"); + var strings = JsonHelper.Deserialize<Dictionary<string, string>>(languageJson); + + if (!strings.TryGetValue("Language", out string languageName)) + { + languageName = languageCode; + } + + MenuItem menuItem = new() + { + Header = languageName, + Command = MiniCommand.Create(() => + { + ViewModel.ChangeLanguage(languageCode); + }) + }; + + menuItems.Add(menuItem); + } + + ChangeLanguageMenuItem.Items = menuItems.ToArray(); } protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { base.OnAttachedToVisualTree(e); - if (this.VisualRoot is MainWindow window) + if (VisualRoot is MainWindow window) { Window = window; } |
