Skip to content

Commit 65398a0

Browse files
committed
2.2发布 同时支持4.6和net6
1 parent 5d305c8 commit 65398a0

File tree

4 files changed

+134
-6
lines changed

4 files changed

+134
-6
lines changed

MultiLanguageForXAML.WPF/DB/EmbeddedJsonDB.cs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,79 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Text;
7-
using System.Text.Json;
87
using System.Threading.Tasks;
98

109
namespace MultiLanguageForXAML.DB
1110
{
11+
#if NET46
12+
using Newtonsoft.Json;
13+
using Newtonsoft.Json.Linq;
14+
public class EmbeddedJsonDB : IDataBase
15+
{
16+
private readonly string? basePath;
17+
private readonly Dictionary<string, JObject?> dataDict = new();
18+
19+
public EmbeddedJsonDB()
20+
{
21+
22+
}
23+
24+
public EmbeddedJsonDB(string basePath)
25+
{
26+
this.basePath = basePath;
27+
}
28+
29+
public string? Get(string key, string cultureName)
30+
{
31+
if (basePath == null)
32+
return null;
33+
34+
if (!dataDict.ContainsKey(cultureName))
35+
{
36+
var files = GetEmbeddedJsonFile(basePath, cultureName);
37+
//找不到匹配的,找近似的。例如 zh-CHS找不到,zh也可以
38+
if (files.Length == 0)
39+
{
40+
bool isSubLan = cultureName.Split('-').Length > 1;
41+
if (isSubLan)
42+
files = GetEmbeddedJsonFile(basePath, $"{cultureName.Split('-')[0]}*");
43+
}
44+
45+
if (files.Length == 0)
46+
return null;
47+
48+
string json = GetEmbeddedResource(files[0]);
49+
var data = JsonConvert.DeserializeObject<JObject>(json);
50+
dataDict.Add(cultureName, data);
51+
}
52+
53+
string result = dataDict[cultureName]?[key]?.ToString() ?? string.Empty;
54+
return result;
55+
}
56+
57+
private static string[] GetEmbeddedJsonFile(string basePath, string cultureName)
58+
{
59+
var files = Assembly.GetEntryAssembly()!.GetManifestResourceNames().Where(m => m.StartsWith($"{basePath}.{cultureName}")).ToArray();
60+
return files;
61+
}
62+
63+
public static string GetEmbeddedResource(string path)
64+
{
65+
try
66+
{
67+
using var reader = new StreamReader(Assembly.GetEntryAssembly()!.GetManifestResourceStream(path)!);
68+
return reader.ReadToEnd();
69+
}
70+
catch (Exception)
71+
{
72+
return string.Empty;
73+
}
74+
}
75+
}
76+
#endif
77+
78+
#if NETCOREAPP
79+
using System.Text.Json;
1280
public class EmbeddedJsonDB : IDataBase
1381
{
1482
private readonly string? basePath;
@@ -71,4 +139,5 @@ public static string GetEmbeddedResource(string path)
71139
}
72140
}
73141
}
142+
#endif
74143
}

MultiLanguageForXAML.WPF/DB/JsonFileDB.cs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,58 @@
11
using System.Collections.Generic;
22
using System.IO;
3-
using System.Text.Json;
43

54
namespace MultiLanguageForXAML.DB
65
{
6+
#if NET46
7+
using Newtonsoft.Json;
8+
using Newtonsoft.Json.Linq;
9+
public class JsonFileDB : IDataBase
10+
{
11+
private readonly string? jsonDir;
12+
private readonly Dictionary<string, JObject?> dataDict = new();
13+
14+
public JsonFileDB()
15+
{
16+
17+
}
18+
19+
public JsonFileDB(string jsonDir)
20+
{
21+
this.jsonDir = jsonDir;
22+
}
23+
24+
public string? Get(string key, string cultureName)
25+
{
26+
if (jsonDir == null)
27+
return null;
28+
29+
if (!dataDict.ContainsKey(cultureName))
30+
{
31+
var files = Directory.GetFiles(jsonDir, $"{cultureName}.json");
32+
//找不到匹配的,找近似的。例如 zh-CHS找不到,zh也可以
33+
if (files.Length == 0)
34+
{
35+
bool isSubLan = cultureName.Split('-').Length > 1;
36+
if (isSubLan)
37+
files = Directory.GetFiles(jsonDir, $"{cultureName.Split('-')[0]}*");
38+
}
39+
40+
if (files.Length == 0)
41+
return null;
42+
43+
string json = File.ReadAllText(files[0]);
44+
var data = JsonConvert.DeserializeObject<JObject>(json);
45+
dataDict.Add(cultureName, data);
46+
}
47+
48+
string? result = dataDict[cultureName]?[key]?.ToString();
49+
return result;
50+
}
51+
}
52+
#endif
53+
54+
#if NETCOREAPP
55+
using System.Text.Json;
756
public class JsonFileDB : IDataBase
857
{
958
private readonly string? jsonDir;
@@ -47,4 +96,5 @@ public JsonFileDB(string jsonDir)
4796
return result;
4897
}
4998
}
99+
#endif
50100
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
2-
32
<PropertyGroup>
4-
<TargetFramework>net6.0-windows</TargetFramework>
3+
<TargetFrameworks>net46;net6.0-windows</TargetFrameworks>
4+
<LangVersion>10.0</LangVersion>
5+
<Nullable>enable</Nullable>
56
<UseWPF>true</UseWPF>
6-
<Version>2.1.8</Version>
7+
<Version>2.2.0</Version>
78
<AssemblyName>MultiLanguageForXAML</AssemblyName>
89
<RootNamespace>MultiLanguageForXAML</RootNamespace>
910
<Platforms>AnyCPU</Platforms>
@@ -13,6 +14,7 @@
1314
<Description>Multilanguage support for WPF </Description>
1415
<Authors>DaZiYuan</Authors>
1516
<PackageReadmeFile>README.md</PackageReadmeFile>
17+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
1618
</PropertyGroup>
1719

1820
<ItemGroup>
@@ -21,4 +23,9 @@
2123
<PackagePath>\</PackagePath>
2224
</None>
2325
</ItemGroup>
26+
27+
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
28+
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
29+
<PackageReference Include="IndexRange" Version="1.0.2" />
30+
</ItemGroup>
2431
</Project>

Samples.WPF/Samples.WPF.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
<PropertyGroup>
44
<OutputType>WinExe</OutputType>
5-
<TargetFramework>net6.0-windows</TargetFramework>
5+
<!--<TargetFramework>net6.0-windows</TargetFramework>-->
6+
<LangVersion>10.0</LangVersion>
7+
<TargetFramework>net46</TargetFramework>
68
<UseWPF>true</UseWPF>
79
<Platforms>AnyCPU</Platforms>
810
<Nullable>enable</Nullable>

0 commit comments

Comments
 (0)