Archive

Posts Tagged ‘design time’

Localization Revisited

May 31, 2012 Leave a comment

Among the first things I covered in this blog was Localization. After I had finished those posts, I felt like I had covered everything one would need to write fully globalized applications. However, meanwhile I found out how to do some things better, and I’d like to share that here.

Making Localization Work at Design Time

One of the drawbacks of the original WPFGlue localization components was that they didn’t work nicely at design time. The reason was that I hadn’t found out how to automatically locate an application’s resources when the application was hosted inside the VS XAML designer.

The key here is a property on the Application class named ResourceAssembly. When the VS designer hosts an assembly, it will put a reference to it into this property, so the MarkupExtensions can locate the assembly that contains the current project’s resources.

Also, I revised the detection for the resource basename. The basename contains the project’s root namespace. One can find this as the namespace of the project’s Application class, which will normally be the project’s only class derived of System.Windows.Application.

I encapsulated this all into a new base class for the localization markup extensions:

Namespace Localization
    Public MustInherit Class ResourceExtension
        Inherits System.Windows.Markup.MarkupExtension

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal key As String)
            MyBase.New()
            Me.Key = key
        End Sub

        Private _Key As String
        Public Property Key() As String
            Get
                If String.IsNullOrEmpty(_Key) Then
                    Return "No Key"
                Else
                    Return _Key
                End If
            End Get
            Set(ByVal value As String)
                _Key = value
            End Set
        End Property

        Private Shared _BaseName As String
        Public Shared Property BaseName() As String
            Get
                If _BaseName Is Nothing And Assembly IsNot Nothing Then
                    Dim applicationType As Type = (Aggregate t As Type In Assembly.GetTypes() Where t.IsSubclassOf(GetType(System.Windows.Application)) Into First())
                    If applicationType IsNot Nothing Then
                        _BaseName = applicationType.Namespace & ".Resources"
                    End If
                End If
                Return _BaseName
            End Get
            Set(ByVal value As String)
                _BaseName = value
                _ResourceManager = Nothing
            End Set
        End Property

        Private Shared _Assembly As System.Reflection.Assembly
        Public Shared Property Assembly() As System.Reflection.Assembly
            Get
                If _Assembly Is Nothing Then
                    _Assembly = System.Reflection.Assembly.GetEntryAssembly
                End If
                If _Assembly Is Nothing Then
                    _Assembly = Application.ResourceAssembly
                End If
                Return _Assembly
            End Get
            Set(ByVal value As System.Reflection.Assembly)
                _Assembly = value
                _ResourceManager = Nothing
            End Set
        End Property

        Private Shared _ResourceManager As System.Resources.ResourceManager
        Public Shared Property ResourceManager() As System.Resources.ResourceManager
            Get
                If _ResourceManager Is Nothing Then
                    If Assembly IsNot Nothing And BaseName IsNot Nothing Then
                        _ResourceManager = New System.Resources.ResourceManager(BaseName, Assembly)
                    End If
                End If
                Return _ResourceManager
            End Get
            Set(ByVal value As System.Resources.ResourceManager)
                _ResourceManager = value
            End Set
        End Property
    End Class
End Namespace

 

Notice the fallback detection for the resource assembly: the first location is the application domain’s entry assembly. If the assembly isn’t running it’s own domain (because it’s hosted in the designer), the next location is the ResourceAssembly. As before, one can override the resource assembly by setting the Localization.ResourceAssembly attached property.

By the way, in the post "The Last Word on Localized Resources" I suggested using a localized URL in order to use different localized ResourceDictionaries in ones XAML files. I don’t recommend this approach any longer. While it works at run time, the designer experience is bad: one will get squiggly lines telling that the resources can’t be found, and one cannot see the values of the resources in the designer panel. With the new markup extensions based on the above class, one will have instant feedback.

Funny enough, VS will use the set of resources that match its own language version. So, running US-English Microsoft Visual Basic 2010 Express on German Windows, I see the English localization in the designer and the German localization when I start the debugger.

How to Use Bitmap Resources

Another thing I learned is how to convert from System.Drawing.Bitmap, which is what one gets from the ResourceManager when one accesses an image resource, to System.Windows.Media.Imaging.BitmapSource, which is what the WPF Image control expects:

Public Overrides Function ProvideValue(ByVal serviceProvider As System.IServiceProvider) As Object
    Dim result As BitmapSource = NoImage
    If ResourceManager IsNot Nothing Then
        Dim data As System.Drawing.Bitmap = TryCast(ResourceManager.GetObject(Key), System.Drawing.Bitmap)
        If data IsNot Nothing Then
            Dim sourceRect = New Int32Rect(0, 0, data.Width, data.Height)
            result = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(data.GetHbitmap, Nothing, sourceRect, BitmapSizeOptions.FromEmptyOptions)
        End If
    End If
    Return result
End Function

 

The conversion happens through a shared bitmap handle, so I assume it’s not too heavy on memory consumption. As a nice side effect, one can now use PNG images without them being unaccountably scaled to 133% their pixel size. Also, the images will now instantly be visible in the designer.

How to Create Alternative Resource Files

In my first post about localized resources, I described a kind of complicated procedure for creating resource files for alternative languages. A somewhat simplified procedure, which goes without closing the IDE, is as follows:

  • Complete your application, creating resources in the My Project / Resources designer as you go.
  • When done, activate the “Show All Files” icon in Solution Explorer.
  • Locate the Resources.resx file in the My Project folder and create a copy of it. You can do this using the context menu in Solution Explorer. The copy can be located anywhere in your project directory. Probably you should keep it outside the My Project folder.
  • Rename the copy to reflect the intended language and culture, e.g. to Resources.de.resx for general German, or Resources.ar-SA.resx for Arabic (Saudi Arabia). At that stage, you can deactivate the “Show All Files” switch again.
  • Double click the file in order to open it in the resource designer.
  • Make sure that the Access Modifier is set to “No Code Generation”.
  • Replace the original resource strings and icons with the appropriate translations.
  • Make sure that the Neutral Language in My Project / Application / Assembly Information is set to the language you used for the original version of the resources.
  •  

    Conclusion

    While support for .Net resources is completely missing from WPF, one can create an experience that feels equally nice at design time and at run time by using some simple custom markup extensions.