Localization file for Blazor with multiple .resx per language

Viewed 57

I am implementing the localization for a Blazor WebAssembly app. and everything works fine with one .resx file. However, I am wondering if it is possible to use multiple .resx for same language, I tried to look in the documentation but it is somehow not mentioned anywhere, and all tutotial or documentation end-up calling the unique resource with:

@inject IStringLocalizer<LocalizationRes> _L

So, is it possible to use multiple .resx for the same language?if yes, how the IStringLocalizer should be used in that case ?

Thank you

1 Answers

You can use multiple Resx files for same language, and distribute them along your project structure. i.e. if you have a folder structure like this

MainFolder
..Cats
....Cats.razor.cs
....Cats.razor
Resources
..Cats
....Cats.en.resx <- english resource
....Cats.es.resx <- spanish resource

in your injection you should use as following:

@inject Microsoft.Extensions.Localization.IStringLocalizer _localizer

to use you implement as i.e.:

<TitleComponent Title="@_localizer["Cats"]" Description="@_localizer["All your Cats."]" />

in the resx you should provide a translation for each implementation and once you propagate the culture change in your app it will take the correct resx file.

_localizer["Cats"].

this to support more than one language, to have multiple files of the same language just apply the same structure along your project i.e.

MainFolder
..Cats
....Cats.razor.cs
....Cats.razor
..Dogs
....Dogs.razor.cs
....Dogs.razor
Resources
..Cats
....Cats.en.resx <- english resource
....Cats.es.resx <- spanish resource
..Dogs
....Dogs.en.resx
....Dogs.es.resx
Related