Skip to content

Commit 3b6dadb

Browse files
author
LunaWanderer1
committed
Modify to use embeeded resources if file does not exist and remove PathHelpers and move the logic to LoadJsonResources
1 parent e2e6a00 commit 3b6dadb

File tree

8 files changed

+45
-16
lines changed

8 files changed

+45
-16
lines changed

LocalizationSample.Blazor.WebApp/LocalizationSample.Blazor.WebApp.Client/LocalizationSample.Blazor.WebApp.Client.csproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1-
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
1+
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
22

33
<PropertyGroup>
44
<TargetFramework>net8.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile>
88
<StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode>
9+
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
910
</PropertyGroup>
1011

12+
<ItemGroup>
13+
<Content Remove="Resources\fr-FR.json" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<EmbeddedResource Include="Resources\fr-FR.json">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</EmbeddedResource>
20+
</ItemGroup>
21+
1122
<ItemGroup>
1223
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.0" />
1324
<ProjectReference Include="..\..\src\My.Extensions.Localization.Json\My.Extensions.Localization.Json.csproj" />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
2+
using System.Globalization;
23

34
var builder = WebAssemblyHostBuilder.CreateDefault(args);
45

56
builder.Services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
67

8+
CultureInfo.CurrentCulture = new CultureInfo("fr-FR");
9+
CultureInfo.CurrentUICulture = new CultureInfo("fr-FR");
10+
711
await builder.Build().RunAsync();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Hello": "Bonjour English"
3+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"Hello": "Bonjour"
2+
"Hello": "Bonjour French"
33
}

LocalizationSample.Blazor.WebApp/LocalizationSample.Blazor.WebApp/LocalizationSample.Blazor.WebApp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net8.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<BlazorWebAssemblyLoadAllGlobalizationData>true</BlazorWebAssemblyLoadAllGlobalizationData>
78
</PropertyGroup>
89

910
<ItemGroup>

src/My.Extensions.Localization.Json/Internal/JsonResourceManager.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Globalization;
44
using System.IO;
55
using System.Linq;
6+
using System.Reflection;
67
using System.Text.Json;
78

89
namespace My.Extensions.Localization.Json.Internal
@@ -182,13 +183,33 @@ IEnumerable<string> GetResourceFiles(string culture)
182183
private static IDictionary<string, string> LoadJsonResources(string filePath)
183184
{
184185
var resources = new Dictionary<string, string>();
186+
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
187+
var directory = !string.IsNullOrEmpty(assemblyLocation) ? Path.GetDirectoryName(assemblyLocation) : "";
188+
filePath = Path.Combine(directory, filePath);
189+
185190
if (File.Exists(filePath))
186191
{
187192
using var reader = new StreamReader(filePath);
188193

189194
using var document = JsonDocument.Parse(reader.BaseStream, _jsonDocumentOptions);
190195

191196
resources = document.RootElement.EnumerateObject().ToDictionary(e => e.Name, e => e.Value.ToString());
197+
} else
198+
{
199+
var resources_embedded = Assembly.GetEntryAssembly().GetManifestResourceNames();
200+
foreach (var resourceName in resources_embedded)
201+
{
202+
if (resourceName.Contains(filePath.Replace("/", "."))) {
203+
using (Stream stream = Assembly.GetEntryAssembly().GetManifestResourceStream(resourceName))
204+
{
205+
using (StreamReader reader = new StreamReader(stream))
206+
{
207+
using var document = JsonDocument.Parse(reader.BaseStream, _jsonDocumentOptions);
208+
resources = document.RootElement.EnumerateObject().ToDictionary(e => e.Name, e => e.Value.ToString());
209+
}
210+
}
211+
}
212+
}
192213
}
193214

194215
return resources;

src/My.Extensions.Localization.Json/Internal/PathHelpers.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/My.Extensions.Localization.Json/JsonStringLocalizerFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public IStringLocalizer Create(Type resourceSource)
4646
// TODO: Check why an exception happen before the host build
4747
if (resourceSource.Name == "Controller")
4848
{
49-
resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(resourceSource.Assembly));
49+
resourcesPath = GetResourcePath(resourceSource.Assembly);
5050

5151
return _localizerCache.GetOrAdd(resourceSource.Name, _ => CreateJsonStringLocalizer(resourcesPath, TryFixInnerClassPath("Controller")));
5252
}
@@ -58,7 +58,7 @@ public IStringLocalizer Create(Type resourceSource)
5858
? typeInfo.Name
5959
: TrimPrefix(typeInfo.FullName, assemblyName + ".");
6060

61-
resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));
61+
resourcesPath = GetResourcePath(assembly);
6262
typeName = TryFixInnerClassPath(typeName);
6363

6464
return _localizerCache.GetOrAdd($"culture={CultureInfo.CurrentUICulture.Name}, typeName={typeName}", _ => CreateJsonStringLocalizer(resourcesPath, typeName));
@@ -80,7 +80,7 @@ public IStringLocalizer Create(string baseName, string location)
8080
{
8181
var assemblyName = new AssemblyName(location);
8282
var assembly = Assembly.Load(assemblyName);
83-
var resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));
83+
var resourcesPath = GetResourcePath(assembly);
8484
string resourceName = null;
8585
if (baseName == string.Empty)
8686
{

0 commit comments

Comments
 (0)