Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localizer factory should respect class namespace #49

Merged
merged 1 commit into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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