change language in runtime without restart application

This commit is contained in:
Mohsen Shahindust 2023-04-12 17:51:47 +03:30
parent 92baf9025a
commit 44df650e5f
5 changed files with 41 additions and 11 deletions

View file

@ -5,8 +5,7 @@
xmlns:conv="clr-namespace:v2rayN.Converters" xmlns:conv="clr-namespace:v2rayN.Converters"
xmlns:local="clr-namespace:v2rayN" xmlns:local="clr-namespace:v2rayN"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes" xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
ShutdownMode="OnExplicitShutdown" ShutdownMode="OnExplicitShutdown">
StartupUri="Views/MainWindow.xaml">
<Application.Resources> <Application.Resources>
<ResourceDictionary xmlns:system="clr-namespace:System;assembly=mscorlib"> <ResourceDictionary xmlns:system="clr-namespace:System;assembly=mscorlib">
<ResourceDictionary.MergedDictionaries> <ResourceDictionary.MergedDictionaries>

View file

@ -1,9 +1,11 @@
using System.Runtime.InteropServices; using ProtosLib.Statistics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows; using System.Windows;
using System.Windows.Threading; using System.Windows.Threading;
using v2rayN.Handler; using v2rayN.Handler;
using v2rayN.Mode;
using v2rayN.Tool; using v2rayN.Tool;
using v2rayN.Views;
namespace v2rayN namespace v2rayN
{ {
@ -13,8 +15,8 @@ namespace v2rayN
public partial class App : Application public partial class App : Application
{ {
public static EventWaitHandle ProgramStarted; public static EventWaitHandle ProgramStarted;
private static Config _config; private static v2rayN.Mode.Config _config;
public static bool ignoreClosing { get; set; } = true;
public App() public App()
{ {
// Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly()); // Locator.CurrentMutable.RegisterViewsForViewModels(Assembly.GetCallingAssembly());
@ -53,6 +55,8 @@ namespace v2rayN
Thread.CurrentThread.CurrentUICulture = new(_config.uiItem.currentLanguage); Thread.CurrentThread.CurrentUICulture = new(_config.uiItem.currentLanguage);
base.OnStartup(e); base.OnStartup(e);
Current.MainWindow = new MainWindow();
Current.MainWindow.Show();
} }
@ -88,5 +92,22 @@ namespace v2rayN
{ {
Utils.SaveLog("TaskScheduler_UnobservedTaskException", e.Exception); Utils.SaveLog("TaskScheduler_UnobservedTaskException", e.Exception);
} }
public static void ChangeCulture(CultureInfo newCulture)
{
if (newCulture != null)
{
Thread.CurrentThread.CurrentCulture = newCulture;
Thread.CurrentThread.CurrentUICulture = newCulture;
_config.uiItem.currentLanguage = newCulture.Name;
var oldWindow = Current.MainWindow;
ignoreClosing = false;
Current.MainWindow = new MainWindow();
oldWindow.Close();
ignoreClosing = true;
Current.MainWindow.Show();
}
}
} }
} }

View file

@ -111,7 +111,7 @@
}; };
public static readonly List<string> allowInsecures = new() { "true", "false", "" }; public static readonly List<string> allowInsecures = new() { "true", "false", "" };
public static readonly List<string> domainStrategy4Freedoms = new() { "AsIs", "UseIP", "UseIPv4", "UseIPv6", "" }; public static readonly List<string> domainStrategy4Freedoms = new() { "AsIs", "UseIP", "UseIPv4", "UseIPv6", "" };
public static readonly List<string> Languages = new() { "zh-Hans", "en", "fa-Ir", "ru" }; public static readonly List<string> Languages = new() { "zh-Hans", "en", "fa-IR", "ru" };
public static readonly List<string> alpns = new() { "h2", "http/1.1", "h2,http/1.1", "" }; public static readonly List<string> alpns = new() { "h2", "http/1.1", "h2,http/1.1", "" };
public static readonly List<string> LogLevel = new() { "debug", "info", "warning", "error", "none" }; public static readonly List<string> LogLevel = new() { "debug", "info", "warning", "error", "none" };
public static readonly List<string> InboundTags = new() { "socks", "http", "socks2", "http2" }; public static readonly List<string> InboundTags = new() { "socks", "http", "socks2", "http2" };

View file

@ -355,6 +355,7 @@
Width="100" Width="100"
Margin="8" Margin="8"
materialDesign:HintAssist.Hint="Language" materialDesign:HintAssist.Hint="Language"
SelectionChanged="cmbCurrentLanguage_SelectionChanged"
Style="{StaticResource DefComboBox}" /> Style="{StaticResource DefComboBox}" />
</Grid> </Grid>

View file

@ -1,6 +1,7 @@
using ReactiveUI; using ReactiveUI;
using Splat; using Splat;
using System.ComponentModel; using System.ComponentModel;
using System.Globalization;
using System.Reactive.Disposables; using System.Reactive.Disposables;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
@ -225,8 +226,11 @@ namespace v2rayN.Views
private void MainWindow_Closing(object? sender, CancelEventArgs e) private void MainWindow_Closing(object? sender, CancelEventArgs e)
{ {
e.Cancel = true; if (App.ignoreClosing)
ViewModel?.ShowHideWindow(false); {
e.Cancel = true;
ViewModel?.ShowHideWindow(false);
}
} }
private void menuExit_Click(object sender, RoutedEventArgs e) private void menuExit_Click(object sender, RoutedEventArgs e)
@ -605,8 +609,13 @@ namespace v2rayN.Views
} }
} }
#endregion #endregion
private void cmbCurrentLanguage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count != 0 && e.RemovedItems.Count != 0)
App.ChangeCulture(new CultureInfo(e.AddedItems[0].ToString()));
}
} }
} }