From 2355c2af62282ff36e56106495c09ed5cfcc3672 Mon Sep 17 00:00:00 2001 From: Ac_K Date: Wed, 11 Jan 2023 01:29:22 +0100 Subject: 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. --- Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml | 58 +--------------------- Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml.cs | 49 ++++++++++++++++-- 2 files changed, 46 insertions(+), 61 deletions(-) (limited to 'Ryujinx.Ava/UI/Views') diff --git a/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml b/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml index 0d0ae119..bd26561a 100644 --- a/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml +++ b/Ryujinx.Ava/UI/Views/Main/MainMenuBarView.axaml @@ -77,63 +77,7 @@ - - - - - - - - - - - - - - - + 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>(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; } -- cgit v1.2.3