【问题标题】:.NET core 1.1 and localization in runtime.NET core 1.1 和运行时本地化
【发布时间】:2017-08-24 21:58:14
【问题描述】:

在我正在开发的应用程序中,需要本地化输出消息。 在我的 web api 项目中,我通过 .net 核心的全球化/本地化功能取得了成功。每个用户在数据库中都有一个特定的语言环境,因此通过自定义 RequestCultureProvider,我设法访问用户的主体,获取他的语言并更改 RequestCultureProvider。 但有时我会运行向用户发送推送通知消息的程序。例如:

    foreach(var user in users) 
        await SendNotification("message", user.DeviceId)

如果我像这样重写:await SendNotification(localizer["message"], user.DeviceId) 那么每个用户都会有相同的“消息”翻译。是否可以在运行时根据 user.cultureInfo 更改本地化程序?

谢谢

【问题讨论】:

    标签: c# asp.net-core asp.net-core-webapi


    【解决方案1】:

    当然可以。

    您只需要使用.WithCulture(...) 调用本地化程序

    foreach(var user in users)
        await SendNotification(localizer.WithCulture(new Culture("en-US"))["message"], user.DeviceId)
    

    当然用包含文化的变量或属性替换en-US,即

    foreach(var user in users)
        await SendNotification(localizer.WithCulture(new Culture(user.CultureInfo))["message"], user.DeviceId)
    

    【讨论】:

      猜你喜欢
      • 2017-09-02
      • 2017-08-18
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      相关资源
      最近更新 更多