From b02da10dbc9e2b25bf6f28e4ee261a3b83717f95 Mon Sep 17 00:00:00 2001 From: Tichau Date: Tue, 14 Jun 2016 19:25:53 +0200 Subject: [PATCH] Add an option to chose the language of the application --- .../FileConverter/FileConverter.csproj | 10 +-- Application/FileConverter/Helpers.cs | 26 ++++++ .../Properties/Resources.fr.resx | 2 +- .../FileConverter/Settings.Settings.cs | 85 +++++++++++++++++++ .../FileConverter/Windows/SettingsWindow.xaml | 5 ++ .../Windows/SettingsWindow.xaml.cs | 17 +++- 6 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 Application/FileConverter/Helpers.cs diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 7ae4163..7dd801f 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -107,14 +107,14 @@ + - - Resources.en.resx + True True - True + Resources.fr.resx @@ -210,9 +210,9 @@ Settings.settings True - + PublicResXFileCodeGenerator - Resources.en.Designer.cs + Resources.fr1.Designer.cs PublicResXFileCodeGenerator diff --git a/Application/FileConverter/Helpers.cs b/Application/FileConverter/Helpers.cs new file mode 100644 index 0000000..eee12f3 --- /dev/null +++ b/Application/FileConverter/Helpers.cs @@ -0,0 +1,26 @@ +// // License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter +{ + using System; + using System.Collections.Generic; + using System.Globalization; + using System.IO; + using System.Linq; + using System.Reflection; + + public static class Helpers + { + public static IEnumerable GetSupportedCulture() + { + //Get all culture + CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures); + + //Find the location where application installed. + string exeLocation = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path)); + + //Return all culture for which satellite folder found with culture code. + return cultures.Where(cultureInfo => Directory.Exists(Path.Combine(exeLocation, cultureInfo.Name))); + } + } +} \ No newline at end of file diff --git a/Application/FileConverter/Properties/Resources.fr.resx b/Application/FileConverter/Properties/Resources.fr.resx index 6308e9a..018df5b 100644 --- a/Application/FileConverter/Properties/Resources.fr.resx +++ b/Application/FileConverter/Properties/Resources.fr.resx @@ -296,7 +296,7 @@ Special paths: See change log ... - Settings + Paramètres Settings diff --git a/Application/FileConverter/Settings.Settings.cs b/Application/FileConverter/Settings.Settings.cs index 28eee9d..3084a4c 100644 --- a/Application/FileConverter/Settings.Settings.cs +++ b/Application/FileConverter/Settings.Settings.cs @@ -1,5 +1,9 @@ // License: http://www.gnu.org/licenses/gpl.html GPL version 3. +using System.Globalization; +using System.Windows; +using System.Windows.Markup; + namespace FileConverter { using System.Collections.ObjectModel; @@ -14,6 +18,7 @@ namespace FileConverter private float durationBetweenEndOfConversionsAndApplicationExit = 3f; private ObservableCollection conversionPresets = new ObservableCollection(); private bool checkUpgradeAtStartup = true; + private CultureInfo applicationLanguage; [XmlAttribute] public int SerializationVersion @@ -22,6 +27,57 @@ namespace FileConverter set; } = Version; + [XmlIgnore] + public CultureInfo ApplicationLanguage + { + get + { + return this.applicationLanguage; + } + + set + { + if (this.applicationLanguage != null && this.applicationLanguage.Equals(value)) + { + return; + } + + this.applicationLanguage = value; + if (this.applicationLanguage != null) + { + System.Threading.Thread.CurrentThread.CurrentCulture = this.applicationLanguage; + System.Threading.Thread.CurrentThread.CurrentUICulture = this.applicationLanguage; + } + + this.OnPropertyChanged(); + } + } + + [XmlElement] + public string ApplicationLanguageName + { + get + { + if (this.ApplicationLanguage == null) + { + return string.Empty; + } + + return this.ApplicationLanguage.Name; + } + + set + { + if (string.IsNullOrEmpty(value)) + { + this.ApplicationLanguage = null; + return; + } + + this.ApplicationLanguage = CultureInfo.GetCultureInfo(value); + } + } + [XmlIgnore] public ObservableCollection ConversionPresets { @@ -107,6 +163,35 @@ namespace FileConverter { this.ConversionPresets[index].OnDeserializationComplete(); } + + // Initialize application if it was not deserialized from the settings. + if (this.ApplicationLanguage == null) + { + CultureInfo bestCandidate = null; + CultureInfo currentUICulture = System.Threading.Thread.CurrentThread.CurrentUICulture; + foreach (CultureInfo culture in Helpers.GetSupportedCulture()) + { + if (culture.Equals(currentUICulture)) + { + bestCandidate = culture; + break; + } + else if (culture.Equals(currentUICulture.Parent)) + { + bestCandidate = culture; + } + } + + if (bestCandidate != null) + { + this.ApplicationLanguage = bestCandidate; + } + else + { + Diagnostics.Debug.Log("Can't find supported culture info for culture {0}. Fallback to default culture.", currentUICulture); + this.ApplicationLanguage = CultureInfo.GetCultureInfo("en"); + } + } } } } diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml b/Application/FileConverter/Windows/SettingsWindow.xaml index 98d1a26..502e6db 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml +++ b/Application/FileConverter/Windows/SettingsWindow.xaml @@ -755,6 +755,11 @@ + + +