添加语言设置

🎨优化电源监听逻辑
This commit is contained in:
Amazing_DM
2020-06-03 10:12:08 +08:00
parent e7daef1226
commit 9e8659a8ea
8 changed files with 167 additions and 54 deletions

View File

@@ -26,7 +26,7 @@ namespace Netch.Controllers
public const string Name = @"Netch";
public const string Copyright = @"Copyright © 2019 - 2020";
public const string Version = @"1.4.8";
public const string Version = @"1.4.9";
public async void Check(bool notifyNoFound, bool isPreRelease)
{

View File

@@ -68,16 +68,21 @@ namespace Netch.Forms
/// <param name="e"></param>
void SystemEvents_PowerModeChanged(object sender, PowerModeChangedEventArgs e)
{
switch (e.Mode)
//不对Netch命令等待状态的电源事件做任何处理
if (!State.Equals(Models.State.Waiting))
{
case PowerModes.Suspend://操作系统即将挂起
Logging.Info("操作系统即将挂起,自动停止===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
ControlFun();
break;
case PowerModes.Resume://操作系统即将从挂起状态继续
Logging.Info("操作系统即将从挂起状态继续,自动重启===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
ControlFun();
break;
switch (e.Mode)
{
case PowerModes.Suspend://操作系统即将挂起
Logging.Info("操作系统即将挂起,自动停止===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
ControlFun();
break;
case PowerModes.Resume://操作系统即将从挂起状态继续
Logging.Info("操作系统即将从挂起状态继续,自动重启===>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
ControlFun();
break;
}
}
}
private void CheckUpdate()
@@ -336,9 +341,6 @@ namespace Netch.Forms
private void MainForm_Load(object sender, EventArgs e)
{
// 加载配置
Utils.Configuration.Load();
// 加载服务器
InitServer();
@@ -829,7 +831,7 @@ namespace Netch.Forms
}
public void ControlFun()
{
//聚焦到启动按钮,防止模式选择框变成蓝色:D
//聚焦到启动按钮,防止模式选择框变成蓝色:D,不过好像没啥用
ControlButton.Focus();
SaveConfigs();
if (State == Models.State.Waiting || State == Models.State.Stopped)

View File

@@ -52,6 +52,8 @@
this.ControlButton = new System.Windows.Forms.Button();
this.GlobalBypassIPsButton = new System.Windows.Forms.Button();
this.BehaviorGroupBox = new System.Windows.Forms.GroupBox();
this.LanguageLabel = new System.Windows.Forms.Label();
this.LanguageComboBox = new System.Windows.Forms.ComboBox();
this.BootShadowsocksFromDLLCheckBox = new System.Windows.Forms.CheckBox();
this.AclAddr = new System.Windows.Forms.TextBox();
this.AclLabel = new System.Windows.Forms.Label();
@@ -297,6 +299,8 @@
//
// BehaviorGroupBox
//
this.BehaviorGroupBox.Controls.Add(this.LanguageLabel);
this.BehaviorGroupBox.Controls.Add(this.LanguageComboBox);
this.BehaviorGroupBox.Controls.Add(this.BootShadowsocksFromDLLCheckBox);
this.BehaviorGroupBox.Controls.Add(this.AclAddr);
this.BehaviorGroupBox.Controls.Add(this.AclLabel);
@@ -323,6 +327,24 @@
this.BehaviorGroupBox.TabStop = false;
this.BehaviorGroupBox.Text = "Behavior";
//
// LanguageLabel
//
this.LanguageLabel.AutoSize = true;
this.LanguageLabel.Location = new System.Drawing.Point(12, 305);
this.LanguageLabel.Name = "LanguageLabel";
this.LanguageLabel.Size = new System.Drawing.Size(65, 17);
this.LanguageLabel.TabIndex = 23;
this.LanguageLabel.Text = "Language";
//
// LanguageComboBox
//
this.LanguageComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.LanguageComboBox.FormattingEnabled = true;
this.LanguageComboBox.Location = new System.Drawing.Point(120, 302);
this.LanguageComboBox.Name = "LanguageComboBox";
this.LanguageComboBox.Size = new System.Drawing.Size(121, 25);
this.LanguageComboBox.TabIndex = 22;
//
// BootShadowsocksFromDLLCheckBox
//
this.BootShadowsocksFromDLLCheckBox.AutoSize = true;
@@ -335,7 +357,7 @@
//
// AclAddr
//
this.AclAddr.Location = new System.Drawing.Point(120, 302);
this.AclAddr.Location = new System.Drawing.Point(120, 273);
this.AclAddr.Name = "AclAddr";
this.AclAddr.Size = new System.Drawing.Size(315, 23);
this.AclAddr.TabIndex = 19;
@@ -344,7 +366,7 @@
// AclLabel
//
this.AclLabel.AutoSize = true;
this.AclLabel.Location = new System.Drawing.Point(12, 305);
this.AclLabel.Location = new System.Drawing.Point(12, 276);
this.AclLabel.Name = "AclLabel";
this.AclLabel.Size = new System.Drawing.Size(78, 17);
this.AclLabel.TabIndex = 20;
@@ -353,7 +375,7 @@
// DetectionInterval_Label
//
this.DetectionInterval_Label.AutoSize = true;
this.DetectionInterval_Label.Location = new System.Drawing.Point(229, 221);
this.DetectionInterval_Label.Location = new System.Drawing.Point(229, 192);
this.DetectionInterval_Label.Name = "DetectionInterval_Label";
this.DetectionInterval_Label.Size = new System.Drawing.Size(136, 17);
this.DetectionInterval_Label.TabIndex = 18;
@@ -361,7 +383,7 @@
//
// DetectionInterval_TextBox
//
this.DetectionInterval_TextBox.Location = new System.Drawing.Point(367, 218);
this.DetectionInterval_TextBox.Location = new System.Drawing.Point(367, 189);
this.DetectionInterval_TextBox.Name = "DetectionInterval_TextBox";
this.DetectionInterval_TextBox.Size = new System.Drawing.Size(68, 23);
this.DetectionInterval_TextBox.TabIndex = 17;
@@ -370,7 +392,7 @@
// EnableStartedTcping_CheckBox
//
this.EnableStartedTcping_CheckBox.AutoSize = true;
this.EnableStartedTcping_CheckBox.Location = new System.Drawing.Point(144, 220);
this.EnableStartedTcping_CheckBox.Location = new System.Drawing.Point(144, 191);
this.EnableStartedTcping_CheckBox.Name = "EnableStartedTcping_CheckBox";
this.EnableStartedTcping_CheckBox.Size = new System.Drawing.Size(66, 21);
this.EnableStartedTcping_CheckBox.TabIndex = 15;
@@ -380,7 +402,7 @@
// DelayTestAfterStartup_Label
//
this.DelayTestAfterStartup_Label.AutoSize = true;
this.DelayTestAfterStartup_Label.Location = new System.Drawing.Point(12, 221);
this.DelayTestAfterStartup_Label.Location = new System.Drawing.Point(12, 192);
this.DelayTestAfterStartup_Label.Name = "DelayTestAfterStartup_Label";
this.DelayTestAfterStartup_Label.Size = new System.Drawing.Size(126, 17);
this.DelayTestAfterStartup_Label.TabIndex = 16;
@@ -388,7 +410,7 @@
//
// STUN_ServerPortTextBox
//
this.STUN_ServerPortTextBox.Location = new System.Drawing.Point(120, 273);
this.STUN_ServerPortTextBox.Location = new System.Drawing.Point(120, 244);
this.STUN_ServerPortTextBox.Name = "STUN_ServerPortTextBox";
this.STUN_ServerPortTextBox.Size = new System.Drawing.Size(315, 23);
this.STUN_ServerPortTextBox.TabIndex = 8;
@@ -397,7 +419,7 @@
// STUNServerPortLabel
//
this.STUNServerPortLabel.AutoSize = true;
this.STUNServerPortLabel.Location = new System.Drawing.Point(12, 276);
this.STUNServerPortLabel.Location = new System.Drawing.Point(12, 247);
this.STUNServerPortLabel.Name = "STUNServerPortLabel";
this.STUNServerPortLabel.Size = new System.Drawing.Size(110, 17);
this.STUNServerPortLabel.TabIndex = 12;
@@ -406,7 +428,7 @@
// STUNServerLabel
//
this.STUNServerLabel.AutoSize = true;
this.STUNServerLabel.Location = new System.Drawing.Point(12, 250);
this.STUNServerLabel.Location = new System.Drawing.Point(12, 221);
this.STUNServerLabel.Name = "STUNServerLabel";
this.STUNServerLabel.Size = new System.Drawing.Size(82, 17);
this.STUNServerLabel.TabIndex = 10;
@@ -424,7 +446,7 @@
//
// STUN_ServerTextBox
//
this.STUN_ServerTextBox.Location = new System.Drawing.Point(120, 244);
this.STUN_ServerTextBox.Location = new System.Drawing.Point(120, 215);
this.STUN_ServerTextBox.Name = "STUN_ServerTextBox";
this.STUN_ServerTextBox.Size = new System.Drawing.Size(315, 23);
this.STUN_ServerTextBox.TabIndex = 11;
@@ -443,7 +465,7 @@
// ProfileCount_Label
//
this.ProfileCount_Label.AutoSize = true;
this.ProfileCount_Label.Location = new System.Drawing.Point(12, 196);
this.ProfileCount_Label.Location = new System.Drawing.Point(12, 167);
this.ProfileCount_Label.Name = "ProfileCount_Label";
this.ProfileCount_Label.Size = new System.Drawing.Size(79, 17);
this.ProfileCount_Label.TabIndex = 8;
@@ -451,7 +473,7 @@
//
// ProfileCount_TextBox
//
this.ProfileCount_TextBox.Location = new System.Drawing.Point(209, 193);
this.ProfileCount_TextBox.Location = new System.Drawing.Point(209, 164);
this.ProfileCount_TextBox.Name = "ProfileCount_TextBox";
this.ProfileCount_TextBox.Size = new System.Drawing.Size(226, 23);
this.ProfileCount_TextBox.TabIndex = 9;
@@ -575,5 +597,7 @@
private System.Windows.Forms.Label AclLabel;
private System.Windows.Forms.CheckBox UseFakeDNSCheckBox;
private System.Windows.Forms.CheckBox BootShadowsocksFromDLLCheckBox;
private System.Windows.Forms.Label LanguageLabel;
private System.Windows.Forms.ComboBox LanguageComboBox;
}
}

View File

@@ -1,5 +1,9 @@
using System;
using Netch.Models.SSD;
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using TaskScheduler;
@@ -101,6 +105,10 @@ namespace Netch.Forms
AclLabel.Text = Utils.i18N.Translate(AclLabel.Text);
AclAddr.Text = Global.Settings.ACL.ToString();
LanguageLabel.Text = Utils.i18N.Translate(LanguageLabel.Text);
LanguageComboBox.Items.AddRange(Utils.i18N.GetTranslateList().ToArray());
LanguageComboBox.SelectedItem = Global.Settings.Language;
if (Global.Settings.TUNTAP.DNS.Count > 0)
{
var dns = "";
@@ -160,7 +168,44 @@ namespace Netch.Forms
Global.Settings.MinimizeWhenStarted = MinimizeWhenStartedCheckBox.Checked;
Global.Settings.RunAtStartup = RunAtStartup.Checked;
Global.Settings.BootShadowsocksFromDLL = BootShadowsocksFromDLLCheckBox.Checked;
Global.Settings.Language = LanguageComboBox.SelectedItem.ToString();
// 加载系统语言
if (Global.Settings.Language.Equals("System"))
{
// 得到当前线程语言代码
var culture = CultureInfo.CurrentCulture.Name;
// 尝试加载内置中文语言
if (culture == "zh-CN")
{
// 加载语言
Utils.i18N.Load(Encoding.UTF8.GetString(Properties.Resources.zh_CN));
}
// 从外置文件中加载语言
if (File.Exists($"i18n\\{culture}"))
{
// 加载语言
Utils.i18N.Load(File.ReadAllText($"i18n\\{culture}"));
}
}
if (Global.Settings.Language.Equals("zh-CN"))
{
// 加载内置中文
Utils.i18N.Load(Encoding.UTF8.GetString(Properties.Resources.zh_CN));
}
else if (Global.Settings.Language.Equals("en-US"))
{
// 加载内置英文
Utils.i18N.Load(Global.Settings.Language);
}
else if (File.Exists($"i18n\\{Global.Settings.Language}"))
{
// 从外置文件中加载语言
Utils.i18N.Load(File.ReadAllText($"i18n\\{Global.Settings.Language}"));
}
// 开机自启判断
TaskSchedulerClass scheduler = new TaskSchedulerClass();
@@ -388,7 +433,7 @@ namespace Netch.Forms
Global.Settings.TUNTAP.UseFakeDNS = UseFakeDNSCheckBox.Checked;
Utils.Configuration.Save();
MessageBox.Show(Utils.i18N.Translate("Saved"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
MessageBox.Show(Utils.i18N.Translate("Saved. Modify some settings need to restart the software"), Utils.i18N.Translate("Information"), MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();
}
}

View File

@@ -178,5 +178,10 @@ namespace Netch.Models
/// 是否使用DLL启动Shadowsocks
/// </summary>
public bool BootShadowsocksFromDLL = false;
/// <summary>
/// 语言设置
/// </summary>
public string Language = "System";
}
}

View File

@@ -49,41 +49,48 @@ namespace Netch
}
}
// 得到当前线程语言代码
var culture = CultureInfo.CurrentCulture.Name;
// 加载配置
Utils.Configuration.Load();
// 如果命令行参数只有一个,且传入有效语言代码,那么覆盖掉已得到的语言代码
if (args.Length == 1)
// 加载系统语言
if (Global.Settings.Language.Equals("System"))
{
try
// 得到当前线程语言代码
var culture = CultureInfo.CurrentCulture.Name;
// 尝试加载内置中文语言
if (culture == "zh-CN")
{
culture = CultureInfo.GetCultureInfo(args[0]).Name;
// 加载语言
Utils.i18N.Load(Encoding.UTF8.GetString(Properties.Resources.zh_CN));
}
catch (CultureNotFoundException)
// 从外置文件中加载语言
if (File.Exists($"i18n\\{culture}"))
{
// 跳过
// 加载语言
Utils.i18N.Load(File.ReadAllText($"i18n\\{culture}"));
}
}
if (Global.Settings.Language.Equals("zh-CN"))
{
// 加载内置中文
Utils.i18N.Load(Encoding.UTF8.GetString(Properties.Resources.zh_CN));
}
else if (Global.Settings.Language.Equals("en-US"))
{
// 加载内置英文
Utils.i18N.Load(Global.Settings.Language);
}
else if (File.Exists($"i18n\\{Global.Settings.Language}"))
{
// 从外置文件中加载语言
Utils.i18N.Load(File.ReadAllText($"i18n\\{Global.Settings.Language}"));
}
// 记录当前系统语言
Utils.Logging.Info($"当前系统语言:{culture}");
// 尝试加载内置中文语言
if (culture == "zh-CN")
{
// 加载语言
Utils.i18N.Load(Encoding.UTF8.GetString(Properties.Resources.zh_CN));
}
// 记录当前程序语言
Utils.Logging.Info($"当前程序语言:{culture}");
// 从外置文件中加载语言
if (File.Exists($"i18n\\{culture}"))
{
// 加载语言
Utils.i18N.Load(File.ReadAllText($"i18n\\{culture}"));
}
Utils.Logging.Info($"当前语言:{Global.Settings.Language}");
// 检查是否已经运行
if (!mutex.WaitOne(0, false))

View File

@@ -177,6 +177,8 @@
"STUN Server": "STUN 服务器",
"STUN Server Port": "STUN 服务器端口",
"Custom ACL": "自定义 ACL 规则",
"Language": "语言",
"Saved. Modify some settings need to restart the software": "保存成功,修改部分设置需重启软件",
"Profile": "配置名",
"Profiles": "配置",

View File

@@ -1,5 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
namespace Netch.Utils
{
@@ -16,6 +18,11 @@ namespace Netch.Utils
/// <param name="text">语言文件</param>
public static void Load(string text)
{
if (text.Equals("en-US"))
{
Data.Clear();
return;
}
var data = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, string>>(text);
if (data != null)
@@ -42,5 +49,26 @@ namespace Netch.Utils
return text;
}
/// <summary>
/// 获取可使用的语言
/// </summary>
/// <returns></returns>
public static List<string> GetTranslateList()
{
List<string> TranslateFile = new List<string>();
TranslateFile.Add("System");
TranslateFile.Add("zh-CN");
TranslateFile.Add("en-US");
if (Directory.Exists("i18n"))
{
foreach (var fileName in Directory.GetFiles("i18n", "*"))
{
TranslateFile.Add(fileName.Substring(5));
}
}
return TranslateFile;
}
}
}