Using IViewLocalizer
I would like to implement view localization with a default or fallback resource file for words and phrases that appear on multiple pages like for example edit, add, delete... I don't want to add these repetitive phrases in the resource file of all the views where they appear so a fallback recource file would really come handy, but I just can't seem to find any solutions on how to do that.
Currently I'm using @inject IViewLocalizer Localizer
on my views and I'm getting the localized phrases with @Localizer["ResourceName"]
from the resource files:
Resources/Views/{ControllerName}/{ViewName}.{langCode}.resx
This works just fine for each separate view and partial view. Now I would like to have a resource file either in the Resources/Views
or Resources
folder that acts as a fallback resource file.
So for example if I say @Localizer["Edit"]
on one of my views and the "Edit" resource is not found in the Resources/Views/{ControllerName}/{ViewName}.{langCode}.resx
or Resources/Views/{ControllerName}/{ViewName}.resx
, it falls back to this default file so that I can use this on all of my views that need the resource "Edit".
I've allready tried with Resources/Resource.{langCode}.resx
but it seems to have no effect.
resx file contains a standard set of header information that describes the format of the resource entries, and specifies the versioning information for the XML code that parses the data. These files contain all the strings, labels, captions, and titles for all text in the three IBM Cognos Office components.
Open a . resx file in the XML (Text) editor. Press Ctrl+Alt+F or choose ReSharper | Windows | File Structure from the main menu . Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there.
I've had a similar problem when I started with localization. If you have a lot of repeated phrases and words on your site it's best to put them in a SharedResource file.
You do it by creating a SharedResource.[LANG_CODE].resx
file in Resources
folder. Then you need to create a dummy class that you call SharedResource
and put it in the root of your project namespace. So if your project is called LocalizationTest
then the class would look something like that:
namespace LocalizationTest
{
public class SharedResource
{
}
}
Then all you need to do to access shared resource in your model or view is to create a IStringLocalizer<SharedResources>
and use it. Like this:
public class HomeController : Controller
{
private readonly IStringLocalizer<HomeController> _localizer;
private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
public HomeController(IStringLocalizer<HomeController> localizer,
IStringLocalizer<SharedResource> sharedLocalizer)
{
_localizer = localizer;
_sharedLocalizer = sharedLocalizer;
}
public IActionResult Index()
{
ViewData["message"] = _sharedLocalizer["Hello!"];
return View();
}
}
Or in a view: @inject IViewLocalizer Localizer @inject IStringLocalizer SharedLocalizer
@{
ViewData["Title"] = SharedLocalizer["Hello!"];
}
<h1>@Localizer["Welcome message"]</h1>
I have skipped import and using statements for brevity.
You can find more info about SharedResource and general localization here: Microsoft Documentation or in an answer to the question I posted here My questions and answers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With