Skip to content

Commit

Permalink
Localize nested classes
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Apr 7, 2020
1 parent 64f06ce commit 19c0ba5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/My.Extensions.Localization.Json/JsonStringLocalizerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public IStringLocalizer Create(Type resourceSource)
: typeInfo.FullName.Substring(assemblyName.Length + 1);
var resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));

typeName = TryFixInnerClassPath(typeName);

return CreateJsonStringLocalizer(resourcesPath, typeName);
}

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

baseName = TryFixInnerClassPath(baseName);

var assemblyName = new AssemblyName(location);
var assembly = Assembly.Load(assemblyName);
var resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));
Expand Down Expand Up @@ -101,5 +105,18 @@ private static string TrimPrefix(string name, string prefix)

return name;
}

private string TryFixInnerClassPath(string path)
{
const char innerClassSeparator = '+';
var fixedPath = path;

if (path.Contains(innerClassSeparator))
{
fixedPath = path.Replace(innerClassSeparator, '.');
}

return fixedPath;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
using Microsoft.Extensions.Logging;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using Moq;
Expand Down Expand Up @@ -56,11 +64,74 @@ public void CreateLocalizerWithBasenameAndLocation(ResourcesType resourcesType)
Assert.Equal("Bonjour", localizer["Hello"]);
}

[Fact]
public async Task LocalizerReturnsTranslationFromInnerClass()
{
var webHostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
})
.Configure(app =>
{
app.UseRequestLocalization("en", "ar");
app.Run(context =>
{
var localizer = context.RequestServices.GetService<IStringLocalizer<Model>>();
LocalizationHelper.SetCurrentCulture("ar");
Assert.Equal("مرحباً", localizer["Hello"]);
return Task.FromResult(0);
});
});

using (var server = new TestServer(webHostBuilder))
{
var client = server.CreateClient();
var response = await client.GetAsync("/");
}
}

private void SetupLocalizationOptions(string resourcesPath, ResourcesType resourcesType = ResourcesType.TypeBased)
=> _localizationOptions.Setup(o => o.Value)
.Returns(() => new JsonLocalizationOptions {
ResourcesPath = resourcesPath,
ResourcesType = resourcesType
});

public class InnerClassStartup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddLocalization();
services.AddJsonLocalization(options => options.ResourcesPath = "Resources");
}

public void Configure(IApplicationBuilder app, IStringLocalizer<Model> localizer)
{
var supportedCultures = new[] { "ar", "en" };
app.UseRequestLocalization(options =>
options
.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures)
.SetDefaultCulture("ar")
);

app.Run(async (context) =>
{
var loc = localizer["Hello"];
await context.Response.WriteAsync(localizer["Hello"]);
});
}
}

public class Model
{
public string Hello { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
</ItemGroup>

<ItemGroup>
<None Update="Resources\JsonStringLocalizerFactoryTests.Model.ar.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\fr-FR.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Hello": "مرحباً"
}

0 comments on commit 19c0ba5

Please sign in to comment.