Skip to content

Commit

Permalink
Merge pull request #2 from 2dust/master
Browse files Browse the repository at this point in the history
Added automatically set the program's language based on the system's language settings
  • Loading branch information
Phleesty authored Nov 17, 2024
2 parents 1b75ff5 + fb4b8d9 commit 2e8b666
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 13 deletions.
131 changes: 131 additions & 0 deletions v2rayN/AmazTool/LocalizationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System.Collections.Generic;
using System.Globalization;

namespace AmazTool
{
public class LocalizationHelper
{
/// <summary>
/// 获取系统当前语言的本地化字符串
/// </summary>
/// <param name="key">要翻译的关键字</param>
/// <returns>对应语言的本地化字符串,如果没有找到则返回关键字</returns>
public static string GetLocalizedValue(string key)
{
// 定义支持的语言
HashSet<string> supportedLanguages = ["zh", "en"];

// 获取当前系统语言的 ISO 两字母代码
string currentLanguage = CultureInfo.CurrentCulture.TwoLetterISOLanguageName;

// 如果当前语言不在支持的语言列表中,默认使用英文
if (!supportedLanguages.Contains(currentLanguage))
{
currentLanguage = "en";
}

// 尝试获取对应语言的翻译
if (languageResources.TryGetValue(key, out var translations))
{
if (translations.TryGetValue(currentLanguage, out var translation))
{
return translation;
}
}

// 如果未找到翻译,返回关键字本身
return key;
}

/// <summary>
/// 存储不同语言的本地化资源
/// </summary>
public static Dictionary<string, Dictionary<string, string>> languageResources = new()
{
{
"Guidelines", new Dictionary<string, string>
{
{ "en", "Please run it from the main application." },
{ "zh", "请从主应用运行!" }
}
},
{
"Upgrade_File_Not_Found", new Dictionary<string, string>
{
{ "en", "Upgrade failed, file not found." },
{ "zh", "升级失败,文件不存在!" }
}
},
{
"In_Progress", new Dictionary<string, string>
{
{ "en", "In progress, please wait..." },
{ "zh", "正在进行中,请等待..." }
}
},
{
"Try_Terminate_Process", new Dictionary<string, string>
{
{ "en", "Try to terminate the v2rayN process." },
{ "zh", "尝试结束 v2rayN 进程..." }
}
},
{
"Failed_Terminate_Process", new Dictionary<string, string>
{
{ "en", "Failed to terminate the v2rayN.Close it manually,or the upgrade may fail." },
{ "zh", "请手动关闭正在运行的v2rayN,否则可能升级失败。" }
}
},
{
"Start_Unzipping", new Dictionary<string, string>
{
{ "en", "Start extracting the update package." },
{ "zh", "开始解压缩更新包..." }
}
},
{
"Success_Unzipping", new Dictionary<string, string>
{
{ "en", "Successfully extracted the update package!" },
{ "zh", "解压缩更新包成功!" }
}
},
{
"Failed_Unzipping", new Dictionary<string, string>
{
{ "en", "Failed to extract the update package!" },
{ "zh", "解压缩更新包失败!" }
}
},
{
"Failed_Upgrade", new Dictionary<string, string>
{
{ "en", "Upgrade failed!" },
{ "zh", "升级失败!" }
}
},
{
"Success_Upgrade", new Dictionary<string, string>
{
{ "en", "Upgrade success!" },
{ "zh", "升级成功!" }
}
},
{
"Information", new Dictionary<string, string>
{
{ "en", "Information" },
{ "zh", "提示" }
}
},
{
"Restart_v2rayN", new Dictionary<string, string>
{
{ "en", "Start v2rayN, please wait..." },
{ "zh", "正在重启,请等待..." }
}
}
};
}
}
7 changes: 5 additions & 2 deletions v2rayN/AmazTool/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace AmazTool
using System;
using System.Threading;

namespace AmazTool
{
internal static class Program
{
Expand All @@ -10,7 +13,7 @@ private static void Main(string[] args)
{
if (args.Length == 0)
{
Console.WriteLine("Please run it from the main application.(请从主应用运行)");
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Guidelines"));
Thread.Sleep(5000);
return;
}
Expand Down
21 changes: 11 additions & 10 deletions v2rayN/AmazTool/UpgradeApp.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Threading;

namespace AmazTool
{
Expand All @@ -9,17 +12,16 @@ internal class UpgradeApp
public static void Upgrade(string fileName)
{
Console.WriteLine(fileName);
Console.WriteLine("In progress, please wait...(正在进行中,请等待)");

Thread.Sleep(9000);

if (!File.Exists(fileName))
{
Console.WriteLine("Upgrade Failed, File Not Exist(升级失败,文件不存在).");
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Upgrade_File_Not_Found"));
return;
}

Console.WriteLine("Try to end the process(尝试结束进程).");
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Try_Terminate_Process"));
try
{
var existing = Process.GetProcessesByName(V2rayN);
Expand All @@ -32,11 +34,10 @@ public static void Upgrade(string fileName)
catch (Exception ex)
{
// Access may be denied without admin right. The user may not be an administrator.
Console.WriteLine("Failed to close v2rayN(关闭v2rayN失败).\n" +
"Close it manually, or the upgrade may fail.(请手动关闭正在运行的v2rayN,否则可能升级失败。\n\n" + ex.StackTrace);
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Failed_Terminate_Process") + ex.StackTrace);
}

Console.WriteLine("Start extracting files(开始解压文件).");
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Start_Unzipping"));
StringBuilder sb = new();
try
{
Expand Down Expand Up @@ -79,16 +80,16 @@ public static void Upgrade(string fileName)
}
catch (Exception ex)
{
Console.WriteLine("Upgrade Failed(升级失败)." + ex.StackTrace);
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Failed_Upgrade") + ex.StackTrace);
//return;
}
if (sb.Length > 0)
{
Console.WriteLine("Upgrade Failed(升级失败)." + sb.ToString());
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Failed_Upgrade") + sb.ToString());
//return;
}

Console.WriteLine("Start v2rayN, please wait...(正在重启,请等待)");
Console.WriteLine(LocalizationHelper.GetLocalizedValue("Restart_v2rayN"));
Thread.Sleep(9000);
Process process = new()
{
Expand Down
5 changes: 4 additions & 1 deletion v2rayN/ServiceLib/Handler/ConfigHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,6 @@ private static bool CompareProfileItem(ProfileItem o, ProfileItem n, bool remark
&& o.Address == n.Address
&& o.Port == n.Port
&& o.Id == n.Id
&& o.AlterId == n.AlterId
&& o.Security == n.Security
&& o.Network == n.Network
&& o.HeaderType == n.HeaderType
Expand All @@ -968,6 +967,10 @@ private static bool CompareProfileItem(ProfileItem o, ProfileItem n, bool remark
&& (o.ConfigType == EConfigType.Trojan || o.StreamSecurity == n.StreamSecurity)
&& o.Flow == n.Flow
&& o.Sni == n.Sni
&& o.Alpn == n.Alpn
&& o.Fingerprint == n.Fingerprint
&& o.PublicKey == n.PublicKey
&& o.ShortId == n.ShortId
&& (!remarks || o.Remarks == n.Remarks);
}

Expand Down

0 comments on commit 2e8b666

Please sign in to comment.