Add LocalizationOptions to specify resources location

This commit is contained in:
Kirthi Krishnamraju 2015-09-11 14:28:55 -07:00
parent a7b1b1e4ec
commit eefe518ac2
6 changed files with 121 additions and 10 deletions

View File

@ -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; }
}
}

View File

@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved. // 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. // 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.DependencyInjection.Extensions;
using Microsoft.Framework.Internal; using Microsoft.Framework.Internal;
using Microsoft.Framework.Localization; 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> /// <param name="services">The <see cref="IServiceCollection"/> to add the services to.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns> /// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddLocalization([NotNull] this IServiceCollection services) 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( services.TryAdd(new ServiceDescriptor(
typeof(IStringLocalizerFactory), typeof(IStringLocalizerFactory),
@ -28,6 +42,11 @@ namespace Microsoft.Framework.DependencyInjection
typeof(StringLocalizer<>), typeof(StringLocalizer<>),
ServiceLifetime.Transient)); ServiceLifetime.Transient));
if (setupAction != null)
{
services.Configure(setupAction);
}
return services; return services;
} }
} }

View File

@ -4,8 +4,9 @@
using System; using System;
using System.Reflection; using System.Reflection;
using System.Resources; using System.Resources;
using Microsoft.Framework.Internal;
using Microsoft.Dnx.Runtime; using Microsoft.Dnx.Runtime;
using Microsoft.Framework.Internal;
using Microsoft.Framework.OptionsModel;
namespace Microsoft.Framework.Localization namespace Microsoft.Framework.Localization
{ {
@ -18,13 +19,23 @@ namespace Microsoft.Framework.Localization
private readonly IApplicationEnvironment _applicationEnvironment; private readonly IApplicationEnvironment _applicationEnvironment;
private readonly string _resourcesRelativePath;
/// <summary> /// <summary>
/// Creates a new <see cref="ResourceManagerStringLocalizer"/>. /// Creates a new <see cref="ResourceManagerStringLocalizer"/>.
/// </summary> /// </summary>
/// <param name="applicationEnvironment"></param> /// <param name="applicationEnvironment">The <see cref="IApplicationEnvironment"/>.</param>
public ResourceManagerStringLocalizerFactory([NotNull] IApplicationEnvironment applicationEnvironment) /// <param name="localizationOptions">The <see cref="IOptions{LocalizationOptions}"/>.</param>
public ResourceManagerStringLocalizerFactory(
[NotNull] IApplicationEnvironment applicationEnvironment,
[NotNull] IOptions<LocalizationOptions> localizationOptions)
{ {
_applicationEnvironment = applicationEnvironment; _applicationEnvironment = applicationEnvironment;
_resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;
if (!string.IsNullOrEmpty(_resourcesRelativePath))
{
_resourcesRelativePath = _resourcesRelativePath.Replace("/", ".") + ".";
}
} }
/// <summary> /// <summary>
@ -37,9 +48,10 @@ namespace Microsoft.Framework.Localization
{ {
var typeInfo = resourceSource.GetTypeInfo(); var typeInfo = resourceSource.GetTypeInfo();
var assembly = typeInfo.Assembly; var assembly = typeInfo.Assembly;
var baseName = typeInfo.FullName; var baseName = _applicationEnvironment.ApplicationName + "." + _resourcesRelativePath + resourceSource.Name;
return new ResourceManagerStringLocalizer( return new ResourceManagerStringLocalizer(
new ResourceManager(resourceSource), new ResourceManager(baseName, assembly),
assembly, assembly,
baseName, baseName,
_resourceNamesCache); _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="baseName">The base name of the resource to load strings from.</param>
/// <param name="location">The location to load resources from.</param> /// <param name="location">The location to load resources from.</param>
/// <returns>The <see cref="ResourceManagerStringLocalizer"/>.</returns> /// <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( return new ResourceManagerStringLocalizer(
new ResourceManager(baseName, assembly), new ResourceManager(baseName, assembly),

View File

@ -6,13 +6,14 @@
"url": "https://github.com/aspnet/localization" "url": "https://github.com/aspnet/localization"
}, },
"dependencies": { "dependencies": {
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*",
"Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-*", "Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-*",
"Microsoft.Framework.Localization.Abstractions": "1.0.0-*", "Microsoft.Framework.Localization.Abstractions": "1.0.0-*",
"Microsoft.Framework.NotNullAttribute.Sources": { "Microsoft.Framework.NotNullAttribute.Sources": {
"type": "build", "type": "build",
"version": "1.0.0-*" "version": "1.0.0-*"
}, },
"Microsoft.Dnx.Runtime.Abstractions": "1.0.0-*" "Microsoft.Framework.OptionsModel": "1.0.0-*"
}, },
"frameworks": { "frameworks": {
"dnx451": { }, "dnx451": { },

View File

@ -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);
}
}
}

View File

@ -1,8 +1,9 @@
{ {
"dependencies": { "dependencies": {
"Microsoft.Framework.DependencyInjection": "1.0.0-*",
"Microsoft.Framework.Localization": "1.0.0-*",
"xunit": "2.1.0-*", "xunit": "2.1.0-*",
"xunit.runner.dnx": "2.1.0-*", "xunit.runner.dnx": "2.1.0-*"
"Microsoft.Framework.Localization": "1.0.0-*"
}, },
"frameworks": { "frameworks": {
"dnx451": { }, "dnx451": { },