Skip to content

Commit

Permalink
2.1.8
Browse files Browse the repository at this point in the history
支持读取嵌入资源
  • Loading branch information
GiantappMan committed Jun 28, 2022
1 parent 8929e7d commit 65854c3
Show file tree
Hide file tree
Showing 15 changed files with 221 additions and 13 deletions.
74 changes: 74 additions & 0 deletions MultiLanguageForXAML.WPF/DB/EmbeddedJsonDB.cs
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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
using System.IO;
using System.Text.Json;

namespace MultiLanguageForXAML
namespace MultiLanguageForXAML.DB
{
public class JsonDB : IDataBase
public class JsonFileDB : IDataBase
{
private readonly string? jsonDir;
private readonly Dictionary<string, JsonElement> dataDict = new();

public JsonDB()
public JsonFileDB()
{

}

public JsonDB(string jsonDir)
public JsonFileDB(string jsonDir)
{
this.jsonDir = jsonDir;
}
Expand Down
2 changes: 1 addition & 1 deletion MultiLanguageForXAML.WPF/MultiLanguageForXAML.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWPF>true</UseWPF>
<Version>2.1.7</Version>
<Version>2.1.8</Version>
<AssemblyName>MultiLanguageForXAML</AssemblyName>
<RootNamespace>MultiLanguageForXAML</RootNamespace>
<Platforms>AnyCPU</Platforms>
Expand Down
15 changes: 15 additions & 0 deletions MultiLanguageForXAML.sln
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{7A6FD990-7
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Samples.WPF.EmbeddedConfig", "Samples.WPF.EmbeddedConfig\Samples.WPF.EmbeddedConfig.csproj", "{FC2750FC-B972-46D6-AE0D-4BDC51342E64}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -49,12 +51,25 @@ Global
{B6B604AF-CAC2-42D8-AD50-A464BE32CEB0}.Release|x64.Build.0 = Release|Any CPU
{B6B604AF-CAC2-42D8-AD50-A464BE32CEB0}.Release|x86.ActiveCfg = Release|Any CPU
{B6B604AF-CAC2-42D8-AD50-A464BE32CEB0}.Release|x86.Build.0 = Release|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x64.ActiveCfg = Debug|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x64.Build.0 = Debug|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x86.ActiveCfg = Debug|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Debug|x86.Build.0 = Debug|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|Any CPU.Build.0 = Release|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x64.ActiveCfg = Release|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x64.Build.0 = Release|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x86.ActiveCfg = Release|Any CPU
{FC2750FC-B972-46D6-AE0D-4BDC51342E64}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B4892761-5C12-4999-9F4E-A84BF5D1D3C4} = {69706E80-5E69-4247-B54E-76E03BFD13B6}
{FC2750FC-B972-46D6-AE0D-4BDC51342E64} = {69706E80-5E69-4247-B54E-76E03BFD13B6}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {11CDA223-EA62-4C23-B769-05179E3A6CD6}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
//怀疑用Environment.CurrentDirectory开机启动时目录会出错,待验证
string appDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string path = Path.Combine(appDir, "Res\\Languages");
LanService.Init(new JsonDB(path), true,"zh");
LanService.Init(new JsonFileDB(path), true,"zh");
```

- **XAML**
Expand Down
7 changes: 7 additions & 0 deletions Samples.WPF.EmbeddedConfig/App.xaml
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>
26 changes: 26 additions & 0 deletions Samples.WPF.EmbeddedConfig/App.xaml.cs
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);
}
}
}
10 changes: 10 additions & 0 deletions Samples.WPF.EmbeddedConfig/AssemblyInfo.cs
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)
)]
21 changes: 21 additions & 0 deletions Samples.WPF.EmbeddedConfig/Languages/en.json
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"
}
22 changes: 22 additions & 0 deletions Samples.WPF.EmbeddedConfig/Languages/zh.json
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 Samples.WPF.EmbeddedConfig/Samples.WPF.EmbeddedConfig.csproj
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>
3 changes: 1 addition & 2 deletions Samples.WPF/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
x:Class="Samples.WPF.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"
StartupUri="MainWindow.xaml">
xmlns:local="clr-namespace:Samples.WPF">
<Application.Resources />
</Application>
9 changes: 7 additions & 2 deletions Samples.WPF/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MultiLanguageForXAML;
using MultiLanguageForXAML.DB;
using System;
using System.Collections.Generic;
using System.Configuration;
Expand All @@ -15,9 +16,13 @@ namespace Samples.WPF
/// </summary>
public partial class App : Application
{
public App()
protected override void OnStartup(StartupEventArgs e)
{

string path = System.IO.Path.Combine(Environment.CurrentDirectory, "Languages");
LanService.Init(new JsonFileDB(path), true, "en");
MainWindow mainwindow = new();
mainwindow.Show();
base.OnStartup(e);
}
}
}
2 changes: 1 addition & 1 deletion Samples.WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<TabItem lan:Xaml.Key="txt">
<StackPanel x:Name="stcPanel">
<Button lan:Xaml.Key="btn_readInCode" Click="BtnReadInCode_Click" />
<ComboBox x:Name="CB" SelectedIndex="0">
<ComboBox x:Name="CB" SelectedIndex="1">
<ComboBoxItem lan:Xaml.Key="zh" Tag="zh" />
<ComboBoxItem lan:Xaml.Key="en" Tag="en" />
</ComboBox>
Expand Down
3 changes: 1 addition & 2 deletions Samples.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MultiLanguageForXAML;
using MultiLanguageForXAML.DB;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -24,8 +25,6 @@ public partial class MainWindow : Window
{
public MainWindow()
{
string path = System.IO.Path.Combine(Environment.CurrentDirectory, "Languages");
LanService.Init(new JsonDB(path), true,"en");
InitializeComponent();
CB.SelectionChanged += CB_SelectionChanged;
}
Expand Down

0 comments on commit 65854c3

Please sign in to comment.