【问题标题】:multiple resx files for 1 culture1 种文化的多个 resx 文件
【发布时间】:2016-08-08 22:29:47
【问题描述】:

我有一个需要支持多种文化类型的 c# 应用程序。我为每种语言制作了一个 resx 文件,更改文化类型会更改我正在使用的 resx 文件。效果很好。 现在我有一个客户不喜欢我使用的标签。他们处于 en-US 文化中,我想保持 en-US 的 resx 不变以及我们大多数客户的方式,但是对于这个特定的客户,有没有办法更改他的资源文件虽然仍然是 en-US 的一部分? 例如,我可以制作一个“en-US2”resx 文件或类似的文件,然后指向它吗?或者有没有更好的方法来为同一种语言创建多个不同的 resx 文件?

【问题讨论】:

  • CultureAndRegionInfoBuilder 可能是您所追求的,但请注意自定义文化 has to be registered on the target machine
  • 1) 您可能希望为特殊客户使用多种资源。就像 MainResource.resx、MainResource.en-US.resx 用于每个人,而 SpecialCase.resx、SpecialCase.en-US.resx 用于某些特定的人。 2) 或者如果您控制此特定客户端的部署,您可以交换资源 =) 如果您真的想要答案,请在您的问题中添加更多信息。
  • 或者您可以在客户端的机器上直接编辑。

标签: c# localization globalization resx currentculture


【解决方案1】:

我曾以相同的想法问过一个类似的问题(here.) 基本上,C# 旨在与单个 Resources.resx 一起使用,您可以在其中培养它。根据我从我周围的所有狩猎中收集到的信息,您有两个选择:将它们全部放在一个文件中(如 workersWelcomeText 和 employeeWelcomeText)或创建多个 resx 文件并构建一个模型来处理它们并返回您需要的资源文件:

模型(不是必需的,但很好的做法):

namespace Bot.Models
{
    public class Dialog
    {
        internal static string Name { get; set; }

        internal static string Culture { get; set; }

        //Returns the rsource file name.culture.resx
        internal static string ResourceFile { get { return $"{System.AppDomain.CurrentDomain.BaseDirectory}Properties\\{Name}.
{Culture}.resx"; } }
    }
}

构造一个从文件中检索资源的方法:

    // You can do this in-line, just remember to reset your resex if you change the file
    internal static string GetStrRes(string resourceStr)
    {
        //Gets your resource file
        System.Resources.ResXResourceSet resxSet = new System.Resources.ResXResourceSet(Models.Dialog.ResourceFile);

        string response = resxSet.GetString(resourceStr);

        return response;
    }

设置模型参数:

Models.Dialog.Name = "Worker";
Models.Dialog.Culture = "en-US";

使用模型:

// Returns content of string resource key, same as Resources.workerText
await context.PostAsync(BotUtils.GetStrRes("RES_KEY"));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    相关资源
    最近更新 更多