【问题标题】:Resource Globalization using C# - Label and Error message使用 C# 进行资源全球化 - 标签和错误消息
【发布时间】:2021-11-17 13:07:45
【问题描述】:

我使用 .NET C# 4.7.2 框架在我的 winform 项目中使用 RESX 文件实现了全球化。当前的 RESX 文件用于标签,现在我需要另一个资源文件用于错误消息。

我不确定如何在同一个项目中创建不同的资源文件“ABC.Error.Resources.cs-CZ.resx”和“ABC.Label.Resources.cs-CZ.resx”作为资源仅为相应的语言标签创建 DLL。

我的解决方案结构是:

--Solution.sln
---Project.proj
----Resources(folder containing label resource files)
-----ABC.Label.Resources.cs-CZ.resx

感谢任何帮助

【问题讨论】:

  • 你应该阅读satellite assemblies
  • @MarkBenningfield,如何为同一种语言创建两个不同的文件(错误消息和标签消息)?

标签: c# globalization


【解决方案1】:

找到了解决上述问题的两种方法。

  • 方法 1

使用 .NET 框架的“ResourceManager”类中的“CreateFileBasedResourceManager

第 1 步:使用 .NET 框架中的“resgen.exe”将“.RESX”文件转换为“.resources”文件。

第2步:使用以下方法从资源文件中获取数据

static string ReadResourceValue(string file, string key)

        {

            string resourceValue = string.Empty;
            try
            {

                string resourceFile = file;

                string filePath =  "ConsoleApp1.en-GB.resources";

                ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
                // retrieve the value of the specified key
                CultureInfo ci = new CultureInfo("en-IN");
                resourceValue = resourceManager.GetString(key, ci);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                resourceValue = string.Empty;
            }
            return resourceValue;
        }
  • 方法 2

使用 .NET 框架的 System.Resources 类中的“ResXResourceSet”。

static string ReadResourceFromFile()
        {
            string resxFile = "Resources\ConsoleApp1.en-GB.resx";
            string retValue = string.Empty;
            using (ResXResourceSet resxSet = new ResXResourceSet(resxFile))
            {
                // Retrieve the string resource for the title.
                retValue = resxSet.GetString("Test1");
            }
            return retValue;
        }

希望这对将来的人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    相关资源
    最近更新 更多