Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use NullStringLocalizer by default #3344

Merged
merged 2 commits into from
Mar 17, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Authentication;
Expand All @@ -10,6 +12,7 @@
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;
Expand Down Expand Up @@ -62,6 +65,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 +102,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 Expand Up @@ -246,5 +253,26 @@ private static void AddDataProtection(OrchardCoreBuilder builder)
services.Add(collection);
});
}

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