【问题标题】:C# edit resx file programmaticallyC# 以编程方式编辑 resx 文件
【发布时间】:2023-03-11 17:48:01
【问题描述】:

在我的应用程序中,我正在遍历具有某种文化的 resx 文件中的所有条目,在我的例子中是挪威语。然后我将这些字符串通过谷歌翻译 api 来获取瑞典语文本。

现在我正在尝试替换瑞典语 resx 文件中的瑞典语文本。有可能这样做吗?

这就是我获得瑞典语版本的方式

ResourceSet resourceSet = Texts.ResourceManager.GetResourceSet(new CultureInfo("nb-NO"), true, true);

foreach (DictionaryEntry entry in resourceSet){
    string resourceKey = entry.Key.ToString();
    string resource = entry.Value as string;
    arguements["q"] = resource;
    string result = Call(arguements);
}

所以我试图将瑞典语版本放入瑞典语资源文件中。有意见吗?

【问题讨论】:

标签: c# localization resx


【解决方案1】:

此完整示例基于现有资源文件创建新资源文件,并解析其值。

private void ProcessResource()
{
    var resxFile = @"..\..\Resource1.de-DE.resx";
    var destResxFile = @"..\..\Resource1.ru-RU.resx";

    using (var reader = new ResXResourceReader(resxFile))
    {
        using (var writer = new ResXResourceWriter(destResxFile))
        {
            foreach (DictionaryEntry entry in reader)
            {
                writer.AddResource(entry.Key.ToString(), Translate(entry.Value.ToString()));
            }
        }
    }
}

private string Translate(string value) => "translated " + value;

【讨论】:

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