I once did something similar, where we added new lines to the Android string resource file, and then used custom build tools that convert them to iOS and Windows formats.
Android string might look like this:
<string name="Hello">Hello, World!</string>
Our tool converts this to a Windows string resource:
<data name="Hello"> <value>Hello, World!</value> </data>
Then add a converter that does nothing with the provided value, but instead assumes that its parameter is a resource identifier:
public sealed class LocalizeConverter : IValueConverter { private static readonly ResourceLoader Loader = ResourceLoader.GetForViewIndependentUse("/Resources"); public object Convert(object value, Type targetType, object parameter, string language) { string resourceId = parameter as string; return !string.IsNullOrEmpty(resourceId) ? Loader.GetString(resourceId) : DependencyProperty.UnsetValue; } public object ConvertBack(object value, Type targetType, object parameter, string language) { throw new NotSupportedException(); } }
Now make this converter available for your XAML, perhaps something like this:
<Page.Resources> <local:LocalizeConverter x:Key="LocalizeConverter" /> </Page.Resources>
Finally, set the Button
Content
property as follows:
<Button Content="{x:Bind Converter={StaticResource LocalizeConverter}, ConverterParameter=Hello, Mode=OneTime}" />
Please note that we do not pass the value to the converter. (In WPF, I would create a markup extension. Unfortunately, this option is not available in UWP, so I chose this converter option without a value.)
If you want to get an even higher level, consider the following:
<Button Content="{x:Bind Language, Converter={StaticResource LocalizeConverter}, ConverterParameter=Hello, Mode=OneWay}" />
This allows you to change the language on the fly if you have resources localized in other languages. (Note Mode=OneWay
instead of Mode=OneTime
.)