-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
221 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace MultiLanguageForXAML.DB | ||
{ | ||
public class EmbeddedJsonDB : IDataBase | ||
{ | ||
private readonly string? basePath; | ||
private readonly Dictionary<string, JsonElement> dataDict = new(); | ||
|
||
public EmbeddedJsonDB() | ||
{ | ||
|
||
} | ||
|
||
public EmbeddedJsonDB(string basePath) | ||
{ | ||
this.basePath = basePath; | ||
} | ||
|
||
public string? Get(string key, string cultureName) | ||
{ | ||
if (basePath == null) | ||
return null; | ||
|
||
if (!dataDict.ContainsKey(cultureName)) | ||
{ | ||
var files = GetEmbeddedJsonFile(basePath, cultureName); | ||
//找不到匹配的,找近似的。例如 zh-CHS找不到,zh也可以 | ||
if (files.Length == 0) | ||
{ | ||
bool isSubLan = cultureName.Split('-').Length > 1; | ||
if (isSubLan) | ||
files = GetEmbeddedJsonFile(basePath, $"{cultureName.Split('-')[0]}*"); | ||
} | ||
|
||
if (files.Length == 0) | ||
return null; | ||
|
||
string json = GetEmbeddedResource(files[0]); | ||
JsonElement data = JsonSerializer.Deserialize<JsonElement>(json); | ||
dataDict.Add(cultureName, data); | ||
} | ||
|
||
string result = dataDict[cultureName].GetProperty(key).GetString()!; | ||
return result; | ||
} | ||
|
||
private static string[] GetEmbeddedJsonFile(string basePath, string cultureName) | ||
{ | ||
var files = Assembly.GetEntryAssembly()!.GetManifestResourceNames().Where(m => m.StartsWith($"{basePath}.{cultureName}")).ToArray(); | ||
return files; | ||
} | ||
|
||
public static string GetEmbeddedResource(string path) | ||
{ | ||
try | ||
{ | ||
using var reader = new StreamReader(Assembly.GetEntryAssembly()!.GetManifestResourceStream(path)!); | ||
return reader.ReadToEnd(); | ||
} | ||
catch (Exception) | ||
{ | ||
return string.Empty; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<Application | ||
x:Class="Samples.WPF.EmbeddedConfig.App" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="clr-namespace:Samples.WPF.EmbeddedConfig"> | ||
<Application.Resources /> | ||
</Application> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using MultiLanguageForXAML; | ||
using MultiLanguageForXAML.DB; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Configuration; | ||
using System.Data; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
|
||
namespace Samples.WPF.EmbeddedConfig | ||
{ | ||
/// <summary> | ||
/// Interaction logic for App.xaml | ||
/// </summary> | ||
public partial class App : Application | ||
{ | ||
protected override void OnStartup(StartupEventArgs e) | ||
{ | ||
LanService.Init(new EmbeddedJsonDB("Samples.WPF.EmbeddedConfig.Languages"), true, "en"); | ||
MainWindow mainwindow = new(); | ||
mainwindow.Show(); | ||
base.OnStartup(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Windows; | ||
|
||
[assembly: ThemeInfo( | ||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located | ||
//(used if a resource is not found in the page, | ||
// or application resource dictionaries) | ||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located | ||
//(used if a resource is not found in the page, | ||
// app, or any theme specific resource dictionaries) | ||
)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"header": "header", | ||
"txt": "one", | ||
"txt2": "two", | ||
"txt3": "three", | ||
"txtBlock_format1": "format 1 {0}", | ||
"txtBlock_format2": "format 2 {0} {1}", | ||
"txtBlock_format3": "format 3 {1} {0} {2}", | ||
"zh": "Chinese", | ||
"en": "English", | ||
"btn_readInCode": "read in code", | ||
"title": "title", | ||
"chk": "checkbox", | ||
"expander": "expander", | ||
"label": "label", | ||
"menuItem": "MenuItem", | ||
"radioBtn": "RadioBtn", | ||
"textBlock": "textblock", | ||
"red": "red", | ||
"blue": "blue" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"header": "头", | ||
"txt": "一", | ||
"txt2": "二", | ||
"txt3": "三", | ||
"txtBlock_format1": "格式化1 {0}", | ||
"txtBlock_format2": "格式化2 {0} {1}", | ||
"txtBlock_format3": "格式化3 {1} { 100 } {0} aaa {2} bb", | ||
"txtBlock_only": "只有中文有", | ||
"btn_readInCode": "从代码中读取", | ||
"zh": "中文", | ||
"en": "英语", | ||
"title": "标题", | ||
"chk": "复选框", | ||
"expander": "展开", | ||
"label": "标签", | ||
"menuItem": "菜单项", | ||
"radioBtn": "单选框", | ||
"textBlock": "文本", | ||
"red": "红色", | ||
"blue": "蓝色" | ||
} |
30 changes: 30 additions & 0 deletions
30
Samples.WPF.EmbeddedConfig/Samples.WPF.EmbeddedConfig.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>WinExe</OutputType> | ||
<TargetFramework>net6.0-windows</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<UseWPF>true</UseWPF> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\Samples.WPF\MainWindow.xaml.cs" Link="MainWindow.xaml.cs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Include="Languages\*.json"> | ||
<CopyToOutputDirectory>Never</CopyToOutputDirectory> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Page Include="..\Samples.WPF\MainWindow.xaml" Link="MainWindow.xaml"> | ||
<Generator>MSBuild:Compile</Generator> | ||
</Page> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\MultiLanguageForXAML.WPF\MultiLanguageForXAML.WPF.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters