Xamarin.Forms中标签StringFormat的本地化 [英] Localization for Label StringFormat in Xamarin.Forms

查看:23
本文介绍了Xamarin.Forms中标签StringFormat的本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在xamarin形式中,我可以将文本本地化为一个标签,例如:
<标签文本="{x:静态资源:AppResources.Text}"/>

In xamarin forms I can localize the text in a label like:
<Label Text="{x:Static resources:AppResources.Text}"/>

具有用于资源的名称空间:
< ContentView ...xmlns:resources ="clr-namespace:ProjectName.Resources; assembly = ProjectName">

With a namespace for the resources:
<ContentView ... xmlns:resources="clr-namespace:ProjectName.Resources;assembly=ProjectName">

我还可以绑定一些值并在标签上添加字符串格式:
<标签文本="{Binding Value,StringFormat ='值是:{0}'}"/>

I can also bind some value and add a string format to the label:
<Label Text="{Binding Value, StringFormat='The value is: {0}' }"/>

问题是文本值是:未本地化.

谁可以同时绑定值和本地化StringFormat?

Who can I do both, bind a value and localize the StringFormat?

推荐答案

我在我必须在资源文件中添加文本,值为:{0} .
我需要为翻译添加一个IMarkupExtension.我将该类添加到与资源文件相同的名称空间中.

I had to add the text The value is: {0} to the resource file.
I needed to add an IMarkupExtension for the translation. I added the class to the same namespace as the resource file.

[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
    private readonly CultureInfo _ci;

    static readonly Lazy<ResourceManager> ResMgr = new Lazy<ResourceManager>(
        () => new ResourceManager(typeof(AppResources).FullName, typeof(TranslateExtension).GetTypeInfo().Assembly));

    public string Text { get; set; }

    public TranslateExtension()
    {
        if (Device.RuntimePlatform == Device.iOS || Device.RuntimePlatform == Device.Android)
        {
            _ci = DependencyService.Get<ILocalize>().GetCurrentCultureInfo();
        }
    }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Text == null)
            return string.Empty;

        return ResMgr.Value.GetString(Text, _ci) ?? Text;
    }
}

并像这样使用它:

<标签文本="{Binding Value,StringFormat = {resources:Translate LabelTextTheValueIs}}""/>

这篇关于Xamarin.Forms中标签StringFormat的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆