diff --git a/MPF.Frontend.Test/EnumExtensionsTests.cs b/MPF.Frontend.Test/EnumExtensionsTests.cs index 26f13a58..cba925bd 100644 --- a/MPF.Frontend.Test/EnumExtensionsTests.cs +++ b/MPF.Frontend.Test/EnumExtensionsTests.cs @@ -14,6 +14,23 @@ namespace MPF.Frontend.Test { #region Long Name + [Theory] + [InlineData(null, "Unknown")] + [InlineData(InterfaceLanguage.AutoDetect, "Auto Detect")] + [InlineData(InterfaceLanguage.English, "English")] + [InlineData(InterfaceLanguage.Korean, "한국어")] + public void LongName_InterfaceLanguage(InterfaceLanguage? lang, string? expected) + { + string? actual = lang.LongName(); + Assert.Equal(expected, actual); + + if (lang != null) + { + actual = EnumExtensions.GetLongName(lang); + Assert.Equal(expected, actual); + } + } + [Theory] [InlineData(null, "Unknown")] [InlineData(InternalProgram.NONE, "Unknown")] @@ -115,6 +132,17 @@ namespace MPF.Frontend.Test #region Short Name + [Theory] + [InlineData(null, "Unknown")] + [InlineData(InterfaceLanguage.AutoDetect, "_AUTO")] + [InlineData(InterfaceLanguage.English, "eng")] + [InlineData(InterfaceLanguage.Korean, "kor")] + public void ShortName_InterfaceLanguage(InterfaceLanguage? lang, string? expected) + { + string? actual = lang.ShortName(); + Assert.Equal(expected, actual); + } + [Theory] [InlineData(null, "Unknown")] [InlineData(InternalProgram.NONE, "Unknown")] @@ -135,6 +163,19 @@ namespace MPF.Frontend.Test #region From String + [Theory] + [InlineData(null, InterfaceLanguage.AutoDetect)] + [InlineData("", InterfaceLanguage.AutoDetect)] + [InlineData("_AUTO", InterfaceLanguage.AutoDetect)] + [InlineData("_auto", InterfaceLanguage.AutoDetect)] + [InlineData("eng", InterfaceLanguage.English)] + [InlineData("kor", InterfaceLanguage.Korean)] + public void ToInterfaceLanguageTest(string? interfaceLanguage, InterfaceLanguage expected) + { + InterfaceLanguage actual = interfaceLanguage.ToInterfaceLanguage(); + Assert.Equal(expected, actual); + } + [Theory] [InlineData(null, InternalProgram.NONE)] [InlineData("", InternalProgram.NONE)] diff --git a/MPF.Frontend/ComboBoxItems/UILanguageComboBoxItem.cs b/MPF.Frontend/ComboBoxItems/UILanguageComboBoxItem.cs deleted file mode 100644 index 61ea4ff3..00000000 --- a/MPF.Frontend/ComboBoxItems/UILanguageComboBoxItem.cs +++ /dev/null @@ -1,68 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace MPF.Frontend.ComboBoxItems -{ - /// - /// Represents a single item in the Default UI Language combo box - /// - public class UILanguageComboBoxItem : IEquatable, IElement - { - private readonly string UILanguage; - - private static readonly string[] languages = { "ENG", "한국어" }; - - public UILanguageComboBoxItem(string language) => UILanguage = language; - - /// - public string Name - { - get - { - if (Array.Exists(languages, lang => lang == UILanguage)) - return UILanguage; - else - return "Auto Detect"; - } - } - - public override string ToString() => Name; - - /// - /// Generate all elements for the known system combo box - /// - /// - public static List GenerateElements() - { - var langValues = new List() - { - new UILanguageComboBoxItem(""), - }; - - foreach (var lang in languages) - { - langValues.Add(new UILanguageComboBoxItem(lang)); - } - - return langValues; - } - - /// - public override bool Equals(object? obj) - { - return Equals(obj as UILanguageComboBoxItem); - } - - /// - public bool Equals(UILanguageComboBoxItem? other) - { - if (other == null) - return false; - - return UILanguage == other.UILanguage; - } - - /// - public override int GetHashCode() => base.GetHashCode(); - } -} diff --git a/MPF.Frontend/EnumExtensions.cs b/MPF.Frontend/EnumExtensions.cs index 5f99c613..e52b0028 100644 --- a/MPF.Frontend/EnumExtensions.cs +++ b/MPF.Frontend/EnumExtensions.cs @@ -62,6 +62,31 @@ namespace MPF.Frontend } } + /// + /// Get the string representation of the InterfaceLanguage enum values + /// + /// InterfaceLanguage value to convert + /// String representing the value, if possible + public static string LongName(this InterfaceLanguage lang) + => ((InterfaceLanguage?)lang).LongName(); + + /// + /// Get the string representation of the InterfaceLanguage enum values + /// + /// InterfaceLanguage value to convert + /// String representing the value, if possible + public static string LongName(this InterfaceLanguage? lang) + { + return lang switch + { + InterfaceLanguage.AutoDetect => "Auto Detect", + InterfaceLanguage.English => "English", + InterfaceLanguage.Korean => "한국어", + + _ => "Unknown", + }; + } + /// /// Get the string representation of the InternalProgram enum values /// @@ -188,6 +213,23 @@ namespace MPF.Frontend #region Convert to Short Name + /// + /// Get the short string representation of the InterfaceLanguage enum values + /// + /// InterfaceLanguage value to convert + /// String representing the value, if possible + public static string ShortName(this InterfaceLanguage? lang) + { + return lang switch + { + InterfaceLanguage.AutoDetect => "_AUTO", + InterfaceLanguage.English => "eng", + InterfaceLanguage.Korean => "kor", + + _ => "Unknown", + }; + } + /// /// Get the short string representation of the InternalProgram enum values /// @@ -223,6 +265,24 @@ namespace MPF.Frontend #region Convert from String + /// + /// Get the InterfaceLanguage enum value for a given string + /// + /// String value to convert + /// InterfaceLanguage represented by the string, if possible + public static InterfaceLanguage ToInterfaceLanguage(this string? internalLanguage) + { + return (internalLanguage?.ToLowerInvariant()) switch + { + "_auto" + or "auto detect" => InterfaceLanguage.AutoDetect, + "eng" or "english" => InterfaceLanguage.English, + "kor" or "한국어" => InterfaceLanguage.Korean, + + _ => InterfaceLanguage.AutoDetect, + }; + } + /// /// Get the InternalProgram enum value for a given string /// diff --git a/MPF.Frontend/Enumerations.cs b/MPF.Frontend/Enumerations.cs index 05804e92..e87cbec6 100644 --- a/MPF.Frontend/Enumerations.cs +++ b/MPF.Frontend/Enumerations.cs @@ -1,5 +1,19 @@ namespace MPF.Frontend { + /// + /// Interface language + /// + public enum InterfaceLanguage + { + /// + /// Default to auto-detecting language + /// + AutoDetect = 0, + + English, + Korean, + } + /// /// Drive type for dumping /// @@ -40,4 +54,4 @@ namespace MPF.Frontend ERROR, SECRET, } -} \ No newline at end of file +} diff --git a/MPF.Frontend/Options.cs b/MPF.Frontend/Options.cs index 58ef3c1b..c4ae0625 100644 --- a/MPF.Frontend/Options.cs +++ b/MPF.Frontend/Options.cs @@ -144,12 +144,19 @@ namespace MPF.Frontend /// /// Default UI language to launch MPF into - /// null/empty = Detect locale /// - public string? DefaultUILanguage + public InterfaceLanguage? DefaultUILanguage { - get { return GetStringSetting(Settings, "DefaultUILanguage", "Auto Detect"); } - set { Settings["DefaultUILanguage"] = value; } + get + { + var valueString = GetStringSetting(Settings, "DefaultUILanguage", RedumpSystem.IBMPCcompatible.LongName()); + var valueEnum = (valueString ?? string.Empty).ToInterfaceLanguage(); + return valueEnum; + } + set + { + Settings["DefaultUILanguage"] = value.ShortName(); + } } /// diff --git a/MPF.Frontend/ViewModels/OptionsViewModel.cs b/MPF.Frontend/ViewModels/OptionsViewModel.cs index 5f3e0c42..5c963e71 100644 --- a/MPF.Frontend/ViewModels/OptionsViewModel.cs +++ b/MPF.Frontend/ViewModels/OptionsViewModel.cs @@ -54,7 +54,7 @@ namespace MPF.Frontend.ViewModels /// /// List of available UI languages /// - public static List UILanguages => UILanguageComboBoxItem.GenerateElements(); + public static List> UILanguages => PopulateUILanguages(); /// /// List of available log compression methods @@ -110,6 +110,15 @@ namespace MPF.Frontend.ViewModels return internalPrograms.ConvertAll(ip => new Element(ip)); } + /// + /// Get a complete list of supported interface languages + /// + private static List> PopulateUILanguages() + { + var languages = new List { InterfaceLanguage.AutoDetect, InterfaceLanguage.English, InterfaceLanguage.Korean }; + return languages.ConvertAll(ip => new Element(ip)); + } + /// /// Get a complete list of supported log compression methods /// diff --git a/MPF.UI/Windows/MainWindow.xaml.cs b/MPF.UI/Windows/MainWindow.xaml.cs index 19ba28ca..46bfc047 100644 --- a/MPF.UI/Windows/MainWindow.xaml.cs +++ b/MPF.UI/Windows/MainWindow.xaml.cs @@ -97,20 +97,14 @@ namespace MPF.UI.Windows // Set default language var dictionary = new ResourceDictionary(); - switch (MainViewModel.Options.DefaultUILanguage) + dictionary.Source = MainViewModel.Options.DefaultUILanguage switch { - case "ENG": - // Change UI language to English - dictionary.Source = new Uri("../Resources/Strings.xaml", UriKind.Relative); - break; - case "한국어": - // Change UI language to Korean - dictionary.Source = new Uri("../Resources/Strings.ko.xaml", UriKind.Relative); - break; - default: - dictionary.Source = new Uri("../Resources/Strings.xaml", UriKind.Relative); - break; - } + InterfaceLanguage.English => new Uri("../Resources/Strings.xaml", UriKind.Relative), + InterfaceLanguage.Korean => new Uri("../Resources/Strings.ko.xaml", UriKind.Relative), + + // Default to English + _ => new Uri("../Resources/Strings.xaml", UriKind.Relative), + }; Application.Current.Resources.MergedDictionaries.Add(dictionary); } @@ -135,7 +129,7 @@ namespace MPF.UI.Windows DebugViewMenuItem!.Visibility = Visibility.Visible; MainViewModel.Init(LogOutput!.EnqueueLog, DisplayUserMessage, ShowMediaInformationWindow); - + // Pass translation strings to MainViewModel var translationStrings = new Dictionary(); translationStrings["StartDumpingButtonString"] = (string)Application.Current.FindResource("StartDumpingButtonString"); diff --git a/MPF.UI/Windows/OptionsWindow.xaml b/MPF.UI/Windows/OptionsWindow.xaml index f6c3b02a..af8f35cc 100644 --- a/MPF.UI/Windows/OptionsWindow.xaml +++ b/MPF.UI/Windows/OptionsWindow.xaml @@ -95,17 +95,7 @@