使用 json 文件进行 Asp.net Core 本地化[已关闭]

问题描述 投票:0回答:2

我正在尝试寻找有关 ASP.NET 本地化的好教程。 在官方文档中,解释是关于.resx文件,我想使用json文件。

如果有人有关于如何编写中间件来做到这一点的好教程。

谢谢

c# asp.net json asp.net-core localization
2个回答
30
投票

Nuget 包

https://www.nuget.org/packages/Askmethat.Aspnet.JsonLocalizer/

解决方案

经过一番调查,我终于在Asp/Localization GitHub中找到了一个例子。

我在这里为那些想要使用平面 JSON 而不破坏默认文化提供程序的人提供。

数据:

扁平 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];
    }
}

通过此解决方案,您可以在ViewsControllers中使用基本的IStringLocalizer

当然如果你有一个大的json文件,你可以使用IMemoryCacheIDistributedMemoryCache来提高性能。

编辑:

在应用程序启动中添加以下行以使用您自己的实现:

services.AddSingleton<IStringLocalizerFactory, JsonStringLocalizerFactory>();
services.AddSingleton<IStringLocalizer, JsonStringLocalizer>();
services.AddLocalization(options => options.ResourcesPath = "Resources");

之后,您可以根据需要配置全球化首选项。


2
投票

看看这里,我自己没有尝试过,但看起来很有希望。

http://ronaldwildenberg.com/asp-net-core-localization-with-json-resource-files/

© www.soinside.com 2019 - 2024. All rights reserved.