How to get the comment from a .resx file entry
Asked Answered
R

2

17

Strings in resource files have a name, a value and a comment
The ResXResourceReader class gives me access to the name and the value.
Is there a way to read the comment?

Rigorous answered 13/1, 2012 at 17:6 Comment(0)
S
21

You should be able to get Comment via ResXDataNode class: http://msdn.microsoft.com/en-us/library/system.resources.resxdatanode.aspx

You will need to set UseResXDataNodes flag on the reader: http://msdn.microsoft.com/en-us/library/system.resources.resxresourcereader.useresxdatanodes.aspx

Subdue answered 13/1, 2012 at 17:15 Comment(0)
T
2

Necromancing.
If the link goes dark - with UseResXDataNodes = true:

public static string ReadRessourceFile()
{
    string[] languages = new string[] { "de", "fr", "it", "en" };
    string pathPattern = System.AppDomain.CurrentDomain.BaseDirectory;
    pathPattern = System.IO.Path.Combine(pathPattern, "..", "..", "..", "libQrCodeGenerator", "Resources", "QRBillText-{0}.resx");
    pathPattern = System.IO.Path.GetFullPath(pathPattern);

    System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>> dict = new System.Collections.Generic.Dictionary<string, System.Collections.Generic.Dictionary<string, string>>(System.StringComparer.InvariantCultureIgnoreCase);

    foreach (string lang in languages)
    {
        dict[lang] = new System.Collections.Generic.Dictionary<string, string>(System.StringComparer.InvariantCultureIgnoreCase);

        string file = string.Format(pathPattern, lang);
        System.Resources.ResXResourceReader rr = new System.Resources.ResXResourceReader(file);
        rr.UseResXDataNodes = true;

        // '# Iterate through the resources and display the contents to the console. 
        foreach (System.Collections.DictionaryEntry d in rr)
        {
            System.Resources.ResXDataNode node = (System.Resources.ResXDataNode)d.Value;
            string value = (string) node.GetValue((System.ComponentModel.Design.ITypeResolutionService)null);
            string comment = node.Comment;

            if(!string.IsNullOrEmpty(comment))
            {
                System.Console.WriteLine(comment);
            }

            // dict[lang][d.Key.ToString()] = d.Value.ToString(); // when not using UseResXDataNodes = true
            dict[lang][d.Key.ToString()] = value;
        }

        // '#Close the reader. 
        rr.Close();
    }

    string json = Newtonsoft.Json.JsonConvert.SerializeObject(dict, Newtonsoft.Json.Formatting.Indented);
    return json;
}
Tenerife answered 24/3, 2022 at 10:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.