Forum Cross Platform with Xamarin
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Xamarin shared project Localization.

EmyMa341EmyMa341 USMember ✭✭
edited January 2017 in Cross Platform with Xamarin

Good day Xamarin community developers, although not every day is good for coders :dizzy:

I have a Xamarin solution with following project in visual studio 2015:

1) Xamarin.iOS
2) Xamarin.Android
3) Xamarin.Shared

Xamarin.iOS and Xamarin.Android are simple iOS and android project in Xamarin with most of the functionalities, code structure quite equivalent to native one, which I preferred when I started this project.

In Xamarin.Shared I have all shared classes, which can be used both in iOS and Android. The following image shows insights of these projects.

Problem: I want to implement localization using .resx file in shared project, just like used in Xamarin.forms PCL project that is TodoLocalization. That is a single location for localization for iOS and Android using .resx.

I went through many links, everyone talks about Xamarin.Forms and PCL projects. No one talks about shared project, just like the project structure I am using. In addition, there is no official documentation available on such project structure.

Apart from that I have tried creating .resx file in shared project:

  • There is no option for creating a resource file in Xamarin shared project.
  • I created a resource file in another project and copied the resource file including designer in shared project but I guess shared project don't support
    resource thats why It was giving me exception 'Embedded resource not found'. I verified all path things but no luck. In addition I also corrected the
    namespaces. (Copied few ideas from here )

Is there any way or hack to create a resource file in Shared project and use a single resource file in iOS and Android.

Looking forward to answers. :)

Posts

  • MdNaushadMdNaushad INMember ✭✭✭

    Did you get answer for this or did you implement it?

  • ClaudioPereiraClaudioPereira BRMember ✭✭

    @MdNaushad said:
    Did you get answer for this or did you implement it?

    When I started develop my App, I picked the Shared project, and when I got to the localize stuffs I was shocked that that .resx would only work proplerly in PCL. After some digging, I came up with the solution by creating a separated PCL with all my resources. That way you can do the .Resx as

    Here is how my implementation ended:

    App(Shared)
    App.iOS
    App.Android
    AppResources(PCL)

    Support for CS code:

    I call the resource directly from the PCL:
    var titlelabel = new Label();
    titlelabel.Text = AppResouces.Resx.AppResource.AppName;

    For support the XAML:
    In the APP(Shared) I created a TranslateExtension.cs

    using System;
    using System.Globalization;
    using System.Reflection;
    using System.Resources;
    using Xamarin.Forms;
    using AppResouces;
    using Xamarin.Forms.Xaml;
    namespace App
    {
        // You exclude the 'Extension' suffix when using in Xaml markup
        [ContentProperty("Text")]
        public class TranslateExtension : IMarkupExtension
        {
            readonly CultureInfo ci = null;
            const string ResourceId = "AppResouces.Resx.AppResource";
    
            public TranslateExtension()
            {
                if (Device.OS == TargetPlatform.iOS || Device.OS == TargetPlatform.Android)
                {
                    ci = DependencyService.Get<App.ILocalize>().GetCurrentCultureInfo();
                }
    
            }
            public string Text { get; set; }
            public object ProvideValue(IServiceProvider serviceProvider)
            {
                if (Text == null)
                    return "";
                Assembly ressourceAssembly = Assembly.Load("AppResouces");
                ResourceManager temp = new ResourceManager(ResourceId, ressourceAssembly);
    
                var translation = temp.GetString(Text, ci);
                if (translation == null)
                {
                    throw new ArgumentException(
                        String.Format("Key '{0}' was not found in resources '{1}' for culture '{2}'.", Text, ResourceId, ci.Name),
                        "Text");
    
                    translation = Text; // HACK: returns the key, which GETS DISPLAYED TO THE USER
                }
                return translation;
            }
        }
    } 
    

    xmlns:helpers="clr-namespace:App"

    Title="{helpers:TranslateExtension AppName}"

    For me that has been working with no problem.

    Hope that won't be late and help in some way,

    Cheers,

  • MdNaushadMdNaushad INMember ✭✭✭

    @ClaudioPereira - I was looking around for it and used the same in my project. Thank you for your time :smile:

  • MdNaushadMdNaushad INMember ✭✭✭

    Apart from using the way you suggested, for my Resource file in Shared Project I had to set the BuildOption as Embedded Resource, iOS and android project doesn't seem to have an issue with that but my UWP project gives out an error , it says "Embedded Resource is not supported by projects with an output type of "appcontainerexe" ".
    Any idea?

  • ClaudioPereiraClaudioPereira BRMember ✭✭

    @MdNaushad said:
    Apart from using the way you suggested, for my Resource file in Shared Project I had to set the BuildOption as Embedded Resource, iOS and android project doesn't seem to have an issue with that but my UWP project gives out an error , it says "Embedded Resource is not supported by projects with an output type of "appcontainerexe" ".
    Any idea?

    I didn't develop the UWP app, so I am not sure if that would work. But I can't see any problem though. From my share project I accessing the Resource pcl as I do if it was any other plugin. so I assume that would work on UWP.

    I think that in UWP that would work without any work around once .resx is a .Net implementation. I hope you will find a solution.

  • MdNaushadMdNaushad INMember ✭✭✭

    @ClaudioPereira said:

    @MdNaushad said:
    Apart from using the way you suggested, for my Resource file in Shared Project I had to set the BuildOption as Embedded Resource, iOS and android project doesn't seem to have an issue with that but my UWP project gives out an error , it says "Embedded Resource is not supported by projects with an output type of "appcontainerexe" ".
    Any idea?

    I didn't develop the UWP app, so I am not sure if that would work. But I can't see any problem though. From my share project I accessing the Resource pcl as I do if it was any other plugin. so I assume that would work on UWP.

    I think that in UWP that would work without any work around once .resx is a .Net implementation. I hope you will find a solution.

    As of now even I am not much concentrating towards the UWP app, If I find any solution in future I will post it here.

Sign In or Register to comment.