ASP.NET *.resx 序列化

发布于 2024-09-01 11:07:59 字数 221 浏览 6 评论 0原文

我正在为一个 I18N 项目做出贡献,并且有一个调用将我们的 *.resx 文件序列化为 JSON 对象(无论出于何种原因)。

我想知道的是:

  • 有没有办法获取给定 *.resx 文件的所有有效键的列表,以便我们可以使用 HttpContext.GetGlobalResourceObject 来获取令牌?
  • 如果这行不通,有没有人想出一个聪明的解决方案?

I'm contributing on an I18N project and there's a call to serialize our *.resx files as JSON objects (for whatever reason).

What I'm wondering is:

  • Is there a way to get a list of all of the valid keys for a given *.resx file so that we could use HttpContext.GetGlobalResourceObject to grab the tokens?
  • If that won't work, has anyone come up with a clever solution that does?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微暖i 2024-09-08 11:07:59
  Sub ReadRessourceFile()
       ''#Requires Assembly System.Windows.Forms 
        Dim rsxr As System.Resources.ResXResourceReader = New System.Resources.ResXResourceReader("items.resx")

        ''# Iterate through the resources and display the contents to the console.    
        Dim d As System.Collections.DictionaryEntry
        For Each d In rsxr
            Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString())
        Next d

        ''#Close the reader. 
        rsxr.Close()
    End Sub

然后您需要将其添加到可序列化字典中,然后您可以使用 System.Web.Extensions.dll 将其序列化为 JSON

Public Class JSONHelper

Public Shared Function Serialize(Of T)(ByVal obj As T) As String
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()  
    Return JSONserializer.Serialize(obj)
End Function

Public Shared Function Deserialize(Of T)(ByVal json As String) As T
    Dim obj As T = Activator.CreateInstance(Of T)()
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
    obj = JSONserializer.Deserialize(Of T)(json)
    Return obj
End Function

End Class

编辑:C# :

public void ReadRessourceFile()
{
    //Requires Assembly System.Windows.Forms '
    System.Resources.ResXResourceReader rsxr = new System.Resources.ResXResourceReader("items.resx");

    // Iterate through the resources and display the contents to the console. '    
    System.Collections.DictionaryEntry d = default(System.Collections.DictionaryEntry);
    foreach (DictionaryEntry d_loopVariable in rsxr) {
        d = d_loopVariable;
        Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString());
    }

    //Close the reader. '
    rsxr.Close();
}

和 JSON 帮助器:

public class JSONHelper
{

    public static string Serialize<T>(T obj)
    {
        System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        return JSONserializer.Serialize(obj);
    }

    public static T Deserialize<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        obj = JSONserializer.Deserialize<T>(json);
        return obj;
    }

}

此外,如果您还想获取注释,您可以设置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;
}
  Sub ReadRessourceFile()
       ''#Requires Assembly System.Windows.Forms 
        Dim rsxr As System.Resources.ResXResourceReader = New System.Resources.ResXResourceReader("items.resx")

        ''# Iterate through the resources and display the contents to the console.    
        Dim d As System.Collections.DictionaryEntry
        For Each d In rsxr
            Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString())
        Next d

        ''#Close the reader. 
        rsxr.Close()
    End Sub

Then you need to add this to a Serializable Dictionary, which you can then serialize to JSON using System.Web.Extensions.dll

Public Class JSONHelper

Public Shared Function Serialize(Of T)(ByVal obj As T) As String
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()  
    Return JSONserializer.Serialize(obj)
End Function

Public Shared Function Deserialize(Of T)(ByVal json As String) As T
    Dim obj As T = Activator.CreateInstance(Of T)()
    Dim JSONserializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
    obj = JSONserializer.Deserialize(Of T)(json)
    Return obj
End Function

End Class

Edit: C# :

public void ReadRessourceFile()
{
    //Requires Assembly System.Windows.Forms '
    System.Resources.ResXResourceReader rsxr = new System.Resources.ResXResourceReader("items.resx");

    // Iterate through the resources and display the contents to the console. '    
    System.Collections.DictionaryEntry d = default(System.Collections.DictionaryEntry);
    foreach (DictionaryEntry d_loopVariable in rsxr) {
        d = d_loopVariable;
        Console.WriteLine(d.Key.ToString() + ":" + ControlChars.Tab + d.Value.ToString());
    }

    //Close the reader. '
    rsxr.Close();
}

And the JSON helper:

public class JSONHelper
{

    public static string Serialize<T>(T obj)
    {
        System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        return JSONserializer.Serialize(obj);
    }

    public static T Deserialize<T>(string json)
    {
        T obj = Activator.CreateInstance<T>();
        System.Web.Script.Serialization.JavaScriptSerializer JSONserializer = new System.Web.Script.Serialization.JavaScriptSerializer();
        obj = JSONserializer.Deserialize<T>(json);
        return obj;
    }

}

Also, if you want to get the comment as well, you can set UseResXDataNodes to true.
Example:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文