2

I am working on localization and I want to create a global resource file for all the strings for all views. I created resource files for model, controller and view but I need a global type of resource file that can store key value pair for Views. I don't don't know how to achieve this and where should I store my global resource file? Should I store it in models or services or in controllers?

2
  • It is more than just models, controllers and views... check this tutorial and wiki, this package offers an easy to implement localization with one resource file as well. Commented May 12, 2020 at 15:11
  • @LazZiya I have edited my question. I need to know how can I store strings for view to achieve localization? and what should I use to store the resx file controller,services or models? Commented May 13, 2020 at 3:58

1 Answer 1

3

in your Resources folder add the following files: (do your necessary imports of the code needed)

public class SharedViewLocalizer : ISharedViewLocalizer
{
    private readonly IStringLocalizer localizer;

    public SharedViewLocalizer(IStringLocalizerFactory factory)
    {
        var type = typeof(SharedResource);
        var assemblyName = new AssemblyName(type.GetTypeInfo().Assembly.FullName);
        this.localizer = factory.Create("SharedResource", assemblyName.Name);
    }

    public LocalizedString this[string key] => this.localizer[key];

    public LocalizedString GetLocalizedString(string key)
    {
        return this.localizer[key];
    }
}



public interface ISharedViewLocalizer
{
    public LocalizedString this[string key]
    {
        get;
    }

    LocalizedString GetLocalizedString(string key);
}


public class SharedResource
{
}

add a file called SharedResource.en-US.resx in the resource folder

in your cshtml:

@using yourdirectory.Resources

@inject ISharedViewLocalizer Localizer

also don't forget to dependency inject

services.AddTransient<ISharedViewLocalizer, SharedViewLocalizer>();

If you have done all this in your cshtml you can then do:

Localizer["something"]

which will write the translation on the html page.

someone posted this: https://medium.com/@flouss/asp-net-core-localization-one-resx-to-rule-them-all-de5c07692fa4

special thanks to him.

Sign up to request clarification or add additional context in comments.

9 Comments

I tried but it's working in controller, but not working in view
I need more details
I copied your code and run in my local. Localizer["something"] won't translate the word. But I use DI in controller is worked.
You have to do 5 things. If any of the steps are skipped it won't work. Also, check the medium post in case. Hope you haven't looked over this: SharedResource.en-US.resx
If resx problem then won't worked on controller also. I followed the posted link added the Data annotation localization and Controller localization, that's why worked on controller. But the View part I followed exactly all yours code, add class SharedViewLocalizer ,add SharedResource.en-US.resx , @inject ISharedViewLocalizer Localizer, add DI services.AddTransient<ISharedViewLocalizer, SharedViewLocalizer>(); and use Localizer["something"] in cshtml. But it's not working
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.