I’m trying to find a good tutorial about asp.net localization.
In the official documentation, the explanation are about .resx files and I want to use json files.
If someone have a good tutorial on how to write the middleware to do that.
Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Nuget Package
https://www.nuget.org/packages/Askmethat.Aspnet.JsonLocalizer/
Solution
After some investigations, I finally find an example in Asp/Localization GitHub.
I provide here for people that wan’t to use a flat json without breaking default culture provider.
Data :
The flat json :
[
{
"Key": "Hello",
"LocalizedValue" : {
"fr-FR": "Bonjour",
"en-US": "Hello"
}
}
]
The C# model :
class JsonLocalization
{
public string Key { get; set; }
public Dictionary<string, string> LocalizedValue = new Dictionary<string, string>();
}
The Middleware
The Factory
This is just to have access to the CultureInfo is the StringLocalizer.
public class JsonStringLocalizerFactory : IStringLocalizerFactory
{
public IStringLocalizer Create(Type resourceSource)
{
return new JsonStringLocalizer();
}
public IStringLocalizer Create(string baseName, string location)
{
return new JsonStringLocalizer();
}
}
The Localizer
The logic to get the data from the JSON file
public class JsonStringLocalizer : IStringLocalizer
{
List<JsonLocalization> localization = new List<JsonLocalization>();
public JsonStringLocalizer()
{
//read all json file
JsonSerializer serializer = new JsonSerializer();
localization = JsonConvert.DeserializeObject<List<JsonLocalization>>(File.ReadAllText(@"localization.json"));
}
public LocalizedString this[string name]
{
get
{
var value = GetString(name);
return new LocalizedString(name, value ?? name, resourceNotFound: value == null);
}
}
public LocalizedString this[string name, params object[] arguments]
{
get
{
var format = GetString(name);
var value = string.Format(format ?? name, arguments);
return new LocalizedString(name, value, resourceNotFound: format == null);
}
}
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures)
{
return localization.Where(l => l.LocalizedValue.Keys.Any(lv => lv == CultureInfo.CurrentCulture.Name)).Select(l => new LocalizedString(l.Key, l.LocalizedValue[CultureInfo.CurrentCulture.Name], true));
}
public IStringLocalizer WithCulture(CultureInfo culture)
{
return new JsonStringLocalizer();
}
private string GetString(string name)
{
var query = localization.Where(l => l.LocalizedValue.Keys.Any(lv => lv == CultureInfo.CurrentCulture.Name));
var value = query.FirstOrDefault(l => l.Key == name);
return value.LocalizedValue[CultureInfo.CurrentCulture.Name];
}
}
With this solution you are able to use the basic IStringLocalizer in your Views and Controllers.
Of course if you have a big json file, you can use IMemoryCache or IDistributedMemoryCache to improve performance.
EDIT :
In the application Startup add this lines to use your own implementation :
services.AddSingleton<IStringLocalizerFactory, JsonStringLocalizerFactory>(); services.AddSingleton<IStringLocalizer, JsonStringLocalizer>(); services.AddLocalization(options => options.ResourcesPath = "Resources");
After that you can configure as you want your globalization preferences.
Method 2
Have a look at here, i didnt try it myself but it looks promising.
http://ronaldwildenberg.com/asp-net-core-localization-with-json-resource-files/
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0