8

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.

Tseng
  • 52,202
  • 10
  • 166
  • 183
Biserka Zinreich
  • 209
  • 1
  • 10
  • That's a good question. Try posting this question on the aspnet/MVC repo on GitHub – Kiran Challa Aug 11 '16 at 22:31
  • the default behavior in previous .NET frameworks/stacks was to have a resource file without a culture in it, i.e. `Resource.resx`. Also please note that when there is no suitable locale, the string put into `@Localizer["string"]` will be returned, which is also used in the official samples iirc: Means: instead of message strings they suggest directly use the default string as "keys", i.e. `@Localizer["This is an default locale string"]` and override this via locale. Example: https://github.com/aspnet/Localization/blob/dev/samples/LocalizationSample/Startup.cs#L74 – Tseng Aug 12 '16 at 00:15
  • I know about using the default string as the key, but that does not help me with localizing repetitive phrases at all! That is exactly what I don't want to do. I don't want to override the phrase in a locale for a specific view because it repeates in most of my views. I want a default resource file for all views where the reptitive phrases go, so I'm interested in the Resource.resx you mentioned. I would like a default resource.resx for each lanaguage. – Biserka Zinreich Aug 12 '16 at 09:14
  • Thanks for the suggestion Kiran Challa. I just did so. – Biserka Zinreich Aug 12 '16 at 10:09

1 Answers1

3

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.

Community
  • 1
  • 1
Daniel Aaron Salwerowicz
  • 1,713
  • 2
  • 11
  • 16