ASP.NET Core 1.1 Localization Generic Service

I am making an ASP.NET Core 1.1 application and trying to set up localization.

When I do on my ValuesControllerimplementation IStringLocalizer, it works fine and localizes my resource file.

public ValuesController(IStringLocalizer<ValuesController> localizer, IService<BaseEntity> service)
{
    _localizer = localizer;
    _service = service;
}

In the above code, find the resources in the Resources / Controllers / Values ​​section of Controller.en-US.resx.

But, when I try to enter IStringLocalizerinto a shared service, it cannot find the resource file.

public class Service<T> : IService<T>
    where T : BaseEntity
{
    #region Properties
    protected IRepository Repository { get; set; }
    protected IUnitOfWorkFactory UnitOfWorkFactory { get; set; }
    private readonly ILogger _logger;
    private readonly IStringLocalizer _localizer;

    #endregion

    #region Ctor
    public Service(IRepository repository, IUnitOfWorkFactory unitOfWorkFactory,
        ILogger<Service<T>> logger, IStringLocalizer<Service<T>> localizer)
    {
        Repository = repository;
        UnitOfWorkFactory = unitOfWorkFactory;
        _logger = logger;
        _localizer = localizer;
    }
}

In the above code, my resource was not found in the section "Resources / Services / Base / Service .en-US.resx"

Any idea on how to do this?

--- EDIT

MyControl.Api (Startup.cs)

namespace MyControl.Api

services.AddLocalization(options => options.ResourcesPath = "Resources");

This line is located inside "MyControl.Api", which is located in the namespace "MyControl.Api".

"/"

"MyControl.Services"

( )

"//Base"

- "MyControl.Services.Services.Base"

+4
1

, . ResourceManagerStringLocalizer ( IStringLocalizer ) - _resourceBaseName . ResourceManager.

IStringLocalizer , - :

FooProject.Resources.Services.FooService`1 [[System.Double, System.Private.CoreLib...

:

FooProject.Resources.Services.FooService

localizer _resourceBaseName . , , GitHub: https://github.com/aspnet/Localization/issues.

ResourceManagerStringLocalizer . baseName csproj :

:

MyCsprojFileName.AnyFolder.SubFolder.FooClass

baseName:

AnyFolder.SubFolder

, baseName, , "Services.Base". , :

public class FooService<T>
{
    private readonly IStringLocalizerFactory _factory;

    public FooService(IStringLocalizerFactory factory)
    {
        _factory = factory;
    }

    public IStringLocalizer GetLocalizer()
    {
        var type = typeof(FooService<>);

        string assemblyName = type.GetTypeInfo().Assembly.GetName().Name;
        string typeName = type.Name.Remove(type.Name.IndexOf('`'));
        string baseName = (type.Namespace + "." + typeName).Substring(assemblyName.Length).Trim('.');

        var localizer = _factory.Create(baseName, assemblyName);

        return localizer;
    }
}
+3

Source: https://habr.com/ru/post/1678364/


All Articles