We would like to enumerate all strings in a resource file in .NET (resx file). We want this to generate a javascript object containing all these key-value pairs. We do this now for satellite assemblies with code like this (this is VB.NET, but any example code is fine):
Dim rm As ResourceManager
rm = New ResourceManager([resource name], [your assembly])
Dim Rs As ResourceSet
Rs = rm.GetResourceSet(Thread.CurrentThread.CurrentCulture, True, True)
For Each Kvp As DictionaryEntry In Rs
[Write out Kvp.Key and Kvp.Value]
Next
However, we haven't found a way to do this for .resx files yet, sadly. How can we enumerate all localization strings in a resx file?
UPDATE:
Following Dennis Myren's comment and the ideas from here, I built a ResXResourceManager. Now I can do the same with .resx files as I did with the embedded resources. Here is the code. Note that Microsoft made a needed constructor private, so I use reflection to access it. You need full trust when using this.
Imports System.Globalization
Imports System.Reflection
Imports System.Resources
Imports System.Windows.Forms
Public Class ResXResourceManager
Inherits ResourceManager
Public Sub New(ByVal BaseName As String, ByVal ResourceDir As String)
Me.New(BaseName, ResourceDir, GetType(ResXResourceSet))
End Sub
Protected Sub New(ByVal BaseName As String, ByVal ResourceDir As String, ByVal UsingResourceSet As Type)
Dim BaseType As Type = Me.GetType().BaseType
Dim Flags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance
Dim Constructor As ConstructorInfo = BaseType.GetConstructor(Flags, Nothing, New Type() { GetType(String), GetType(String), GetType(Type) }, Nothing)
Constructor.Invoke(Me, Flags, Nothing, New Object() { BaseName, ResourceDir, UsingResourceSet }, Nothing)
End Sub
Protected Overrides Function GetResourceFileName(ByVal culture As CultureInfo) As String
Dim FileName As String
FileName = MyBase.GetResourceFileName(culture)
If FileName IsNot Nothing AndAlso FileName.Length > 10 Then
Return FileName.Substring(0, FileName.Length - 10) & ".resx"
End If
Return Nothing
End Function
End Class
-
Use System.Resources.ResXResourceReader (it's in System.Windows.Forms.dll)
Erik Hesselink : Will that automatically choose the right file based on locale (like the ResourceManager) or do I have to write my own logic for finding the right file?baretta : ResXResourceReader is more a low-level .resx XML file parser, than a resource manager. Maybe possible to use System.Web.Compilation.ResourceExpressionBuilder directly, i haven't tried it though. You would need something like ResourceManager.CreateFileBasedResourceManager, but for .resx files.. -
http://tonesdotnetblog.wordpress.com/2010/02/14/string-enumerations-and-resource-files-in-c/ has an alternative solution that involves putting attributes on enumerations that enable reading strings from resource files. This uses an alternative custom tool.
-
You can parse the .resx file for the appropriate key names and then request the resources for a particular culture in the normal way.
0 comments:
Post a Comment