Commit c1f8800f authored by 潘栩锋's avatar 潘栩锋 🚴

添加 Misc添加Culture ,支持lang文件夹内的多国语言包。

parent 9eca2e65
using System; using System;
using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Windows; using System.Windows;
namespace FLY.Thick.Base.UI
namespace Misc
{ {
public class Culture : INotifyPropertyChanged public class Culture : INotifyPropertyChanged
{ {
...@@ -12,60 +14,126 @@ namespace FLY.Thick.Base.UI ...@@ -12,60 +14,126 @@ namespace FLY.Thick.Base.UI
public string Language { get; set; } = "zh"; public string Language { get; set; } = "zh";
public string filePath = "culture.json"; public string filePath = "culture.json";
string fileFullPath = "culture.json"; string dirPath;
public List<string> langs { get; } = new List<string>();
static Dictionary<string, string> langInfos;
static Culture()
{
langInfos = new Dictionary<string, string>
{
{ "ch", "中文" },
{ "en", "English" },
{ "ru", "русский язык" },
{ "fr", "français" },
{ "de", "dé yǔ" },
{ "ja", "日本語" },
{ "ko", "한국어" }
};
}
public static string GetLangInfo(string lang)
{
if (langInfos.ContainsKey(lang))
{
return langInfos[lang];
}
else
{
return lang;
}
}
public static LanguageInfo GetLanguageInfo(string lang)
{
return new LanguageInfo() { Info = Culture.GetLangInfo(lang), Language = lang };
}
public bool Save() public bool Save()
{ {
string fileFullPath = Path.Combine(dirPath, filePath);
return CultureJsonDb.Save(fileFullPath, this); return CultureJsonDb.Save(fileFullPath, this);
} }
public bool Load() public bool Load()
{ {
//记录加载时的路径。 就算用 System.Environment.CurrentDirectory = rootDir; 改变后, 也能保存回加载时的位置 //记录加载时的路径。 就算用 System.Environment.CurrentDirectory = rootDir; 改变后, 也能保存回加载时的位置
string path = System.IO.Directory.GetCurrentDirectory(); dirPath = System.IO.Directory.GetCurrentDirectory();
fileFullPath = Path.Combine(path, filePath); string fileFullPath = Path.Combine(dirPath, filePath);
return CultureJsonDb.Load(fileFullPath, this); return CultureJsonDb.Load(fileFullPath, this);
} }
void GetOtherLang()
{
langs.Clear();
langs.Add("ch");
langs.Add("en");
//添加其它语言包
string langPath = Path.Combine(dirPath, "lang");
if (!Directory.Exists(langPath))
return;
DirectoryInfo directory = new DirectoryInfo(langPath);
var dirs = directory.GetDirectories();
foreach (var dir in dirs)
{
string xamlFilePath = Path.Combine(dir.FullName, "StringResource.xaml");
if (File.Exists(xamlFilePath))
{
langs.Add(dir.Name);
}
}
}
public void LoadCulture(Type appType) public void LoadCulture(Type appType)
{ {
Assembly asm = Assembly.GetAssembly(appType); Assembly asm = Assembly.GetAssembly(appType);
string assemblyName = asm.GetName().Name; string assemblyName = asm.GetName().Name;
GetOtherLang();
string language = Language; string language = Language;
if (language == "zh") if (language == "zh")
return; return;
//List<ResourceDictionary> dictionaryList = new List<ResourceDictionary>(); //程序集的,只支持 zh,en. 其它都是 lang 文件夹 内的
//foreach (ResourceDictionary dictionary in Application.Current.Resources.MergedDictionaries) ResourceDictionary resourceDictionary = null;
//{ if (language == "en")
// dictionaryList.Add(dictionary); {
//} string uriString = $@"pack://application:,,,/{assemblyName};component/Themes/StringResources/{language}/StringResource.xaml";
string requestedCulture = $@"pack://application:,,,/{assemblyName};component/Themes/StringResources/{language}/StringResource.xaml"; try
{
ResourceDictionary resourceDictionary; resourceDictionary = new ResourceDictionary()
{
Source = new Uri(uriString)
};
}
catch
{
return;
}
}
else
{
string uriString = Path.Combine(dirPath, "lang", language, "StringResource.xaml");
try try
{ {
resourceDictionary = new ResourceDictionary() { Source = new Uri(requestedCulture) }; using (FileStream fs = new FileStream(uriString, FileMode.Open, FileAccess.Read))
{
resourceDictionary = (ResourceDictionary)System.Windows.Markup.XamlReader.Load(fs);
}
} }
catch catch
{ {
return; return;
} }
}
//if(resourceDictionary == null)
//{
// requestedCulture = @"Resources\StringResource.xaml";
// resourceDictionary = dictionaryList.FirstOrDefault(d =< d.Source.OriginalString.Equals(requestedCulture));
//}
if (resourceDictionary != null) if (resourceDictionary != null)
{ {
//Application.Current.Resources.MergedDictionaries.Remove(resourceDictionary);
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary); Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
} }
} }
...@@ -116,4 +184,12 @@ namespace FLY.Thick.Base.UI ...@@ -116,4 +184,12 @@ namespace FLY.Thick.Base.UI
} }
public string Language { get; set; } = "zh"; public string Language { get; set; } = "zh";
} }
public class LanguageInfo : INotifyPropertyChanged
{
public string Language { get; set; }
public string Info { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
} }
...@@ -64,6 +64,7 @@ ...@@ -64,6 +64,7 @@
</StartupObject> </StartupObject>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="PresentationFramework" />
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core"> <Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework> <RequiredTargetFramework>3.5</RequiredTargetFramework>
...@@ -71,6 +72,7 @@ ...@@ -71,6 +72,7 @@
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" /> <Reference Include="System.Xml.Linq" />
<Reference Include="WindowsBase" /> <Reference Include="WindowsBase" />
...@@ -80,6 +82,7 @@ ...@@ -80,6 +82,7 @@
<Compile Include="common.cs" /> <Compile Include="common.cs" />
<Compile Include="Conventer.cs" /> <Compile Include="Conventer.cs" />
<Compile Include="CRC.cs" /> <Compile Include="CRC.cs" />
<Compile Include="Culture.cs" />
<Compile Include="DATARANGE.cs" /> <Compile Include="DATARANGE.cs" />
<Compile Include="Enumerable.cs" /> <Compile Include="Enumerable.cs" />
<Compile Include="IgnoreAttribute.cs" /> <Compile Include="IgnoreAttribute.cs" />
......
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using FLY.AppHelper; using FLY.AppHelper;
using Misc; using Misc;
using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel; using System.ComponentModel;
using System.Linq; using System.Linq;
...@@ -39,17 +40,24 @@ namespace FLY.Thick.Base.UI.CustomSection ...@@ -39,17 +40,24 @@ namespace FLY.Thick.Base.UI.CustomSection
public RelayCommand OkCmd { get; private set; } public RelayCommand OkCmd { get; private set; }
Culture culture; Culture culture;
public UcSectionLanguageVm() public UcSectionLanguageVm()
{ {
OkCmd = new RelayCommand(Ok); OkCmd = new RelayCommand(Ok);
LanguageList.Add(new LanguageInfo() { Info = "中文", Language = "ch" });
LanguageList.Add(new LanguageInfo() { Info = "English", Language = "en" });
} }
public void Init(Culture culture) public void Init(Culture culture)
{ {
this.culture = culture; this.culture = culture;
foreach (var lang in culture.langs) {
LanguageList.Add(Culture.GetLanguageInfo(lang));
}
try try
{ {
CurrentLanguage = LanguageList.First(l => l.Language == culture.Language); CurrentLanguage = LanguageList.First(l => l.Language == culture.Language);
...@@ -59,6 +67,7 @@ namespace FLY.Thick.Base.UI.CustomSection ...@@ -59,6 +67,7 @@ namespace FLY.Thick.Base.UI.CustomSection
CurrentLanguage = LanguageList[0]; CurrentLanguage = LanguageList[0];
} }
} }
private void Ok() private void Ok()
{ {
string msg = (string)Application.Current.FindResource("str.PgCustomSections.IsSureToReboot"); string msg = (string)Application.Current.FindResource("str.PgCustomSections.IsSureToReboot");
...@@ -77,12 +86,4 @@ namespace FLY.Thick.Base.UI.CustomSection ...@@ -77,12 +86,4 @@ namespace FLY.Thick.Base.UI.CustomSection
AppJustOne.Restart(); AppJustOne.Restart();
} }
} }
public class LanguageInfo : INotifyPropertyChanged
{
public string Language { get; set; }
public string Info { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
} }
...@@ -96,7 +96,6 @@ ...@@ -96,7 +96,6 @@
<Compile Include="CtMicroGage.xaml.cs"> <Compile Include="CtMicroGage.xaml.cs">
<DependentUpon>CtMicroGage.xaml</DependentUpon> <DependentUpon>CtMicroGage.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Culture.cs" />
<Compile Include="CustomMenu\AdrMenu.cs" /> <Compile Include="CustomMenu\AdrMenu.cs" />
<Compile Include="CustomMenu\CustomMenu.cs" /> <Compile Include="CustomMenu\CustomMenu.cs" />
<Compile Include="CustomMenu\UpdateMenu.cs" /> <Compile Include="CustomMenu\UpdateMenu.cs" />
......
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
<system:String x:Key="str.PgBorderSearch.ForwBorder">F.Border</system:String> <system:String x:Key="str.PgBorderSearch.ForwBorder">F.Border</system:String>
<system:String x:Key="str.PgBorderSearch.BackwBorder">B.Border</system:String> <system:String x:Key="str.PgBorderSearch.BackwBorder">B.Border</system:String>
<system:String x:Key="str.PgBorderSearch.Width">Width</system:String> <system:String x:Key="str.PgBorderSearch.Width">Width</system:String>
<system:String x:Key="str.PgBorderSearch.ExpWidth">Exp. Width</system:String>
<system:String x:Key="str.PgBorderSearch.XRange">X Range</system:String> <system:String x:Key="str.PgBorderSearch.XRange">X Range</system:String>
<system:String x:Key="str.PgBorderSearch.YRange">Y Range</system:String> <system:String x:Key="str.PgBorderSearch.YRange">Y Range</system:String>
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
<system:String x:Key="str.PgCustomSections.XStep" >X-axis Step</system:String> <system:String x:Key="str.PgCustomSections.XStep" >X-axis Step</system:String>
<system:String x:Key="str.PgCustomSections.SelectMusicFile" >Select Music File</system:String> <system:String x:Key="str.PgCustomSections.SelectMusicFile" >Select Music File</system:String>
<system:String x:Key="str.PgCustomSections.IsSureToReboot">The App must be rebooted to execute new parameters. Do you want to reboot it?</system:String>
<system:String x:Key="str.PgCustomSections.Language">Language</system:String> <system:String x:Key="str.PgCustomSections.Language">Language</system:String>
<system:String x:Key="str.PgCustomSections.Apply">Apply</system:String> <system:String x:Key="str.PgCustomSections.Apply">Apply</system:String>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment