Emil
Emil

Reputation: 6893

How to reuse Xamarin.forms resx files in UWP native C# application

I have xamarin.forms application for android and ios, I want to target UWP as well but i dont want to use Xamarin.Forms generated UWP rather I will reuse ViewModels and resources from XF shared library and design UWP natively.

There are resx files as translation files within the Shared project and I am using in the xaml with help of TranslatorExtension as below like mentioned in the article

[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
    const string ResourceId = "LocalizationSample.Resources.AppResources";

    static readonly Lazy resmgr = 
        new Lazy(() => 
            new ResourceManager(ResourceId, typeof(TranslateExtension)
                    .GetTypeInfo().Assembly));

    public string Text { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        if (Text == null)
            return "";

        var ci = CrossMultilingual.Current.CurrentCultureInfo;
        var translation = resmgr.Value.GetString(Text, ci);

        if (translation == null)
        {
            translation = Text; // returns the key, which GETS DISPLAYED TO THE USER
        }
        return translation;
    }
}

And referencing in Xaml code in the ContentPage simply <Label Text="{i18n:Translate HelloWorld}"

This works perfectly fine but I couldnt figure out how to use translationextension in a UWP xaml Page. Is it only meant to be used for Xamarin.Forms application. Is there something similar for UWP. So basically how can reuse those resx files without duplicating for UWP?

PS, if i have to use Xamarin.forms nuget and namespace in order to use IMarkupExtension, it is still fine for me.

Upvotes: 0

Views: 89

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32775

This works perfectly fine but I couldnt figure out how to use translationextension in a UWP xaml Page. Is it only meant to be used for Xamarin.Forms application. Is there something similar for UWP. So basically how can reuse those resx files without duplicating for UWP?

I'm afraid you can't use above TranslateExtension in UWP Xaml, Because above IMarkupExtension in forms specific, you can't use it in UWP Xaml.

For making UWP native localization, please refer this document and use an x:Uid directive to associate a control or other element in your markup with a string resource identifier.

XAML

<TextBlock x:Uid="Greeting"/>

Upvotes: 0

Related Questions