Handling translation in code
There are scenarios in which you need to retrieve a translated string directly in the code of the application (for example, when you need to localize the message of a popup dialog). In this scenario, things are slightly more complicated. Let's understand the reasoning a bit more.
When you enable localization using the special x:Uid
property, WinUI generates an instance of a class called ResourceManager
under the hood, which is responsible for properly loading the resources file and retrieving the correct resource based on the value of the property. Unfortunately, at the time of writing, WinUI doesn't expose the ResourceManager
instance, which is generated behind the scenes, making it impossible to use it to load translations in code.
As such, we have to create and maintain our own instance of the ResourceManager
class and use it to retrieve localized strings from a resource file. The easiest way to achieve this goal is to create a helper...