diff --git a/src/Microsoft.Framework.Localization/LocalizationOptions.cs b/src/Microsoft.Framework.Localization/LocalizationOptions.cs
new file mode 100644
index 0000000000..f1da826d83
--- /dev/null
+++ b/src/Microsoft.Framework.Localization/LocalizationOptions.cs
@@ -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
+{
+ ///
+ /// Provides programmatic configuration for localization.
+ ///
+ public class LocalizationOptions
+ {
+ ///
+ /// The relative path under application root where resource files are located.
+ ///
+ public string ResourcesPath { get; set; }
+ }
+}
diff --git a/src/Microsoft.Framework.Localization/LocalizationServiceCollectionExtensions.cs b/src/Microsoft.Framework.Localization/LocalizationServiceCollectionExtensions.cs
index f81c13afb4..543a985cda 100644
--- a/src/Microsoft.Framework.Localization/LocalizationServiceCollectionExtensions.cs
+++ b/src/Microsoft.Framework.Localization/LocalizationServiceCollectionExtensions.cs
@@ -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
/// The to add the services to.
/// The .
public static IServiceCollection AddLocalization([NotNull] this IServiceCollection services)
+ {
+ return AddLocalization(services, setupAction: null);
+ }
+
+ ///
+ /// Adds services required for application localization.
+ ///
+ /// The to add the services to.
+ /// An action to configure the .
+ /// The .
+ public static IServiceCollection AddLocalization(
+ [NotNull] this IServiceCollection services,
+ Action 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;
}
}
diff --git a/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizerFactory.cs b/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizerFactory.cs
index b76fbdde53..49c7d12c56 100644
--- a/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizerFactory.cs
+++ b/src/Microsoft.Framework.Localization/ResourceManagerStringLocalizerFactory.cs
@@ -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;
+
///
/// Creates a new .
///
- ///
- public ResourceManagerStringLocalizerFactory([NotNull] IApplicationEnvironment applicationEnvironment)
+ /// The .
+ /// The .
+ public ResourceManagerStringLocalizerFactory(
+ [NotNull] IApplicationEnvironment applicationEnvironment,
+ [NotNull] IOptions localizationOptions)
{
_applicationEnvironment = applicationEnvironment;
+ _resourcesRelativePath = localizationOptions.Value.ResourcesPath ?? string.Empty;
+ if (!string.IsNullOrEmpty(_resourcesRelativePath))
+ {
+ _resourcesRelativePath = _resourcesRelativePath.Replace("/", ".") + ".";
+ }
}
///
@@ -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
/// The base name of the resource to load strings from.
/// The location to load resources from.
/// The .
- 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),
diff --git a/src/Microsoft.Framework.Localization/project.json b/src/Microsoft.Framework.Localization/project.json
index 44e1cb40b3..03fa7b30f4 100644
--- a/src/Microsoft.Framework.Localization/project.json
+++ b/src/Microsoft.Framework.Localization/project.json
@@ -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": { },
diff --git a/test/Microsoft.Framework.Localization.Tests/LocalizationServiceCollectionExtensionsTest.cs b/test/Microsoft.Framework.Localization.Tests/LocalizationServiceCollectionExtensionsTest.cs
new file mode 100644
index 0000000000..213077e992
--- /dev/null
+++ b/test/Microsoft.Framework.Localization.Tests/LocalizationServiceCollectionExtensionsTest.cs
@@ -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), services[2].ServiceType);
+ Assert.Equal(ServiceLifetime.Singleton, services[2].Lifetime);
+ }
+ }
+}
diff --git a/test/Microsoft.Framework.Localization.Tests/project.json b/test/Microsoft.Framework.Localization.Tests/project.json
index bc4a953cec..c69723680f 100644
--- a/test/Microsoft.Framework.Localization.Tests/project.json
+++ b/test/Microsoft.Framework.Localization.Tests/project.json
@@ -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": { },