Use lines in a .resw file directly in XAML

I know that the usual way of referencing localized strings from a .resw file would be this:

XAML:

<Button x:Uid="ButtonUid" /> 

Resources.resw:

 ButtonUid.Content = "Hello World" 

But is it possible to do so:

XAML (pseudo code):

 <Button Content = "$buttonLabel" /> 

Resources.resw:

 buttonLabel = "Hello World" 

The reason I want to do this, as in the second exapmle, is because it is an application that I port from iOS and Android to WP. I would like to convert the iOS or Android string file to .resw syntax, but not iterate over each line and add .Content or .Text or whatever it is used for. Is there an easy solution for this?

+2
source share
2 answers

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 .)

+2
source

You can use CustomXamlResourceLoader :

 public class XamlResourceLoader : CustomXamlResourceLoader { private readonly ResourceLoader _loader; public XamlResourceLoader() { _loader = ResourceLoader.GetForViewIndependentUse(); } protected override object GetResource(string resourceId, string objectType, string propertyName, string propertyType) { return _loader.GetString(resourceId) ?? resourceId; } } 

Then in your constructor App.xaml.cs:
CustomXamlResourceLoader.Current = new XamlResourceLoader();

And finally in your xaml:
<Button Content = "{CustomResource buttonLabel}" />

+2
source

Source: https://habr.com/ru/post/1013345/


All Articles