Handle Multilingual in Xamarin Forms (Without any plugin)

A few years ago, I created a Multilingual plugin to handle multiple language support in Xamarin apps. The reason I did this, was because there was no .NET Standard at the time so for things like getting the device culture we had to create a platform implementation.

Many changes has been made and now the plugin is no longer needed. In this article, I will show you the simplest way to add multi-lingual support to your app. Also will provide a migration guide if you are currently using the plugin.

Let’s start

1.Create an AppResource File

Add one resx file per each language you want to support. “It must follow a specific naming convention: use the same filename as the base resources file (eg. AppResources) followed by a period (.) and then the language code”.

2. Add your texts in your AppResources file


3. Use your AppResources file in your code

To use your AppResource texts you have to import the AppResources class namespace in XAML and load it by using static reference.

In code-behind:

AppResources.WelcomeText

3. Language Management

Get Device Language

CultureInfo.InstalledUICulture

Get or Set the actual language

Thread.CurrentThread.CurrentUICulture = new CultureInfo(“es”);

AppResources.Culture= new CultureInfo(“es”);

Get all culture languages supported

CultureInfo.GetCultures(CultureTypes.NeutralCultures);

That’s all you need to support multi-lingual in your project :).

NOTE

If you are using visual studio for mac and you are having issues changing the language, there is an issue reported, and also a solution adding some lines in the android .csproject file to fix it. Check it here.

Multilingual plugin migration

Since the plugin is no longer required if you are using it, you must remove it. Follow these migration steps:

1. Remove the plugin of your project

2. Go to Helpers/TranslateExtension and remove the TranslateExtension file.

3. Do a File/Replace all in your visual studio (Command + Shift + F on Mac), to replace the namespace import of the TranslateExtension file with your AppResource file namespace import.

4. Do a File/Replace all in your visual studio, to replace the Translate reference in your XAML with the AppResource static property.

5. Replace the language management code

CrossMultilingual.Current.CurrentCultureInfo


by


Thread.CurrentThread.CurrentUICulture

Now we are ready :).

You can check the full source code here.

Happy coding! :).

You may also like

21 Comments

  1. Hello,

    Thanks for such great tutorial!

    I downloaded, compiled and ran this demo project but failed in Android. Language in UI is always set to English. It works fine in iOS.
    Any suggestions?

    VS for Mac: 8.6 (build 4520)
    Xamarin.Forms: 4.4.0.991265
    Xamarin.Essentials: 1.3.1

        1. Try in a physical device! Just tested this sample in my Physical Pixel 3 device and it is working fine!

  2. Hi,
    Thanks for the new way. I am struggling with a unique problem. I tried a static way and also a plugin.Multilanguage package.with 2 resource files English and Spanish. But in both the option the application not picking Spanish Language. It is pulling always English (Appresources.resx) key value. I really appreciate your help.

    1. Hi, If you are using visual studio for mac there is an issue reported, and also a solution adding some lines in the android .csproject file to fix it. Check it here

  3. Thanks for this guide!

    I’ve been a happy user of your plugin – and were are launching quite a large app this week – and then I found the language switching didn’t work on Android any more…. – but I see that is as intended and will migrate now.

    Just wanted to thank you for the plugin and your tips so far 😉

      1. Just for others to see if looking here. You can specify the use of a simple UpperCase converter in Xaml like this:

        Text=”{Binding Source={x:Static resource:AppResources.FilterYear}, Converter={StaticResource UpperCase}}”

        where the converter is declared in App.Xaml as:

        😉

  4. A mí no me funciona, siempre queda el lenguaje que tenga configurado el dispositivo.

    Mi app funciona para varios países y tengo un archivo de resources por cada uno (2 de ellos están en inglés y el resto en español, pero con ciertas diferencias).

    Cuando el usuario inicia sesión, está asociado a un país y en base a eso, asigno el idioma a la app.

    Con el plugin Multilingual podía cambiar el idioma en el momento y funcionaba correctamente, pero al remover el nuget y hacer las modificaciones de este post, prevalece el idioma del dispositivo.

    Por ejemplo, lo que está comentado era con el plugin:

    public static void SetLanguage()
    {
    string pais = Settings.CountryId;
    if (string.IsNullOrEmpty(pais))
    {
    //CrossMultilingual.Current.CurrentCultureInfo = CrossMultilingual.Current.CurrentCultureInfo;
    //CrossMultilingual.Current.CurrentCultureInfo;
    var ci = CultureInfo.InstalledUICulture;
    Thread.CurrentThread.CurrentUICulture = ci;
    Resource.Culture = ci;
    }
    else
    {
    string ci = “es”;
    switch (pais)
    {
    case “MX”:
    ci = “es-MX”;
    break;

    case “BZ”:
    ci = “en-BZ”;
    break;

    case “GT”:
    ci = “es-GT”;
    break;

    case “SV”:
    ci = “es-SV”;
    break;

    case “HN”:
    ci = “es-HN”;
    break;

    case “NI”:
    ci = “es-NI”;
    break;
    case “CR”:
    ci = “es-CR”;
    break;
    case “PA”:
    ci = “es-PA”;
    break;
    case “DO”:
    ci = “es-DO”;
    break;
    default:
    ci = “es”;
    break;
    }
    //CrossMultilingual.Current.CurrentCultureInfo = new System.Globalization.CultureInfo(ci);
    Resource.Culture = new CultureInfo(ci);
    Thread.CurrentThread.CurrentUICulture = new CultureInfo(ci);
    }

    }

    ¿Qué me hará falta?

    1. Si estás usando visual studio para Mac, revisa la nota sobre agregar unas líneas extras en el csproject

  5. hi ,i can’t resource file like this AppResource.fr.resx. when i add this it did not add designer class for this resource file , and that’s why my language did not change . please help me to resolve this .
    i’m using VS 16.6 on windows.

  6. cool ça marche pour moi mais la langue ne change pas tant que je l’instancie pas une nouvelle page …
    si je fais par exemple un popAsync la page se pressente dans la langue précédente

  7. cool it works for me but the language doesn’t change until I instantiate a new page …
    if I make for example a popAsync the page is displayed in the previous language