Skip to content

Commit

Permalink
Fixed #111- Added the ability to change the language within the app
Browse files Browse the repository at this point in the history
  • Loading branch information
ShankarBUS committed Oct 19, 2020
1 parent e1685d5 commit d4fe47b
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 3 deletions.
6 changes: 6 additions & 0 deletions ModernFlyouts/Classes/AppDataHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,12 @@ public static Orientation SessionsPanelOrientation
set => SetEnum(value);
}

public static string Language
{
get => GetString();
set => SetString(value);
}

#endregion
}
}
3 changes: 3 additions & 0 deletions ModernFlyouts/Navigation/GeneralSettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<GroupBox Header="{x:Static resx:Strings.Settings_Behavior}">
<ui:ToggleSwitch Header="{x:Static resx:Strings.Settings_RunAtStartup}" IsOn="{Binding RunAtStartup}" />
</GroupBox>
<ComboBox ui:ControlHelper.Header="{x:Static resx:Strings.Settings_Language}"
ItemsSource="{x:Static utils:LocalizationHelper.SupportedLanguages}"
SelectedItem="{Binding Path=(utils:LocalizationHelper.CurrentLanguage)}" />
<GroupBox Header="{x:Static resx:Strings.Settings_DefaultFlyout}">
<StackPanel>
<ui:RadioButtons x:Name="DefaultFlyoutSelector"
Expand Down
5 changes: 4 additions & 1 deletion ModernFlyouts/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using ModernFlyouts.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
Expand All @@ -23,6 +24,8 @@ static void Main(string[] args)

DUIHandler.ForceFindDUIAndHide(false);

LocalizationHelper.Initialize();

var app = new App();
app.Run();
}
Expand Down
29 changes: 28 additions & 1 deletion ModernFlyouts/Properties/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion ModernFlyouts/Properties/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,14 @@
<data name="Settings.FlyoutTopBar" xml:space="preserve">
<value>Flyout TopBar</value>
</data>
<data name="Settings.Language" xml:space="preserve">
<value>Language</value>
</data>
<data name="Settings.LanguageDescription" xml:space="preserve">
<value>Please restart the app to change the language properly</value>
</data>
<data name="Settings.MaxVerticalSessionControlsCount" xml:space="preserve">
<value>Maximum Number of Media Session Controls vertically</value>
<value>Maximum Number of Media Session Controls to show vertically</value>
</data>
<data name="Settings.Modules" xml:space="preserve">
<value>Modules</value>
Expand Down Expand Up @@ -296,6 +302,9 @@
<data name="Settings.ShowVolumeControlInGSMTCFlyout" xml:space="preserve">
<value>Show Volume control in Global System Media Transport (Media) Controls flyout</value>
</data>
<data name="Settings.SystemDefault" xml:space="preserve">
<value>System Default</value>
</data>
<data name="Settings.TrayIconMode" xml:space="preserve">
<value>Tray Icon Mode</value>
</data>
Expand Down
126 changes: 126 additions & 0 deletions ModernFlyouts/Utilities/LocalizationHelper.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,88 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Windows.Data;

namespace ModernFlyouts.Utilities
{
public class LocalizationHelper
{
private static CultureInfo systemUICulture;

#region Properties

private static LanguageInfo currentLanguage;

public static LanguageInfo CurrentLanguage
{
get { return currentLanguage; }
set
{
if (currentLanguage != value)
{
currentLanguage = value;
OnStaticPropertyChanged();
OnCurrentLanguageChanged();
}
}
}

public static ObservableCollection<LanguageInfo> SupportedLanguages { get; private set; }

#endregion

public static void Initialize()
{
systemUICulture = CultureInfo.CurrentUICulture;
SupportedLanguages = GetAllSupportedLanguages();
var language = AppDataHelper.Language;
CurrentLanguage = SupportedLanguages.First(x => x.LanguageName == language);
}

private static ObservableCollection<LanguageInfo> GetAllSupportedLanguages()
{
var supportedLanguages = new ObservableCollection<LanguageInfo>();

supportedLanguages.Add(new LanguageInfo(string.Empty));

var resourceManager = Properties.Strings.ResourceManager;
var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
foreach (var culture in cultures)
{
try
{
if (culture.Equals(CultureInfo.InvariantCulture))
{
continue;
}

var resourceSet = resourceManager.GetResourceSet(culture, true, false);
if (resourceSet != null)
{
supportedLanguages.Add(new LanguageInfo(culture));
}
}
catch (CultureNotFoundException)
{

}
}

return supportedLanguages;
}

private static void OnCurrentLanguageChanged()
{
var uiCulture = CurrentLanguage.CultureInfo ?? systemUICulture;
Thread.CurrentThread.CurrentUICulture = uiCulture;
AppDataHelper.Language = CurrentLanguage.LanguageName;
}

public static string GetLocalisedEnumValue(Enum value)
{
var resourceId = $"Enums.{value.GetType().Name}.{value}";
Expand All @@ -20,6 +96,56 @@ public static string GetLocalisedEnumValue(Enum value)
return value.ToString();
}
}

public static event PropertyChangedEventHandler StaticPropertyChanged;

private static void OnStaticPropertyChanged([CallerMemberName] string propertyName = "")
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(propertyName));
}
}

public class LanguageInfo
{
public LanguageInfo(CultureInfo cultureInfo)
{
CultureInfo = cultureInfo;
Initialize();
}

public LanguageInfo(string cultureName)
{
if (!string.IsNullOrEmpty(cultureName))
{
CultureInfo = new CultureInfo(cultureName);
}
Initialize();
}

public CultureInfo CultureInfo { get; private set; }

public string DisplayName { get; private set; }

public string LanguageName { get; private set; }

private void Initialize()
{
if (CultureInfo != null)
{
DisplayName = $"{CultureInfo.NativeName} - {CultureInfo.EnglishName} [{CultureInfo.Name}]";
LanguageName = CultureInfo.Name;
}
else
{
DisplayName = Properties.Strings.Settings_SystemDefault;
LanguageName = string.Empty;
}
}

public override string ToString()
{
return DisplayName;
}
}

public class EnumToLocalizedStringConverter : IValueConverter
Expand Down

3 comments on commit d4fe47b

@Samuel12321
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@ShankarBUS
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Samuel12321,

I have noticed a new bug.

When you scroll the language selection ComboBox with mouse, the settings page scrolls instead of the combobox popup.
We may need to disable smooth scrolling to make it work properly.

@ShankarBUS
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Samuel12321,

I have noticed a new bug.

When you scroll the language selection ComboBox with mouse, the settings page scrolls instead of the combobox popup.
We may need to disable smooth scrolling to make it work properly.

Never mind fixed it in c194db2

Please sign in to comment.