From 36c1339a9359b026fc57b73857567e0a6031c223 Mon Sep 17 00:00:00 2001 From: Alex Z Date: Mon, 20 Jan 2020 06:48:05 +0300 Subject: [PATCH] le hyar' - add change language - refract code --- src/UniversalValveToolbox/Base/DtoModel.cs | 24 ++ .../Model/Dto/AddonDtoModel.cs | 37 +++ .../Model/Dto/EngineDtoModel.cs | 30 +++ .../Model/Dto/SettingsDtoModel.cs | 36 +++ .../Model/Dto/ToolDtoModel.cs | 24 ++ .../Model/Dto/VProjectDtoModel.cs | 30 +++ .../Model/Provider/DataProvider.cs | 21 ++ .../Model/Provider/LanguageProvider.cs | 7 + .../Model/VIewModel/SettingsViewModel.cs | 32 +++ src/UniversalValveToolbox/Program.cs | 7 + .../translations/LangDict.Designer.cs | 81 +++++++ .../Properties/translations/LangDict.resx | 126 ++++++++++ .../translations/LangDict.ru.Designer.cs | 0 .../Properties/translations/LangDict.ru.resx | 126 ++++++++++ src/UniversalValveToolbox/UI/FormMain.cs | 10 +- src/UniversalValveToolbox/UI/FormSettings.cs | 14 +- .../UniversalValveToolbox.csproj | 46 +++- .../Utils/JsonFileUtil.cs | 14 ++ src/UniversalValveToolbox/Utils/JsonReader.cs | 219 ------------------ .../Utils/LanguageManager.cs | 23 ++ src/UniversalValveToolbox/json/settings.json | 10 +- 21 files changed, 674 insertions(+), 243 deletions(-) create mode 100644 src/UniversalValveToolbox/Base/DtoModel.cs create mode 100644 src/UniversalValveToolbox/Model/Dto/AddonDtoModel.cs create mode 100644 src/UniversalValveToolbox/Model/Dto/EngineDtoModel.cs create mode 100644 src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs create mode 100644 src/UniversalValveToolbox/Model/Dto/ToolDtoModel.cs create mode 100644 src/UniversalValveToolbox/Model/Dto/VProjectDtoModel.cs create mode 100644 src/UniversalValveToolbox/Model/Provider/DataProvider.cs create mode 100644 src/UniversalValveToolbox/Model/Provider/LanguageProvider.cs create mode 100644 src/UniversalValveToolbox/Model/VIewModel/SettingsViewModel.cs create mode 100644 src/UniversalValveToolbox/Properties/translations/LangDict.Designer.cs create mode 100644 src/UniversalValveToolbox/Properties/translations/LangDict.resx create mode 100644 src/UniversalValveToolbox/Properties/translations/LangDict.ru.Designer.cs create mode 100644 src/UniversalValveToolbox/Properties/translations/LangDict.ru.resx create mode 100644 src/UniversalValveToolbox/Utils/JsonFileUtil.cs delete mode 100644 src/UniversalValveToolbox/Utils/JsonReader.cs create mode 100644 src/UniversalValveToolbox/Utils/LanguageManager.cs diff --git a/src/UniversalValveToolbox/Base/DtoModel.cs b/src/UniversalValveToolbox/Base/DtoModel.cs new file mode 100644 index 0000000..1ee0993 --- /dev/null +++ b/src/UniversalValveToolbox/Base/DtoModel.cs @@ -0,0 +1,24 @@ +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Runtime.CompilerServices; +using System.Windows.Forms; +using Newtonsoft.Json; + +namespace UniversalValveToolbox.Base { + public abstract class DtoModel : INotifyPropertyChanged { + + protected bool UpdateField(T value, ref T field, [CallerMemberName]string name = null) { + var updated = !EqualityComparer.Default.Equals(value, field); + if (updated) { + field = value; + OnPropertyChanged(name); + } + return updated; + } + + protected void OnPropertyChanged([CallerMemberName]string name = null) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); + + public event PropertyChangedEventHandler PropertyChanged; + } +} diff --git a/src/UniversalValveToolbox/Model/Dto/AddonDtoModel.cs b/src/UniversalValveToolbox/Model/Dto/AddonDtoModel.cs new file mode 100644 index 0000000..3059a61 --- /dev/null +++ b/src/UniversalValveToolbox/Model/Dto/AddonDtoModel.cs @@ -0,0 +1,37 @@ +using UniversalValveToolbox.Base; + +namespace UniversalValveToolbox.Utils.Dto { + public class AddonDtoModel : DtoModel { + private int[] engine; + private string args; + private string bin; + private string name; + private string category; + + public int[] Engine { + get => this.engine; + set => this.UpdateField(value, ref this.engine); + } + + public string Name { + get => this.name; + set => this.UpdateField(value, ref this.name); + } + + public string Category { + get => this.category; + set => this.UpdateField(value, ref this.category); + } + + public string Bin { + get => this.bin; + set => this.UpdateField(value, ref this.bin); + } + + public string Args { + get => this.args; + set => this.UpdateField(value, ref this.args); + } + } + +} diff --git a/src/UniversalValveToolbox/Model/Dto/EngineDtoModel.cs b/src/UniversalValveToolbox/Model/Dto/EngineDtoModel.cs new file mode 100644 index 0000000..51441d3 --- /dev/null +++ b/src/UniversalValveToolbox/Model/Dto/EngineDtoModel.cs @@ -0,0 +1,30 @@ +using UniversalValveToolbox.Base; + +namespace UniversalValveToolbox.Model.Dto { + public class EngineDtoModel : DtoModel { + private int appid; + private string name; + private string bin; + private ToolDtoModel[] tools; + + public int Appid { + get => appid; + set => UpdateField(value, ref appid); + } + + public string Name { + get => name; + set => UpdateField(value, ref name); + } + + public string Bin { + get => bin; + set => UpdateField(value, ref bin); + } + + public ToolDtoModel[] Tools { + get => tools; + set => UpdateField(value, ref tools); + } + } +} diff --git a/src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs b/src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs new file mode 100644 index 0000000..fc9678c --- /dev/null +++ b/src/UniversalValveToolbox/Model/Dto/SettingsDtoModel.cs @@ -0,0 +1,36 @@ +using UniversalValveToolbox.Base; + +namespace UniversalValveToolbox.Model.Dto { + public class SettingsDtoModel : DtoModel { + private string defaultProject; + private int[] availableEnginies; + private string[] availableLanguages; + private string language; + private string theme; + + public string DefaultProject { + get => defaultProject; + set => UpdateField(value, ref defaultProject); + } + + public int[] AvailableEnginies { + get => availableEnginies; + set => UpdateField(value, ref availableEnginies); + } + + public string[] AvailableLanguages { + get => availableLanguages; + set => UpdateField(value, ref availableLanguages); + } + + public string Language { + get => language; + set => UpdateField(value, ref language); + } + + public string Theme { + get => theme; + set => UpdateField(value, ref theme); + } + } +} diff --git a/src/UniversalValveToolbox/Model/Dto/ToolDtoModel.cs b/src/UniversalValveToolbox/Model/Dto/ToolDtoModel.cs new file mode 100644 index 0000000..72ede12 --- /dev/null +++ b/src/UniversalValveToolbox/Model/Dto/ToolDtoModel.cs @@ -0,0 +1,24 @@ +using UniversalValveToolbox.Base; + +namespace UniversalValveToolbox.Model.Dto { + public class ToolDtoModel : DtoModel { + private string args; + private string bin; + private string name; + + public string Args { + get => args; + set => UpdateField(value, ref args); + } + + public string Bin { + get => bin; + set => UpdateField(value, ref bin); + } + + public string Name { + get => name; + set => UpdateField(value, ref name); + } + } +} diff --git a/src/UniversalValveToolbox/Model/Dto/VProjectDtoModel.cs b/src/UniversalValveToolbox/Model/Dto/VProjectDtoModel.cs new file mode 100644 index 0000000..4f29657 --- /dev/null +++ b/src/UniversalValveToolbox/Model/Dto/VProjectDtoModel.cs @@ -0,0 +1,30 @@ +using UniversalValveToolbox.Base; + +namespace UniversalValveToolbox.Model.Dto { + public class VProjectDtoModel : DtoModel { + private int engine; + private string path; + private string name; + private string args; + + public int Engine { + get => engine; + set => UpdateField(value, ref engine); + } + + public string Path { + get => path; + set => UpdateField(value, ref path); + } + + public string Name { + get => name; + set => UpdateField(value, ref name); + } + + public string Args { + get => args; + set => UpdateField(value, ref args); + } + } +} diff --git a/src/UniversalValveToolbox/Model/Provider/DataProvider.cs b/src/UniversalValveToolbox/Model/Provider/DataProvider.cs new file mode 100644 index 0000000..5e2c998 --- /dev/null +++ b/src/UniversalValveToolbox/Model/Provider/DataProvider.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Runtime.CompilerServices; +using System.Windows.Forms; +using Newtonsoft.Json; +using UniversalValveToolbox.Model.Dto; +using UniversalValveToolbox.Utils; + +namespace UniversalValveToolbox.Model.Provider { + class DataProvider { + + private readonly string SettingsPath = Path.Combine(Application.StartupPath, "json", "settings.json"); + + public SettingsDtoModel Settings { + get => JsonFileUtil.ReadValue(SettingsPath); + set => JsonFileUtil.WriteValue(SettingsPath, value); + } + } +} diff --git a/src/UniversalValveToolbox/Model/Provider/LanguageProvider.cs b/src/UniversalValveToolbox/Model/Provider/LanguageProvider.cs new file mode 100644 index 0000000..0952b9b --- /dev/null +++ b/src/UniversalValveToolbox/Model/Provider/LanguageProvider.cs @@ -0,0 +1,7 @@ +using UniversalValveToolbox.Properties.translations; + +namespace UniversalValveToolbox.Model.Provider { + public class LanguageProvider { + public string[] Languages { get; } = { LangDict.en_US, LangDict.ru_RU }; + } +} diff --git a/src/UniversalValveToolbox/Model/VIewModel/SettingsViewModel.cs b/src/UniversalValveToolbox/Model/VIewModel/SettingsViewModel.cs new file mode 100644 index 0000000..33830f0 --- /dev/null +++ b/src/UniversalValveToolbox/Model/VIewModel/SettingsViewModel.cs @@ -0,0 +1,32 @@ +using System; +using UniversalValveToolbox.Base; +using UniversalValveToolbox.Model.Dto; +using UniversalValveToolbox.Model.Provider; + +namespace UniversalValveToolbox.Model.VIewModel { + public class SettingsViewModel : DtoModel { + private readonly SettingsDtoModel settings; + private readonly LanguageProvider languageProvider; + + private int selectedLanguage; + + public int SelectedLanguageIndex { + get => selectedLanguage; + set { + if (UpdateField(value, ref selectedLanguage)) { + settings.Language = settings.AvailableLanguages[selectedLanguage]; + } + } + } + + public string[] Languages => languageProvider.Languages; + private string SelectedLanguage => Languages[SelectedLanguageIndex]; + + public SettingsViewModel(SettingsDtoModel settings, LanguageProvider languageProvider) { + this.settings = settings; + this.languageProvider = languageProvider; + + SelectedLanguageIndex = Math.Max(0, Array.IndexOf(settings.AvailableLanguages, settings.Language)); + } + } +} diff --git a/src/UniversalValveToolbox/Program.cs b/src/UniversalValveToolbox/Program.cs index 05abc87..6fa7345 100644 --- a/src/UniversalValveToolbox/Program.cs +++ b/src/UniversalValveToolbox/Program.cs @@ -1,5 +1,7 @@ using System; using System.Windows.Forms; +using UniversalValveToolbox.Model.Provider; +using UniversalValveToolbox.Utils; namespace UniversalValveToolbox { static class Program { @@ -8,6 +10,11 @@ namespace UniversalValveToolbox { /// [STAThread] static void Main() { + var dataProvide = new DataProvider(); + var currSettings = dataProvide.Settings; + + LanguageManager.UpdateLanguage(currSettings.Language); + Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormMain()); diff --git a/src/UniversalValveToolbox/Properties/translations/LangDict.Designer.cs b/src/UniversalValveToolbox/Properties/translations/LangDict.Designer.cs new file mode 100644 index 0000000..b7499cc --- /dev/null +++ b/src/UniversalValveToolbox/Properties/translations/LangDict.Designer.cs @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace UniversalValveToolbox.Properties.translations { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "16.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class LangDict { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal LangDict() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("UniversalValveToolbox.Properties.translations.LangDict", typeof(LangDict).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to English. + /// + internal static string en_US { + get { + return ResourceManager.GetString("en-US", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Russian. + /// + internal static string ru_RU { + get { + return ResourceManager.GetString("ru-RU", resourceCulture); + } + } + } +} diff --git a/src/UniversalValveToolbox/Properties/translations/LangDict.resx b/src/UniversalValveToolbox/Properties/translations/LangDict.resx new file mode 100644 index 0000000..b0e1d36 --- /dev/null +++ b/src/UniversalValveToolbox/Properties/translations/LangDict.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + English + + + Russian + + \ No newline at end of file diff --git a/src/UniversalValveToolbox/Properties/translations/LangDict.ru.Designer.cs b/src/UniversalValveToolbox/Properties/translations/LangDict.ru.Designer.cs new file mode 100644 index 0000000..e69de29 diff --git a/src/UniversalValveToolbox/Properties/translations/LangDict.ru.resx b/src/UniversalValveToolbox/Properties/translations/LangDict.ru.resx new file mode 100644 index 0000000..48a2634 --- /dev/null +++ b/src/UniversalValveToolbox/Properties/translations/LangDict.ru.resx @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Английский + + + Русский + + \ No newline at end of file diff --git a/src/UniversalValveToolbox/UI/FormMain.cs b/src/UniversalValveToolbox/UI/FormMain.cs index de14441..3f118e1 100644 --- a/src/UniversalValveToolbox/UI/FormMain.cs +++ b/src/UniversalValveToolbox/UI/FormMain.cs @@ -2,8 +2,9 @@ using System; using System.Diagnostics; using System.Windows.Forms; +using UniversalValveToolbox.Model.Provider; +using UniversalValveToolbox.Model.VIewModel; using UniversalValveToolbox.Utils; -using UniversalValveToolbox.Utils.Dto; namespace UniversalValveToolbox { public partial class FormMain : Form { @@ -107,8 +108,8 @@ namespace UniversalValveToolbox { } private void OpenSettings() { - var dataManager = new DataManager(); - var settingsDto = dataManager.ReadSettings(); + var dataManager = new DataProvider(); + var settingsDto = dataManager.Settings; var languageProvider = new LanguageProvider(); var settingsModel = new SettingsViewModel(settingsDto, languageProvider); @@ -116,7 +117,8 @@ namespace UniversalValveToolbox { var frmSettings = new FormSettings(settingsModel); if (frmSettings.ShowDialog() == DialogResult.OK) { - dataManager.SaveSettings(settingsDto); + dataManager.Settings = settingsDto; + Application.Restart(); } } } diff --git a/src/UniversalValveToolbox/UI/FormSettings.cs b/src/UniversalValveToolbox/UI/FormSettings.cs index 04c5aa7..35ca6d6 100644 --- a/src/UniversalValveToolbox/UI/FormSettings.cs +++ b/src/UniversalValveToolbox/UI/FormSettings.cs @@ -1,18 +1,24 @@ using kasthack.binding.wf; using System; +using System.Threading; using System.Windows.Forms; -using UniversalValveToolbox.Utils.Dto; +using UniversalValveToolbox.Model.VIewModel; namespace UniversalValveToolbox { public partial class FormSettings : Form { public FormSettings(SettingsViewModel settings) { InitializeComponent(); - comboBoxLang.SelectedIndex = 0; + comboBoxLang.Items.Clear(); + comboBoxLang.Items.AddRange(settings.Languages); + comboBoxLang.Bind(a => a.SelectedIndex, settings, a => a.SelectedLanguageIndex); + comboBoxTheme.SelectedIndex = 0; - comboBoxLang.Bind(a => a.DataSource, settings, a => a.Languages); - comboBoxLang.Bind(a => a.SelectedIndex, settings, a => a.SelectedLanguageIndex); + + //comboBoxLang.Bind(a => a.DataSource, settings, a => a.Languages); + //comboBoxLang.SelectedIndex = settings.SelectedLanguageIndex; + //this.Bind(a => a.Text, settings, a => a.Language); diff --git a/src/UniversalValveToolbox/UniversalValveToolbox.csproj b/src/UniversalValveToolbox/UniversalValveToolbox.csproj index 4329255..20b0d3d 100644 --- a/src/UniversalValveToolbox/UniversalValveToolbox.csproj +++ b/src/UniversalValveToolbox/UniversalValveToolbox.csproj @@ -60,6 +60,16 @@ + + LangDict.resx + True + True + + + True + True + LangDict.ru.resx + Form @@ -117,8 +127,26 @@ True MessageBoxes.resx - + + + + + + + + + + + + + ResXFileCodeGenerator + LangDict.Designer.cs + + + ResXFileCodeGenerator + LangDict.ru.Designer.cs + FormAbout.cs @@ -166,28 +194,28 @@ MessageBoxes.Designer.cs - Always + PreserveNewest - Always + PreserveNewest - Always + PreserveNewest - Always + PreserveNewest - Always + PreserveNewest - Always + PreserveNewest - Always + PreserveNewest - Always + PreserveNewest SettingsSingleFileGenerator diff --git a/src/UniversalValveToolbox/Utils/JsonFileUtil.cs b/src/UniversalValveToolbox/Utils/JsonFileUtil.cs new file mode 100644 index 0000000..7057063 --- /dev/null +++ b/src/UniversalValveToolbox/Utils/JsonFileUtil.cs @@ -0,0 +1,14 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace UniversalValveToolbox.Utils { + static class JsonFileUtil { + public static T ReadValue(string path) => JsonConvert.DeserializeObject(File.ReadAllText(path)); + public static void WriteValue(string path, T value) => File.WriteAllText(path, JsonConvert.SerializeObject(value, Formatting.Indented)); + } +} diff --git a/src/UniversalValveToolbox/Utils/JsonReader.cs b/src/UniversalValveToolbox/Utils/JsonReader.cs deleted file mode 100644 index b2a81f1..0000000 --- a/src/UniversalValveToolbox/Utils/JsonReader.cs +++ /dev/null @@ -1,219 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.IO; -using System.Runtime.CompilerServices; -using System.Windows.Forms; -using UniversalValveToolbox.Utils.Dto; -using Newtonsoft.Json; - -namespace UniversalValveToolbox.Utils { - class DataManager { - private readonly string SettingsPath = Path.Combine(Application.StartupPath, "json", "settings.json"); - - public Settings ReadSettings() => this.ReadModel(SettingsPath); - public void SaveSettings(Settings settingsDto) => this.WriteModel(SettingsPath, settingsDto); - - - private T ReadModel(string path) => JsonConvert.DeserializeObject(File.ReadAllText(path)); - private void WriteModel(string path, T value) => File.WriteAllText(path, JsonConvert.SerializeObject(value, Formatting.Indented)); - } -} - -namespace UniversalValveToolbox.Utils.Dto { - public class Addon : BaseModel { - private int[] engine; - - private string args; - - private string bin; - - private string name; - - public int[] Engine { - get => this.engine; - set => this.UpdateField(value, ref this.engine); - } - - public string Name { - get => this.name; - set => this.UpdateField(value, ref this.name); - } - - private string category; - - public string Category { - get => this.category; - set => this.UpdateField(value, ref this.category); - } - - public string Bin { - get => this.bin; - set => this.UpdateField(value, ref this.bin); - } - - public string Args { - get => this.args; - set => this.UpdateField(value, ref this.args); - } - } - - public class Engine : BaseModel { - private int appid; - - public int Appid { - get => this.appid; - set => this.UpdateField(value, ref this.appid); - } - - private string name; - - public string Name { - get => this.name; - set => this.UpdateField(value, ref this.name); - } - - private string bin; - - public string Bin { - get => this.bin; - set => this.UpdateField(value, ref this.bin); - } - - private Tool[] tools; - - public Tool[] Tools { - get => this.tools; - set => this.UpdateField(value, ref this.tools); - } - } - - public class Tool : BaseModel { - private string args; - - public string Args { - get => this.args; - set => this.UpdateField(value, ref this.args); - } - - private string bin; - - public string Bin { - get => this.bin; - set => this.UpdateField(value, ref this.bin); - } - - private string name; - - public string Name { - get => this.name; - set => this.UpdateField(value, ref this.name); - } - } - - public class VProject : BaseModel { - private int engine; - - public int Engine { - get => this.engine; - set => this.UpdateField(value, ref this.engine); - } - - private string path; - - public string Path { - get => this.path; - set => this.UpdateField(value, ref this.path); - } - - private string name; - - public string Name { - get => this.name; - set => this.UpdateField(value, ref this.name); - } - - private string args; - - public string Args { - get => this.args; - set => this.UpdateField(value, ref this.args); - } - } - - public class Project : Dictionary { } - - public class SettingsViewModel : BaseModel { - private readonly Settings settings; - private readonly LanguageProvider languageProvider; - - private int selectedLanguage; - - public int SelectedLanguageIndex { - get => this.selectedLanguage; - set { - if (this.UpdateField(value, ref selectedLanguage)) { - this.settings.Language = this.SelectedLanguage; - } - } - } - - public string[] Languages => this.languageProvider.Languages; - private string SelectedLanguage => this.Languages[this.SelectedLanguageIndex]; - - public SettingsViewModel(Settings settings, LanguageProvider languageProvider) { - this.settings = settings; - this.languageProvider = languageProvider; - - this.SelectedLanguageIndex = Math.Max(0, Array.IndexOf(this.Languages, settings.Language)); - } - } - - public class LanguageProvider { - public string[] Languages { get; } = { "English", "Россиян" }; - } - - public class Settings : BaseModel { - private string defaultProject; - private int[] avalibleEnginies; - private string language; - private string theme; - - public string DefaultProject { - get => this.defaultProject; - set => this.UpdateField(value, ref this.defaultProject); - } - - public int[] AvalibleEnginies { - get => this.avalibleEnginies; - set => this.UpdateField(value, ref this.avalibleEnginies); - } - - public string Language { - get => this.language; - set => this.UpdateField(value, ref this.language); - } - - public string Theme { - get => this.theme; - set => this.UpdateField(value, ref this.theme); - } - } - - public abstract class BaseModel : INotifyPropertyChanged { - - protected bool UpdateField(T value, ref T field, [CallerMemberName]string name = null) { - var updated = !EqualityComparer.Default.Equals(value, field); - if (updated) { - field = value; - OnPropertyChanged(name); - } - return updated; - } - - protected void OnPropertyChanged([CallerMemberName]string name = null) => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); - - public event PropertyChangedEventHandler PropertyChanged; - } - -} diff --git a/src/UniversalValveToolbox/Utils/LanguageManager.cs b/src/UniversalValveToolbox/Utils/LanguageManager.cs new file mode 100644 index 0000000..04ce620 --- /dev/null +++ b/src/UniversalValveToolbox/Utils/LanguageManager.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace UniversalValveToolbox.Utils { + static class LanguageManager { + public static void UpdateLanguage(string newLanguage, bool restart = false) { + var currThread = Thread.CurrentThread; + var newCultureInfo = new CultureInfo(newLanguage); + + currThread.CurrentCulture = currThread.CurrentUICulture = newCultureInfo; + + if (restart) { + Application.Restart(); + } + } + } +} diff --git a/src/UniversalValveToolbox/json/settings.json b/src/UniversalValveToolbox/json/settings.json index 4cce65b..0857413 100644 --- a/src/UniversalValveToolbox/json/settings.json +++ b/src/UniversalValveToolbox/json/settings.json @@ -1,11 +1,7 @@ { "DefaultProject": null, - "AvalibleEnginies": null, - "Language": [ - { - "selected": "en-US", - "available": [ "en-US", "ru-RU" ] - } - ], + "AvailableEnginies": null, + "Language": "en-US", + "AvailableLanguages": [ "en-US", "ru-RU" ], "Theme": null } \ No newline at end of file