mohsinali1317
mohsinali1317

Reputation: 4425

C# edit resx file programmatically

In my app I am looping over all the entries from a resx file with a certain culture, in my case Norwegian. Then I put those strings through a google translator api to get the Swedish text.

Now I am trying to replace the Swedish text in the Swedish resx file. Is it possible to do so?

This is how I am getting the Swedish version

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;
    arguments["q"] = resource;
    string result = Call(arguments);
}

So I am trying to put the Swedish version into the Swedish resource file. Any views?

Upvotes: 0

Views: 1463

Answers (1)

sasha_gud
sasha_gud

Reputation: 1735

This complete sample creates new resource file based on existing one, parsing it's values.

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;

Upvotes: 3

Related Questions