Skip to content

Commit 65854c3

Browse files
committed
2.1.8
支持读取嵌入资源
1 parent 8929e7d commit 65854c3

15 files changed

+221
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using System.Text.Json;
8+
using System.Threading.Tasks;
9+
10+
namespace MultiLanguageForXAML.DB
11+
{
12+
public class EmbeddedJsonDB : IDataBase
13+
{
14+
private readonly string? basePath;
15+
private readonly Dictionary<string, JsonElement> dataDict = new();
16+
17+
public EmbeddedJsonDB()
18+
{
19+
20+
}
21+
22+
public EmbeddedJsonDB(string basePath)
23+
{
24+
this.basePath = basePath;
25+
}
26+
27+
public string? Get(string key, string cultureName)
28+
{
29+
if (basePath == null)
30+
return null;
31+
32+
if (!dataDict.ContainsKey(cultureName))
33+
{
34+
var files = GetEmbeddedJsonFile(basePath, cultureName);
35+
//找不到匹配的,找近似的。例如 zh-CHS找不到,zh也可以
36+
if (files.Length == 0)
37+
{
38+
bool isSubLan = cultureName.Split('-').Length > 1;
39+
if (isSubLan)
40+
files = GetEmbeddedJsonFile(basePath, $"{cultureName.Split('-')[0]}*");
41+
}
42+
43+
if (files.Length == 0)
44+
return null;
45+
46+
string json = GetEmbeddedResource(files[0]);
47+
JsonElement data = JsonSerializer.Deserialize<JsonElement>(json);
48+
dataDict.Add(cultureName, data);
49+
}
50+
51+
string result = dataDict[cultureName].GetProperty(key).GetString()!;
52+
return result;
53+
}
54+
55+
private static string[] GetEmbeddedJsonFile(string basePath, string cultureName)
56+
{
57+
var files = Assembly.GetEntryAssembly()!.GetManifestResourceNames().Where(m => m.StartsWith($"{basePath}.{cultureName}")).ToArray();
58+
return files;
59+
}
60+
61+
public static string GetEmbeddedResource(string path)
62+
{
63+
try
64+
{
65+
using var reader = new StreamReader(Assembly.GetEntryAssembly()!.GetManifestResourceStream(path)!);
66+
return reader.ReadToEnd();
67+
}
68+
catch (Exception)
69+
{
70+
return string.Empty;
71+
}
72+
}
73+
}
74+
}

MultiLanguageForXAML.WPF/JsonDB.cs renamed to MultiLanguageForXAML.WPF/DB/JsonFileDB.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
using System.IO;
33
using System.Text.Json;
44

5-
namespace MultiLanguageForXAML
5+
namespace MultiLanguageForXAML.DB
66
{
7-
public class JsonDB : IDataBase
7+
public class JsonFileDB : IDataBase
88
{
99
private readonly string? jsonDir;
1010
private readonly Dictionary<string, JsonElement> dataDict = new();
1111

12-
public JsonDB()
12+
public JsonFileDB()
1313
{
1414

1515
}
1616

17-
public JsonDB(string jsonDir)
17+
public JsonFileDB(string jsonDir)
1818
{
1919
this.jsonDir = jsonDir;
2020
}

MultiLanguageForXAML.WPF/MultiLanguageForXAML.WPF.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0-windows</TargetFramework>
55
<UseWPF>true</UseWPF>
6-
<Version>2.1.7</Version>
6+
<Version>2.1.8</Version>
77
<AssemblyName>MultiLanguageForXAML</AssemblyName>
88
<RootNamespace>MultiLanguageForXAML</RootNamespace>
99
<Platforms>AnyCPU</Platforms>

MultiLanguageForXAML.sln

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{7A6FD990-7
1515
README.md = README.md
1616
EndProjectSection
1717
EndProject
18+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.WPF.EmbeddedConfig", "Samples.WPF.EmbeddedConfig\Samples.WPF.EmbeddedConfig.csproj", "{FC2750FC-B972-46D6-AE0D-4BDC51342E64}"
19+
EndProject
1820
Global
1921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2022
Debug|Any CPU = Debug|Any CPU
@@ -49,12 +51,25 @@ Global
4951
{B6B604AF-CAC2-42D8-AD50-A464BE32CEB0}.Release|x64.Build.0 = Release|Any CPU
5052
{B6B604AF-CAC2-42D8-AD50-A464BE32CEB0}.Release|x86.ActiveCfg = Release|Any CPU
5153
{B6B604AF-CAC2-42D8-AD50-A464BE32CEB0}.Release|x86.Build.0 = Release|Any CPU
54+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
55+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|Any CPU.Build.0 = Debug|Any CPU
56+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x64.ActiveCfg = Debug|Any CPU
57+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x64.Build.0 = Debug|Any CPU
58+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x86.ActiveCfg = Debug|Any CPU
59+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x86.Build.0 = Debug|Any CPU
60+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x64.ActiveCfg = Release|Any CPU
63+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x64.Build.0 = Release|Any CPU
64+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x86.ActiveCfg = Release|Any CPU
65+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x86.Build.0 = Release|Any CPU
5266
EndGlobalSection
5367
GlobalSection(SolutionProperties) = preSolution
5468
HideSolutionNode = FALSE
5569
EndGlobalSection
5670
GlobalSection(NestedProjects) = preSolution
5771
{B4892761-5C12-4999-9F4E-A84BF5D1D3C4} = {69706E80-5E69-4247-B54E-76E03BFD13B6}
72+
{FC2750FC-B972-46D6-AE0D-4BDC51342E64} = {69706E80-5E69-4247-B54E-76E03BFD13B6}
5873
EndGlobalSection
5974
GlobalSection(ExtensibilityGlobals) = postSolution
6075
SolutionGuid = {11CDA223-EA62-4C23-B769-05179E3A6CD6}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
//怀疑用Environment.CurrentDirectory开机启动时目录会出错,待验证
3939
string appDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
4040
string path = Path.Combine(appDir, "Res\\Languages");
41-
LanService.Init(new JsonDB(path), true,"zh");
41+
LanService.Init(new JsonFileDB(path), true,"zh");
4242
```
4343

4444
- **XAML**

Samples.WPF.EmbeddedConfig/App.xaml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<Application
2+
x:Class="Samples.WPF.EmbeddedConfig.App"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:local="clr-namespace:Samples.WPF.EmbeddedConfig">
6+
<Application.Resources />
7+
</Application>
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using MultiLanguageForXAML;
2+
using MultiLanguageForXAML.DB;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Configuration;
6+
using System.Data;
7+
using System.Linq;
8+
using System.Threading.Tasks;
9+
using System.Windows;
10+
11+
namespace Samples.WPF.EmbeddedConfig
12+
{
13+
/// <summary>
14+
/// Interaction logic for App.xaml
15+
/// </summary>
16+
public partial class App : Application
17+
{
18+
protected override void OnStartup(StartupEventArgs e)
19+
{
20+
LanService.Init(new EmbeddedJsonDB("Samples.WPF.EmbeddedConfig.Languages"), true, "en");
21+
MainWindow mainwindow = new();
22+
mainwindow.Show();
23+
base.OnStartup(e);
24+
}
25+
}
26+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Windows;
2+
3+
[assembly: ThemeInfo(
4+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
5+
//(used if a resource is not found in the page,
6+
// or application resource dictionaries)
7+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
8+
//(used if a resource is not found in the page,
9+
// app, or any theme specific resource dictionaries)
10+
)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"header": "header",
3+
"txt": "one",
4+
"txt2": "two",
5+
"txt3": "three",
6+
"txtBlock_format1": "format 1 {0}",
7+
"txtBlock_format2": "format 2 {0} {1}",
8+
"txtBlock_format3": "format 3 {1} {0} {2}",
9+
"zh": "Chinese",
10+
"en": "English",
11+
"btn_readInCode": "read in code",
12+
"title": "title",
13+
"chk": "checkbox",
14+
"expander": "expander",
15+
"label": "label",
16+
"menuItem": "MenuItem",
17+
"radioBtn": "RadioBtn",
18+
"textBlock": "textblock",
19+
"red": "red",
20+
"blue": "blue"
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"header": "",
3+
"txt": "",
4+
"txt2": "",
5+
"txt3": "",
6+
"txtBlock_format1": "格式化1 {0}",
7+
"txtBlock_format2": "格式化2 {0} {1}",
8+
"txtBlock_format3": "格式化3 {1} { 100 } {0} aaa {2} bb",
9+
"txtBlock_only": "只有中文有",
10+
"btn_readInCode": "从代码中读取",
11+
"zh": "中文",
12+
"en": "英语",
13+
"title": "标题",
14+
"chk": "复选框",
15+
"expander": "展开",
16+
"label": "标签",
17+
"menuItem": "菜单项",
18+
"radioBtn": "单选框",
19+
"textBlock": "文本",
20+
"red": "红色",
21+
"blue": "蓝色"
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>net6.0-windows</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<UseWPF>true</UseWPF>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<Compile Include="..\Samples.WPF\MainWindow.xaml.cs" Link="MainWindow.xaml.cs" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<EmbeddedResource Include="Languages\*.json">
16+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
17+
</EmbeddedResource>
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<Page Include="..\Samples.WPF\MainWindow.xaml" Link="MainWindow.xaml">
22+
<Generator>MSBuild:Compile</Generator>
23+
</Page>
24+
</ItemGroup>
25+
26+
<ItemGroup>
27+
<ProjectReference Include="..\MultiLanguageForXAML.WPF\MultiLanguageForXAML.WPF.csproj" />
28+
</ItemGroup>
29+
30+
</Project>

Samples.WPF/App.xaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
x:Class="Samples.WPF.App"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5-
xmlns:local="clr-namespace:Samples.WPF"
6-
StartupUri="MainWindow.xaml">
5+
xmlns:local="clr-namespace:Samples.WPF">
76
<Application.Resources />
87
</Application>

Samples.WPF/App.xaml.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MultiLanguageForXAML;
2+
using MultiLanguageForXAML.DB;
23
using System;
34
using System.Collections.Generic;
45
using System.Configuration;
@@ -15,9 +16,13 @@ namespace Samples.WPF
1516
/// </summary>
1617
public partial class App : Application
1718
{
18-
public App()
19+
protected override void OnStartup(StartupEventArgs e)
1920
{
20-
21+
string path = System.IO.Path.Combine(Environment.CurrentDirectory, "Languages");
22+
LanService.Init(new JsonFileDB(path), true, "en");
23+
MainWindow mainwindow = new();
24+
mainwindow.Show();
25+
base.OnStartup(e);
2126
}
2227
}
2328
}

Samples.WPF/MainWindow.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<TabItem lan:Xaml.Key="txt">
1515
<StackPanel x:Name="stcPanel">
1616
<Button lan:Xaml.Key="btn_readInCode" Click="BtnReadInCode_Click" />
17-
<ComboBox x:Name="CB" SelectedIndex="0">
17+
<ComboBox x:Name="CB" SelectedIndex="1">
1818
<ComboBoxItem lan:Xaml.Key="zh" Tag="zh" />
1919
<ComboBoxItem lan:Xaml.Key="en" Tag="en" />
2020
</ComboBox>

Samples.WPF/MainWindow.xaml.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MultiLanguageForXAML;
2+
using MultiLanguageForXAML.DB;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -24,8 +25,6 @@ public partial class MainWindow : Window
2425
{
2526
public MainWindow()
2627
{
27-
string path = System.IO.Path.Combine(Environment.CurrentDirectory, "Languages");
28-
LanService.Init(new JsonDB(path), true,"en");
2928
InitializeComponent();
3029
CB.SelectionChanged += CB_SelectionChanged;
3130
}

0 commit comments

Comments
 (0)