0

From the outset of our project we've been storing strings in a resource file 'MyResources.resx', which was done to support the possibility of localizing the software in the future.

We've been using the following syntax for referencing the strings:

<TextBlock Text="{x:Static MyResources.Hello}" />

Is that correct, or should we be using the following:

<TextBlock Text="{Binding Source={x:Static MyResources.Hello}}" />

I've only recently come across the second syntax so I'm a bit concerned that what we've been using wouldn't actually change the text at runtime!

Also, is it okay to have the resx file in the main project, or should it reside in a project of its own? From the little I've seen about the localization process, it seems to involve generating a new DLL - is this a "full build" of the whole project, or does it (somehow) just extract the translated resx file(s) into the DLL?

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152

1 Answers1

1

For static localization, where you do not need to dynamically switch languages at runtime, your first example is the most straightforward solution. x:Static has relatively little overhead. It's nice and lightweight.

The second syntax won't make a difference in your case: the resource properties you are binding to do not raise change notifications, so the UI would not reflect any changes if you change MyResources.Culture at runtime. In either case, only newly created UI elements would reflect the new language.

If you do need to switch languages dynamically, neither of these approaches will suffice. There are some resources out there that can help you, but if you can get by with what you have, it'll make your life a lot easier. There's also a middle ground: you could create a custom MarkupExtension that takes in a text resource ID and provides localized text. Initially, it could simply delve into your resx resources, but you could potentially refactor it into a more dynamic solution later on (if and when you decide you need it).

From the little I've seen about the localization process, it seems to involve generating a new DLL - is this a "full build" of the whole project, or does it (somehow) just extract the translated resx file(s) into the DLL?

I believe what you're referring to is a satellite assembly. As I recall, satellite assemblies contain localized resources for a specific culture, but nothing more. You would bundle them with your main application or library, and the resource infrastructure will select which assembly to probe for resources based on the runtime CultureInfo.

Mike Strobel
  • 25,075
  • 57
  • 69
  • Many thanks. I won't need to switch language dynamically - the requirement would be able to have the UI translated based on the PC's regional settings. – Andrew Stephens Jan 10 '18 at 08:24