Fix #2874 part 2 - Add AddLocalization() overload for MVC localization.

This commit is contained in:
Ryan Nowak 2015-08-06 12:15:04 -07:00
parent 4b3bac9dbb
commit 152e4ef915
3 changed files with 61 additions and 6 deletions

View File

@ -0,0 +1,52 @@
// 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 Microsoft.AspNet.Mvc.Razor;
using Microsoft.Framework.Internal;
namespace Microsoft.Framework.DependencyInjection
{
/// <summary>
/// Extension methods for configuring MVC view localization.
/// </summary>
public static class MvcLocalizationMvcBuilderExtensions
{
/// <summary>
/// Adds MVC localization to the application.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
/// <remarks>
/// Adding localization also adds support for views via
/// <see cref="MvcViewFeaturesMvcBuilderExtensions.AddViews(IMvcBuilder)"/> and the Razor view engine
/// via <see cref="MvcRazorMvcBuilderExtensions.AddRazorViewEngine(IMvcBuilder)"/>.
/// </remarks>
public static IMvcBuilder AddLocalization([NotNull] this IMvcBuilder builder)
{
return AddLocalization(builder, LanguageViewLocationExpanderFormat.Suffix);
}
/// <summary>
/// Adds MVC localization to the application.
/// </summary>
/// <param name="builder">The <see cref="IMvcBuilder"/>.</param>
/// <param name="format">The view format for localized views.</param>
/// <returns>The <see cref="IMvcBuilder"/>.</returns>
/// <remarks>
/// Adding localization also adds support for views via
/// <see cref="MvcViewFeaturesMvcBuilderExtensions.AddViews(IMvcBuilder)"/> and the Razor view engine
/// via <see cref="MvcRazorMvcBuilderExtensions.AddRazorViewEngine(IMvcBuilder)"/>.
/// </remarks>
public static IMvcBuilder AddLocalization(
[NotNull] this IMvcBuilder builder,
LanguageViewLocationExpanderFormat format)
{
builder.AddViews();
builder.AddRazorViewEngine();
MvcLocalizationServiceCollectionExtensions.AddMvcLocalization(builder.Services, format);
return builder;
}
}
}

View File

@ -10,10 +10,13 @@ using Microsoft.Framework.WebEncoders;
namespace Microsoft.Framework.DependencyInjection namespace Microsoft.Framework.DependencyInjection
{ {
/// <summary>
/// Extension methods for configuring MVC view localization.
/// </summary>
public static class MvcLocalizationServiceCollectionExtensions public static class MvcLocalizationServiceCollectionExtensions
{ {
/// <summary> /// <summary>
/// Adds Mvc localization to the application. /// Adds MVC localization to the application.
/// </summary> /// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns> /// <returns>The <see cref="IServiceCollection"/>.</returns>
@ -23,10 +26,10 @@ namespace Microsoft.Framework.DependencyInjection
} }
/// <summary> /// <summary>
/// Adds Mvc localization to the application. /// Adds MVC localization to the application.
/// </summary> /// </summary>
/// <param name="services">The <see cref="IServiceCollection"/>.</param> /// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="option">The view format for localized views.</param> /// <param name="format">The view format for localized views.</param>
/// <returns>The <see cref="IServiceCollection"/>.</returns> /// <returns>The <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddMvcLocalization( public static IServiceCollection AddMvcLocalization(
[NotNull] this IServiceCollection services, [NotNull] this IServiceCollection services,
@ -40,7 +43,7 @@ namespace Microsoft.Framework.DependencyInjection
DefaultOrder.DefaultFrameworkSortOrder); DefaultOrder.DefaultFrameworkSortOrder);
services.TryAdd(ServiceDescriptor.Singleton<IHtmlLocalizerFactory, HtmlLocalizerFactory>()); services.TryAdd(ServiceDescriptor.Singleton<IHtmlLocalizerFactory, HtmlLocalizerFactory>());
services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer<>), typeof(HtmlLocalizer<>))); services.TryAdd(ServiceDescriptor.Transient(typeof(IHtmlLocalizer<>), typeof(HtmlLocalizer<>)));
services.TryAdd(ServiceDescriptor.Transient<IViewLocalizer, ViewLocalizer>()); services.TryAdd(ServiceDescriptor.Transient<IViewLocalizer, ViewLocalizer>());
if (!services.Any(sd => sd.ServiceType == typeof(IHtmlEncoder))) if (!services.Any(sd => sd.ServiceType == typeof(IHtmlEncoder)))
{ {

View File

@ -5,15 +5,15 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
using Microsoft.AspNet.Mvc.Localization;
using Microsoft.AspNet.Mvc.Razor; using Microsoft.AspNet.Mvc.Razor;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.Localization; using Microsoft.Framework.Localization;
using Microsoft.Framework.OptionsModel; using Microsoft.Framework.OptionsModel;
using Microsoft.Framework.WebEncoders; using Microsoft.Framework.WebEncoders;
using Microsoft.Framework.WebEncoders.Testing; using Microsoft.Framework.WebEncoders.Testing;
using Xunit; using Xunit;
namespace Microsoft.AspNet.Mvc.Localization.Test namespace Microsoft.Framework.DependencyInjection
{ {
public class MvcLocalizationServiceCollectionExtensionsTest public class MvcLocalizationServiceCollectionExtensionsTest
{ {