带有json文件的Asp.net Core本地化 [英] Asp.net Core localization with json files
问题描述
我正在尝试找到一个很好的有关asp.net本地化的教程. 在官方文档中,解释是关于 .resx 文件的,我想使用json文件.
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.
谢谢
推荐答案
Nutt软件包
https://www.nuget.org/packages/Askmethat.Aspnet.JsonLocalizer /
解决方案
经过一些调查,我终于在Asp/Localization GitHub中找到一个示例.
After some investigations, I finally find an example in Asp/Localization GitHub.
我在这里为不使用默认json的情况下不使用平面json的人提供
I provide here for people that wan't to use a flat json without breaking default culture provider.
数据:
平面json:
[
{
"Key": "Hello",
"LocalizedValue" : {
"fr-FR": "Bonjour",
"en-US": "Hello"
}
}
]
C#模型:
class JsonLocalization
{
public string Key { get; set; }
public Dictionary<string, string> LocalizedValue = new Dictionary<string, string>();
}
中间件
工厂
这只是要访问CultureInfo的StringLocalizer.
public class JsonStringLocalizerFactory : IStringLocalizerFactory
{
public IStringLocalizer Create(Type resourceSource)
{
return new JsonStringLocalizer();
}
public IStringLocalizer Create(string baseName, string location)
{
return new JsonStringLocalizer();
}
}
本地化程序
从JSON文件获取数据的逻辑
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];
}
}
使用此解决方案,您可以在视图和控制器中使用基本的 IStringLocalizer .
With this solution you are able to use the basic IStringLocalizer in your Views and Controllers.
当然,如果您有一个很大的json文件,则可以使用 IMemoryCache 或 IDistributedMemoryCache 来提高性能.
Of course if you have a big json file, you can use IMemoryCache or IDistributedMemoryCache to improve performance.
在应用程序启动中,添加以下行以使用您自己的实现:
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.
这篇关于带有json文件的Asp.net Core本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!