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?
-
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.LazZiya– LazZiya2020-05-12 15:11:54 +00:00Commented 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?Abhi Singh– Abhi Singh2020-05-13 03:58:56 +00:00Commented May 13, 2020 at 3:58
Add a comment
|
1 Answer
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.
9 Comments
Howard Hee
I tried but it's working in controller, but not working in view
Mo D Genesis
I need more details
Howard Hee
I copied your code and run in my local. Localizer["something"] won't translate the word. But I use DI in controller is worked.
Mo D Genesis
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
Howard Hee
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 |