Skip to content

Commit

Permalink
Use NullStringLocalizer by default in the CMS (#3344)
Browse files Browse the repository at this point in the history
Fixes #3301
  • Loading branch information
jtkech authored and sebastienros committed Mar 17, 2019
1 parent 8b339fe commit 99b90fa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.Extensions.Localization;

namespace OrchardCore.Localization
{
internal class NullStringLocalizerFactory : IStringLocalizerFactory
{
public IStringLocalizer Create(Type resourceSource) => NullStringLocalizer.Instance;
public IStringLocalizer Create(string baseName, string location) => NullStringLocalizer.Instance;

private class NullStringLocalizer : IStringLocalizer
{
public static NullStringLocalizer Instance { get; } = new NullStringLocalizer();

public LocalizedString this[string name] => new LocalizedString(name, name);

public LocalizedString this[string name, params object[] arguments]
=> new LocalizedString(name, string.Format(name, arguments));

public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
=> Enumerable.Empty<LocalizedString>();

public IStringLocalizer WithCulture(CultureInfo culture) => Instance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
using Microsoft.Net.Http.Headers;
using OrchardCore;
using OrchardCore.Environment.Extensions;
using OrchardCore.Environment.Shell;
using OrchardCore.Environment.Shell.Configuration;
using OrchardCore.Environment.Shell.Descriptor.Models;
using OrchardCore.Localization;
using OrchardCore.Modules;

namespace Microsoft.Extensions.DependencyInjection
Expand Down Expand Up @@ -62,6 +64,10 @@ private static void AddDefaultServices(IServiceCollection services)

// These services might be moved at a higher level if no components from OrchardCore needs them.
services.AddLocalization();

// For performance, prevents the 'ResourceManagerStringLocalizer' from being used.
services.AddSingleton<IStringLocalizerFactory, NullStringLocalizerFactory>();

services.AddWebEncoders();

// ModularTenantRouterMiddleware which is configured with UseOrchardCore() calls UseRouter() which requires the routing services to be
Expand Down Expand Up @@ -95,7 +101,7 @@ private static void AddExtensionServices(OrchardCoreBuilder builder)
{
builder.ApplicationServices.AddSingleton<IModuleNamesProvider, AssemblyAttributeModuleNamesProvider>();
builder.ApplicationServices.AddSingleton<IApplicationContext, ModularApplicationContext>();

builder.ApplicationServices.AddExtensionManagerHost();

builder.ConfigureServices(services =>
Expand Down

0 comments on commit 99b90fa

Please sign in to comment.