Skip to content

Commit 19c0ba5

Browse files
committed
Localize nested classes
1 parent 64f06ce commit 19c0ba5

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public IStringLocalizer Create(Type resourceSource)
4343
: typeInfo.FullName.Substring(assemblyName.Length + 1);
4444
var resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));
4545

46+
typeName = TryFixInnerClassPath(typeName);
47+
4648
return CreateJsonStringLocalizer(resourcesPath, typeName);
4749
}
4850

@@ -58,6 +60,8 @@ public IStringLocalizer Create(string baseName, string location)
5860
throw new ArgumentNullException(nameof(location));
5961
}
6062

63+
baseName = TryFixInnerClassPath(baseName);
64+
6165
var assemblyName = new AssemblyName(location);
6266
var assembly = Assembly.Load(assemblyName);
6367
var resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));
@@ -101,5 +105,18 @@ private static string TrimPrefix(string name, string prefix)
101105

102106
return name;
103107
}
108+
109+
private string TryFixInnerClassPath(string path)
110+
{
111+
const char innerClassSeparator = '+';
112+
var fixedPath = path;
113+
114+
if (path.Contains(innerClassSeparator))
115+
{
116+
fixedPath = path.Replace(innerClassSeparator, '.');
117+
}
118+
119+
return fixedPath;
120+
}
104121
}
105122
}

test/My.Extensions.Localization.Json.Tests/JsonStringLocalizerFactoryTests.cs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
using Microsoft.Extensions.Logging;
1+
using System.Net;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Builder;
4+
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.TestHost;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Localization;
9+
using Microsoft.Extensions.Logging;
210
using Microsoft.Extensions.Logging.Abstractions;
311
using Microsoft.Extensions.Options;
412
using Moq;
@@ -56,11 +64,74 @@ public void CreateLocalizerWithBasenameAndLocation(ResourcesType resourcesType)
5664
Assert.Equal("Bonjour", localizer["Hello"]);
5765
}
5866

67+
[Fact]
68+
public async Task LocalizerReturnsTranslationFromInnerClass()
69+
{
70+
var webHostBuilder = new WebHostBuilder()
71+
.ConfigureServices(services =>
72+
{
73+
services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
74+
})
75+
.Configure(app =>
76+
{
77+
app.UseRequestLocalization("en", "ar");
78+
79+
app.Run(context =>
80+
{
81+
var localizer = context.RequestServices.GetService<IStringLocalizer<Model>>();
82+
83+
LocalizationHelper.SetCurrentCulture("ar");
84+
85+
Assert.Equal("مرحباً", localizer["Hello"]);
86+
87+
return Task.FromResult(0);
88+
});
89+
});
90+
91+
using (var server = new TestServer(webHostBuilder))
92+
{
93+
var client = server.CreateClient();
94+
var response = await client.GetAsync("/");
95+
}
96+
}
97+
5998
private void SetupLocalizationOptions(string resourcesPath, ResourcesType resourcesType = ResourcesType.TypeBased)
6099
=> _localizationOptions.Setup(o => o.Value)
61100
.Returns(() => new JsonLocalizationOptions {
62101
ResourcesPath = resourcesPath,
63102
ResourcesType = resourcesType
64103
});
104+
105+
public class InnerClassStartup
106+
{
107+
public void ConfigureServices(IServiceCollection services)
108+
{
109+
services.AddMvc();
110+
services.AddLocalization();
111+
services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
112+
}
113+
114+
public void Configure(IApplicationBuilder app, IStringLocalizer<Model> localizer)
115+
{
116+
var supportedCultures = new[] { "ar", "en" };
117+
app.UseRequestLocalization(options =>
118+
options
119+
.AddSupportedCultures(supportedCultures)
120+
.AddSupportedUICultures(supportedCultures)
121+
.SetDefaultCulture("ar")
122+
);
123+
124+
app.Run(async (context) =>
125+
{
126+
var loc = localizer["Hello"];
127+
await context.Response.WriteAsync(localizer["Hello"]);
128+
});
129+
}
130+
}
131+
132+
public class Model
133+
{
134+
public string Hello { get; set; }
135+
}
65136
}
66137
}

test/My.Extensions.Localization.Json.Tests/My.Extensions.Localization.Json.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
</ItemGroup>
1919

2020
<ItemGroup>
21+
<None Update="Resources\JsonStringLocalizerFactoryTests.Model.ar.json">
22+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
23+
</None>
2124
<None Update="Resources\fr-FR.json">
2225
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
2326
</None>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Hello": "مرحباً"
3+
}

0 commit comments

Comments
 (0)