Add LocalizationOptions to specify resources location
This commit is contained in:
parent
a7b1b1e4ec
commit
eefe518ac2
|
|
@ -0,0 +1,16 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
namespace Microsoft.Framework.Localization
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides programmatic configuration for localization.
|
||||
/// </summary>
|
||||
public class LocalizationOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// The relative path under application root where resource files are located.
|
||||
/// </summary>
|
||||
public string ResourcesPath { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using Microsoft.Framework.DependencyInjection.Extensions;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.Localization;
|
||||
|
|
@ -18,6 +19,19 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
||||
/// <returns>The <see cref="IServiceCollection"/>.</returns>
|
||||
public static IServiceCollection AddLocalization([NotNull] this IServiceCollection services)
|
||||
{
|
||||
return AddLocalization(services, setupAction: null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds services required for application localization.
|
||||
/// </summary>
|
||||
/// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
|
||||
/// <param name="setupAction">An action to configure the <see cref="LocalizationOptions"/>.</param>
|
||||
/// <returns>The <see cref="IServiceCollection"/>.</returns>
|
||||
public static IServiceCollection AddLocalization(
|
||||
[NotNull] this IServiceCollection services,
|
||||
Action<LocalizationOptions> setupAction)
|
||||
{
|
||||
services.TryAdd(new ServiceDescriptor(
|
||||
typeof(IStringLocalizerFactory),
|
||||
|
|
@ -28,6 +42,11 @@ namespace Microsoft.Framework.DependencyInjection
|
|||
typeof(StringLocalizer<>),
|
||||
ServiceLifetime.Transient));
|
||||
|
||||
if (setupAction != null)
|
||||
{
|
||||
services.Configure(setupAction);
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,8 +4,9 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Dnx.Runtime;
|
||||
using Microsoft.Framework.Internal;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
|
||||
namespace Microsoft.Framework.Localization
|
||||
{
|
||||
|
|
@ -18,13 +19,23 @@ namespace Microsoft.Framework.Localization
|
|||
|
||||
private readonly IApplicationEnvironment _applicationEnvironment;
|
||||
|
||||
private readonly string _resourcesRelativePath;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="ResourceManagerStringLocalizer"/>.
|
||||
/// </summary>
|
||||
/// <param name="applicationEnvironment"></param>
|
||||
public ResourceManagerStringLocalizerFactory([NotNull] IApplicationEnvironment applicationEnvironment)
|
||||
/// <param name="applicationEnvironment">The <see cref="IApplicationEnvironment"/>.</param>
|
||||
/// <param name="localizationOptions">The <see cref="IOptions{LocalizationOptions}"/>.</param>
|
||||
public ResourceManagerStringLocalizerFactory(
|
||||
[NotNull] IApplicationEnvironment applicationEnvironment,
|
||||
[NotNull] IOptions<LocalizationOptions> localizationOptions)
|
||||
{
|
||||
_applicationEnvironment = applicationEnvironment;
|
||||
_resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;
|
||||
if (!string.IsNullOrEmpty(_resourcesRelativePath))
|
||||
{
|
||||
_resourcesRelativePath = _resourcesRelativePath.Replace("/", ".") + ".";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -37,9 +48,10 @@ namespace Microsoft.Framework.Localization
|
|||
{
|
||||
var typeInfo = resourceSource.GetTypeInfo();
|
||||
var assembly = typeInfo.Assembly;
|
||||
var baseName = typeInfo.FullName;
|
||||
var baseName = _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + resourceSource.Name;
|
||||
|
||||
return new ResourceManagerStringLocalizer(
|
||||
new ResourceManager(resourceSource),
|
||||
new ResourceManager(baseName, assembly),
|
||||
assembly,
|
||||
baseName,
|
||||
_resourceNamesCache);
|
||||
|
|
@ -51,9 +63,11 @@ namespace Microsoft.Framework.Localization
|
|||
/// <param name="baseName">The base name of the resource to load strings from.</param>
|
||||
/// <param name="location">The location to load resources from.</param>
|
||||
/// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns>
|
||||
public IStringLocalizer Create([NotNull] string baseName, [NotNull] string location)
|
||||
public IStringLocalizer Create([NotNull] string baseName, string location)
|
||||
{
|
||||
var assembly = Assembly.Load(new AssemblyName(location ?? _applicationEnvironment.ApplicationName));
|
||||
var rootPath = location ?? _applicationEnvironment.ApplicationName;
|
||||
var assembly = Assembly.Load(new AssemblyName(rootPath));
|
||||
baseName = rootPath + "." + _resourcesRelativePath + baseName;
|
||||
|
||||
return new ResourceManagerStringLocalizer(
|
||||
new ResourceManager(baseName, assembly),
|
||||
|
|
|
|||
|
|
@ -6,13 +6,14 @@
|
|||
"url": "https://github.com/aspnet/localization"
|
||||
},
|
||||
"dependencies": {
|
||||
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Framework.Localization.Abstractions": "1.0.0-*",
|
||||
"Microsoft.Framework.NotNullAttribute.Sources": {
|
||||
"type": "build",
|
||||
"version": "1.0.0-*"
|
||||
},
|
||||
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*"
|
||||
"Microsoft.Framework.OptionsModel": "1.0.0-*"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": { },
|
||||
|
|
|
|||
|
|
@ -0,0 +1,60 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System.Linq;
|
||||
using Microsoft.Framework.DependencyInjection;
|
||||
using Microsoft.Framework.OptionsModel;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Framework.Localization.Test
|
||||
{
|
||||
public class LocalizationServiceCollectionExtensionsTest
|
||||
{
|
||||
[Fact]
|
||||
public void AddLocalization_AddsNeededServices()
|
||||
{
|
||||
// Arrange
|
||||
var collection = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
collection.AddLocalization();
|
||||
|
||||
// Assert
|
||||
var services = collection.ToList();
|
||||
Assert.Equal(2, services.Count);
|
||||
|
||||
Assert.Equal(typeof(IStringLocalizerFactory), services[0].ServiceType);
|
||||
Assert.Equal(typeof(ResourceManagerStringLocalizerFactory), services[0].ImplementationType);
|
||||
Assert.Equal(ServiceLifetime.Singleton, services[0].Lifetime);
|
||||
|
||||
Assert.Equal(typeof(IStringLocalizer<>), services[1].ServiceType);
|
||||
Assert.Equal(typeof(StringLocalizer<>), services[1].ImplementationType);
|
||||
Assert.Equal(ServiceLifetime.Transient, services[1].Lifetime);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddLocalizationWithLocalizationOptions_AddsNeededServices()
|
||||
{
|
||||
// Arrange
|
||||
var collection = new ServiceCollection();
|
||||
|
||||
// Act
|
||||
collection.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||
|
||||
// Assert
|
||||
var services = collection.ToList();
|
||||
Assert.Equal(3, services.Count);
|
||||
|
||||
Assert.Equal(typeof(IStringLocalizerFactory), services[0].ServiceType);
|
||||
Assert.Equal(typeof(ResourceManagerStringLocalizerFactory), services[0].ImplementationType);
|
||||
Assert.Equal(ServiceLifetime.Singleton, services[0].Lifetime);
|
||||
|
||||
Assert.Equal(typeof(IStringLocalizer<>), services[1].ServiceType);
|
||||
Assert.Equal(typeof(StringLocalizer<>), services[1].ImplementationType);
|
||||
Assert.Equal(ServiceLifetime.Transient, services[1].Lifetime);
|
||||
|
||||
Assert.Equal(typeof(IConfigureOptions<LocalizationOptions>), services[2].ServiceType);
|
||||
Assert.Equal(ServiceLifetime.Singleton, services[2].Lifetime);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
{
|
||||
"dependencies": {
|
||||
"Microsoft.Framework.DependencyInjection": "1.0.0-*",
|
||||
"Microsoft.Framework.Localization": "1.0.0-*",
|
||||
"xunit": "2.1.0-*",
|
||||
"xunit.runner.dnx": "2.1.0-*",
|
||||
"Microsoft.Framework.Localization": "1.0.0-*"
|
||||
"xunit.runner.dnx": "2.1.0-*"
|
||||
},
|
||||
"frameworks": {
|
||||
"dnx451": { },
|
||||
|
|
|
|||
Loading…
Reference in New Issue