优化语言设置

This commit is contained in:
蓝点lilac
2020-12-30 22:51:09 +08:00
parent 38a10ddb9d
commit 5f3b903e89
2 changed files with 22 additions and 32 deletions

View File

@@ -74,32 +74,24 @@ namespace ContextMenuManager
}
}
public static string LanguageIniPath
public static string LanguageIniPath => $@"{LangsDir}\{Language}.ini";
public static string Language
{
get
{
string language = ConfigWriter.GetValue("General", "Language");
DirectoryInfo di = new DirectoryInfo(LangsDir);
if(language == string.Empty && di.Exists)
if(language == string.Empty)
{
string sysLanguageName = new CultureInfo(GetUserDefaultUILanguage()).Name;
foreach(FileInfo fi in di.GetFiles())
{
string name = Path.GetFileNameWithoutExtension(fi.Name);
//如果为空,则赋值为系统显示语言文件名文件(存在时)
if(name.Equals(sysLanguageName, StringComparison.OrdinalIgnoreCase))
{
language = fi.FullName; break;
}
}
language = new CultureInfo(GetUserDefaultUILanguage()).Name;
}
if(!File.Exists($@"{LangsDir}\{language}.ini"))
{
language = string.Empty;
}
else if(!File.Exists(language)) language = string.Empty;
return language;
}
set
{
ConfigWriter.SetValue("General", "Language", value);
}
set => ConfigWriter.SetValue("General", "Language", value);
}
public static bool AutoBackup

View File

@@ -213,7 +213,7 @@ namespace ContextMenuManager.Controls
readonly PictureButton btnOpenDir = new PictureButton(AppImage.Open);
readonly List<string> iniPaths = new List<string>();
readonly List<string> languages = new List<string>();
protected override void OnResize(EventArgs e)
{
@@ -234,15 +234,13 @@ namespace ContextMenuManager.Controls
cmbLanguages.Items.Clear();
cmbLanguages.Items.Add("(default) 简体中文");
string str = AppString.Other.Translators + Environment.NewLine + new string('-', 74);
DirectoryInfo di = new DirectoryInfo(AppConfig.LangsDir);
if(di.Exists)
if(Directory.Exists(AppConfig.LangsDir))
{
iniPaths.Clear();
FileInfo[] fis = di.GetFiles();
foreach(FileInfo fi in fis)
languages.Clear();
foreach(string fileName in Directory.GetFiles(AppConfig.LangsDir,"*.ini"))
{
iniPaths.Add(fi.FullName);
IniReader reader = new IniReader(fi.FullName);
languages.Add(Path.GetFileNameWithoutExtension(fileName));
IniReader reader = new IniReader(fileName);
string language = reader.GetValue("General", "Language");
string translator = reader.GetValue("General", "Translator");
str += Environment.NewLine + language + new string('\t', 5) + translator;
@@ -255,27 +253,27 @@ namespace ContextMenuManager.Controls
private void ChangeLanguage()
{
string path = "default";
string language = "default";
int index = GetSelectIndex();
if(cmbLanguages.SelectedIndex == index) return;
if(cmbLanguages.SelectedIndex > 0) path = iniPaths[cmbLanguages.SelectedIndex - 1];
if(cmbLanguages.SelectedIndex > 0) language = languages[cmbLanguages.SelectedIndex - 1];
if(MessageBoxEx.Show(AppString.MessageBox.RestartApp, MessageBoxButtons.OKCancel) != DialogResult.OK)
{
cmbLanguages.SelectedIndex = index;
}
else
{
AppConfig.LanguageIniPath = path;
AppConfig.Language = language;
Application.Restart();
}
}
private int GetSelectIndex()
{
string path = AppConfig.LanguageIniPath;
for(int i = 0; i < iniPaths.Count; i++)
string language = AppConfig.Language;
for(int i = 0; i < languages.Count; i++)
{
if(iniPaths[i].Equals(path, StringComparison.OrdinalIgnoreCase)) return i + 1;
if(languages[i].Equals(language, StringComparison.OrdinalIgnoreCase)) return i + 1;
}
return 0;
}