I know the usual way to reference localized strings from a .resw file would be like this:
XAML:
<Button x:Uid="ButtonUid" />
Resources.resw:
ButtonUid.Content = "Hello World"
But is it also possible to kind of do it like this:
XAML (pseudo code):
<Button Content = "$buttonLabel" />
Resources.resw:
buttonLabel = "Hello World"
The reason why I want to do it like in the second exapmle is because this is an app that I'm porting from iOS and Android to WP. I'd like to convert the iOS or Android strings file to .resw syntax but without going over every single string and adding .Content or .Text or whatever it is used for. Is there an easy solution for that?
You can use a 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 App.xaml.cs constructor:CustomXamlResourceLoader.Current = new XamlResourceLoader();
And finally in your xaml:<Button Content = "{CustomResource buttonLabel}" />
I once did something similar, where we added any new strings to the Android string resource file, then used custom build tools convert those into iOS and Windows formats.
An Android string might look like this:
<string name="Hello">Hello, World!</string>
Our tool converts this into a Windows string resource:
<data name="Hello">
<value>Hello, World!</value>
</data>
Next, add a converter that does nothing to the provided value, but instead assumes its parameter is a resource id:
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 that converter available to your XAML, perhaps something like this:
<Page.Resources>
<local:LocalizeConverter x:Key="LocalizeConverter" />
</Page.Resources>
And finally, set your Button
's Content
property as follows:
<Button
Content="{x:Bind Converter={StaticResource LocalizeConverter}, ConverterParameter=Hello, Mode=OneTime}"
/>
Note that we don't supply any value to the converter. (In WPF I would have created a markup extension. Sadly, this option is not available in UWP, so I came up with this value-less converter option as an alternative.)
If you want to get even niftier, consider this:
<Button
Content="{x:Bind Language, Converter={StaticResource LocalizeConverter}, ConverterParameter=Hello, Mode=OneWay}"
/>
This lets you change the language on the fly, if you have resources localized into other languages. (Note Mode=OneWay
instead of Mode=OneTime
.)
来源:https://stackoverflow.com/questions/40767143/use-strings-in-resw-file-directly-in-xaml