MHTri
MHTri

Reputation: 910

Is there a way to have "general" resx files?

I've a bunch of user interface elements that re-use translated terms e.g. buttons with "click here" or "view more" in Japanese, French, German, etc.

Is is possible to have resx files where I can store such terms and reuse anywhere, not just on the page (and culture) it's named against?

Upvotes: 1

Views: 256

Answers (1)

NineBerry
NineBerry

Reputation: 28519

This answer explains how to use shared resources using the .net core way using localizers and dependency injection.

This is explained in detail on here: Globalization and localization in ASP.NET Core

Basically, you create an empty class anywhere in your project with a name you want to use to group the resources:

public class SharedResource
{
}

You can also have multiple classes to have different groups like

public class CommonButtonTexts
{
}

public class CommonErrorMessages
{
}

If you then want to use a text, you reference a Localizer for the given class. Example in C# Code like in a Controller or some helper classes:

public class HomeController : Controller
{
    private readonly IStringLocalizer<SharedResource> _sharedLocalizer;
    private readonly IStringLocalizer<CommonButtonTexts> _commonButtonTextsLocalizer;
    private readonly IStringLocalizer<CommonErrorMessages> _commonErrorMessagesLocalizer;

    // Localizers are created via Dependency Injection
    public HomeController(IStringLocalizer<SharedResource> sharedLocalizer, IStringLocalizer<CommonButtonTexts> commonButtonTextsLocalizer,
        IStringLocalizer<CommonErrorMessages> commonErrorMessagesLocalizer)
    {
        _sharedLocalizer = sharedLocalizer;
        _commonButtonTextsLocalizer = commonButtonTextsLocalizer;
        _commonErrorMessagesLocalizer = commonErrorMessagesLocalizer;
    }

    public IActionResult Index()
    {
        string someString = _sharedLocalizer["ProductName"];
        string yesButtonCaption = _commonButtonTextsLocalizer["Yes"];
        string overworkedError = _commonErrorMessagesLocalizer["The programmer was overworked and didn't finish this code."];

        // Do something with the strings

        return View();
    }
}

Example for using it from a Razor view:

@using Microsoft.Extensions.Localization;

@inject IStringLocalizer<SharedResource> SharedLocalizer
@inject IStringLocalizer<CommonButtonTexts> CommonButtonTextsLocalizer
@inject IStringLocalizer<CommonErrorMessages> CommonErrorMessagesLocalizer

<p>@SharedLocalizer["ProductName"]</p>
<p>@CommonButtonTextsLocalizer["Yes"]</p>
<p>@CommonErrorMessagesLocalizer["The programmer was overworked and didn't finish this code."]</p>

The translations will then be looked up in Resource files based on the empty classes we use. For example, in the above sample, the translations for english will be taken from SharedResource.en.resx, CommonButtonTexts.en.resx and CommonErrorMessages.en.resx. (Place the *.resx files in the same directory as the other view-specific resource files).

Upvotes: 1

Related Questions