問題描述
我正在努力尋找一個關于 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.
謝謝
推薦答案
Nuget 包
https://www.nuget.org/packages/Askmethat.Aspnet.JsonLocalizer/
解決方案
經過一番調查,終于在Asp/Localization GitHub中找到了一個例子.
After some investigations, I finally find an example in Asp/Localization GitHub.
我在這里為那些不想在不破壞默認文化提供程序的情況下使用平面 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];
}
}
通過這個解決方案,您可以在 Views 和 Controllers 中使用基本的 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 本地化的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!