[Feature] ResourceString Markup Extension for easy localization #4622
Replies: 3 comments
-
Hey @HerrickSpencer, thank you for your new contributions! 😄 I like the idea of offering an extension like this, but I'm not convinced that a markup extension is the right solution for this. Specifically, markup extensions add quite a lot of overhead, as each one needs to be instantiated, plus other things. My idea to solve this would be to instead fully embrace the "new" UWP stuff like compiled bindings, and using binding to functions instead. Consider something like this to replicate the same snippet you shared in your post: <Button Content="{x:Bind helpers:Resources.Get('ButtonText')}"/> This would result in perfect codegen (same as doing it manually in code behind), and no allocations. |
Beta Was this translation helpful? Give feedback.
-
Great thoughts @Sergio0694 This was discussed in the blog post as well... I like the idea of both options, so I've created a static function to allow users to choose either. I've also updated the Sample app page to show this as well. |
Beta Was this translation helpful? Give feedback.
-
@HerrickSpencer Secondly, There is no way to dynamically convert all strings from language to another directly without shutting down the application |
Beta Was this translation helpful? Give feedback.
-
Describe the problem this feature would solve
Microsoft’s current documentation shows examples of how to globalize your UI facing strings in UWP/WPF XAML by using the Uid as a lookup in an included resx file. This technique works great, but does have some limitations.
Commonly used resource string technique:
<Button x:Uid="MyButton"/>
In a blog post I wrote for #ifdef, I described the way some of our In-Box apps are handling resources with a markup extension. I'd like to add a version of that extension to the toolkit, and add some better features to it as well.
Describe the solution
With the markup extension:
<Button Content="{str:ResourceString Name=ButtonText}"/>
As you see we are using a custom markup extension to bind your content strings. We are able to simply pass in the name of the resource we want to use and get the localized string back.
Now our resource file need only contain simple names without the properties, making it much cleaner and easier to read.
One feature I'm adding to this is the ability to specify a language as well if the developer wanted to get resources from another language context.
Describe alternatives you've considered
Above I show the usage of the Uid methodology, but this limits developers to one string per Uid and forces the use of property names in resource files.
Beta Was this translation helpful? Give feedback.
All reactions