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
Show file tree
Hide file tree
Changes from all commits
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
@@ -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
Copy link
Member

@hishamco hishamco Mar 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not wrong if anyone asking DI for the current IStringLocalizer an exception will be thrown because the NullStringLocalizer is private

Have a look to NullLoggerProvider https://github.com/aspnet/Extensions/blob/f162f1006bf8954f0102af8ff98c04077cf21b04/src/Logging/Logging.Abstractions/src/NullLoggerProvider.cs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are wrong ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So can you tell me the expected type?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always inject a typed localizer which resolves StringLocalizer<TResourceSource> which injects and uses IStringLocalizerFactory which here is our NullStringLocalizerFactory.

Localizer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it .. it will return NullStringLocalizerFactory.NullStringLocalizer as you mentioned because it is defined as nested type

thanks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, it was not clear to me either ;)

{
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