I'm using the TranslateExtension
from Xamarin. Is it possible to add a StringFormat
to the call?
Currently, I have
<Label Text="{i18n:Translate User}" />
but I would need something like this
<Label Text="{i18n:Translate User, StringFormat='{0}:'}" />
If I do the latter, I get
Xamarin.Forms.Xaml.XamlParseException: Cannot assign property "StringFormat": Property does not exists, or is not assignable, or mismatching type between value and property
I know I could add another translation with a colon, but it would be nice to have a different option.
You can add a parameter property to TranslateExtension.
My TranslateExtension looks like this. You can take the Parameter parts and add it to the one from the Xamarin sample.
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
public string Text { get; set; }
public string Parameter { get; set; }
object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
{
try
{
if (Text == null)
return null;
var culture = new CultureInfo(CultureHelper.CurrentIsoLanguage);
var result = LocalizationResources.ResourceManager.GetString(Text, culture);
if (string.IsNullOrWhiteSpace(Parameter))
{
return string.IsNullOrWhiteSpace(result) ? "__TRANSLATE__" : result;
}
return string.IsNullOrWhiteSpace(result) ? "__TRANSLATE__" : string.Format(result, Parameter);
}
catch (Exception ex)
{
TinyInsights.TrackErrorAsync(ex);
return "__TRANSLATE__";
}
}
}