I have the resx-files for localization in a portable-class-library in a class MyAppResources. So in code I can get localized strings just with:
View.FindViewById<Button>(Resource.Id.btnCacheClear).Text = MyAppResources.TextClearCache;
But is there also a way to set this string already in the axml?
<Button
android:id="@+id/btnCacheClear"
android:text= ?? />
Thx, Tom
I would imagine the cleanest way would be to define a custom attribute that would read from your PCL resources via a ResourceManager .
I am working with MvvmCross, and got it working with a custom language binding parser:
public class CustomLanguageBindingParser : MvxBindingParser , IMvxLanguageBindingParser
{
protected override MvxSerializableBindingDescription ParseBindingDescription()
{
this.SkipWhitespace();
string resourceName = (string)this.ReadValue();
// Pass the resource name in as the parameter on the StringResourceConverter.
return new MvxSerializableBindingDescription
{
Converter = "StringResource",
ConverterParameter = resourceName,
Path = null,
Mode = MvxBindingMode.OneTime
};
}
public string DefaultConverterName { get; set; }
public string DefaultTextSourceName { get; set; }
}
And a converter:
public class StringResourceConverter : IMvxValueConverter
{
private static ResourceManager manager = new ResourceManager(typeof(AppResources));
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// Ignore value. We are using parameter only.
return manager.GetString((string)parameter);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Register your parser in your MvxAndroidSetup class:
protected override void InitializeIoC()
{
base.InitializeIoC();
Mvx.RegisterType<IMvxLanguageBindingParser, CustomLanguageBindingParser>();
}
In your .axml, define a namespace xmlns:local="http://schemas.android.com/apk/res-auto"
And invoke the resource. For example on a TextView: local:MvxLang="Text MyResourceKey"
This hooks into the MvvmCross binding system. The first part "Text" determines the target property, while the second part is parsed as a resource key. The language binding parser translates this into a binding with your custom converter and the key as the converter parameter. The converter does the string lookup based on the converter parameter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With