Search code examples
c#localizationasp.net-core-localization

c# dynamically inject translations (resources) into IStringLocalizer


I have a Blazor app in which I use localization following this. In addition to my resx files I have in my solution I would like to be able to load new translation options for a combobox/dropdown from a database. (The database will be updated from time to time with new options, but probably not very many languages will be added)

Is there a way to accomplish this? perhaps something along the line of:

localizer.AddResource("en-GB","fish","fish");
localizer.AddResource("da-DK","fish","fisk");

Solution

  • This is not possible with the default localization infrastructure! You can develop your own localization library that implements the IStringLocalizer interface... or you can use a nuget like XLocalizer.

    With XLocalizer you can setup datasource as DB, XML or RESX. DB and XML provides ability to update resources during runtime. RESX can't be updated during runtime, however you need to restart the app in order for changes to be available for RESX files.

    It is also worth to mention that XLocalizer offers online translation support from different providers like google translate, IBM Watson, My Memory, etc.

    Here is the docs for XLocalizer: https://docs.ziyad.info/en/XLocalizer/v1.0/index.md

    Samples for XML, DB and RESX: https://github.com/LazZiya/XLocalizer.Samples