Skip to content

Commit

Permalink
Localizer factory should respect class namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Apr 3, 2020
1 parent 5b1013d commit 64f06ce
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/My.Extensions.Localization.Json/JsonStringLocalizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ private void BuildResourcesCache(string culture)
: $"{_resourceName}.{culture}.json";
_searchedLocation = Path.Combine( _resourcesPath, resourceFile);
if (resourceFile.StartsWith("Views"))
if (!File.Exists(_searchedLocation))
{
if (!File.Exists(_searchedLocation))
if (resourceFile.Count(r => r == '.') > 1)
{
var resourceFileWithoutExtension = Path.GetFileNameWithoutExtension(resourceFile);
var resourceFileWithoutCulture = resourceFileWithoutExtension.Substring(0, resourceFileWithoutExtension.LastIndexOf('.'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public IStringLocalizer Create(Type resourceSource)

var typeInfo = resourceSource.GetTypeInfo();
var assembly = typeInfo.Assembly;
var assemblyName = resourceSource.Assembly.GetName().Name;
var typeName = $"{assemblyName}.{typeInfo.Name}" == typeInfo.FullName
? typeInfo.Name
: typeInfo.FullName.Substring(assemblyName.Length + 1);
var resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));

return CreateJsonStringLocalizer(resourcesPath, typeInfo.Name);
return CreateJsonStringLocalizer(resourcesPath, typeName);
}

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

var assembly = Assembly.GetExecutingAssembly();
var assemblyName = new AssemblyName(location);
var assembly = Assembly.Load(assemblyName);
var resourcesPath = Path.Combine(PathHelpers.GetApplicationRoot(), GetResourcePath(assembly));
string resourceName = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,24 @@ public async Task LocalizeViewWithPathConventions()
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Bienvenu", content);
}

[Fact]
public async Task StringLocalizeShouldWorkWithControllersPrefix()
{
// Arrange
var url = "/Home/Privacy";
var request = new HttpRequestMessage(HttpMethod.Get, url);
var culture = "fr-FR";
var cookieValue = $"c={culture}|uic={culture}";
request.Headers.Add("Cookie", $"{CookieRequestCultureProvider.DefaultCookieName}={cookieValue}");

// Act
var response = await _client.SendAsync(request);
var content = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Contains("Utilisez cette page pour détailler la politique de confidentialité de votre site.", WebUtility.HtmlDecode(content));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void CreateLocalizerWithBasenameAndLocation(ResourcesType resourcesType)
// Arrange
var localizerFactory = new JsonStringLocalizerFactory(_localizationOptions.Object, _loggerFactory);
var location = "My.Extensions.Localization.Json.Tests";
var basename = $"{location}.{nameof(Test)}";
var basename = $"{location}.Common.{nameof(Test)}";

// Act
var localizer = localizerFactory.Create(basename, location);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
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;
using My.Extensions.Localization.Json.Internal;
using My.Extensions.Localization.Json.Tests.Common;
using Xunit;

Expand All @@ -20,18 +17,17 @@ namespace My.Extensions.Localization.Json.Tests
public class JsonStringLocalizerTests
{
private readonly IStringLocalizer _localizer;
private readonly Mock<IOptions<LocalizationOptions>> _localizationOptions;
private readonly ILogger _logger;
private readonly Mock<IOptions<JsonLocalizationOptions>> _localizationOptions;

public JsonStringLocalizerTests()
{
_localizationOptions = new Mock<IOptions<LocalizationOptions>>();
var _localizationOptions = new Mock<IOptions<JsonLocalizationOptions>>();
_localizationOptions.Setup(o => o.Value)
.Returns(() => new LocalizationOptions { ResourcesPath = "Resources" });
_logger = NullLogger.Instance;

var resourcePath = Path.Combine(PathHelpers.GetApplicationRoot(), _localizationOptions.Object.Value.ResourcesPath);
_localizer = new JsonStringLocalizer(resourcePath, nameof(Test), _logger);
.Returns(() => new JsonLocalizationOptions { ResourcesPath = "Resources" });
var localizerFactory = new JsonStringLocalizerFactory(_localizationOptions.Object, NullLoggerFactory.Instance);
var location = "My.Extensions.Localization.Json.Tests";
var basename = $"{location}.Common.{nameof(Test)}";
_localizer = localizerFactory.Create(basename, location);
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
<None Update="Resources\fr-FR.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Test.ar.json">
<None Update="Resources\Common\Test.ar.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Test.fr-FR.json">
<None Update="Resources\Common\Test.fr-FR.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\Test.fr.json">
<None Update="Resources\Common\Test.fr.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down

0 comments on commit 64f06ce

Please sign in to comment.