0
votes

I'm trying to learn how to implement and test localization within a Xamarin Forms application using MvvmCross.Forms. Using Visual Studio 2017 or 2019 Community edition.

So far I have tried to follow the tutorials.

https://www.mvvmcross.com/documentation/plugins/resxlocalization

https://mobileprogrammerblog.wordpress.com/2017/12/30/mvvm-cross-with-xamarin-platform-resx-localization/

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/localization/

But none of these are showing how to implement a solution for a .Net Standard class where the Visual Studio solution has the following setup.

  • Application.Core - This is for ViewModels and Services (.Net Standard 2.0 Library with MvvmCross NuGet)
  • Application.UI - This is the shared Views and View Logic (.Net Standard 2.0 Library with Xamarin.Forms MvvmCross MvvmCross.Forms NuGets)
  • Application.UWP - Application Specific Presenter (Xamarin.Forms MvvmCross MvvmCross.Forms NuGets)
  • Application.Android - Application Specific Presenter (Xamarin.Forms MvvmCross MvvmCross.Forms NuGets)
  • Application.iOS - Application Specific Presenter (Xamarin.Forms MvvmCross MvvmCross.Forms NuGets)

NuGet Versions

  • Xamarin.Forms - v3.6
  • MvvmCross - v6.2.3
  • MvvmCross.Forms - v6.2.3

I would like to understand how to implement localization with as little code going into the application specific presentation layers as possible.

Can anyone point me to a tutorial on how MvvmCross.Forms should be implemented with Xamarin.Forms?

I'm happy to user either JSon, RESX or any other method for storing the specific languages but I want to be able to support Right to Left languages and layouts as well.

1

1 Answers

5
votes

If you're using Xamarin Forms, you can create the resx files in your Core project, for example AppResources.resx and AppResources.es.resx and there put a simple text like:

In AppResources.resx

<data name="Hello" xml:space="preserve">
    <value>Hello</value> 
</data>

and in AppResources.es.resx

<data name="Hello" xml:space="preserve">
    <value>Hola</value> 
</data>

Then you create a class named something like BaseViewModel that extends from MvxViewModel and there you reference the index of the string localizated:

public class BaseViewModel : MvxViewModel
{
    public BaseViewModel()
    {
    }

    public string this[string index] => AppResources.ResourceManager.GetString(index);
}

And now extend all your viewmodels from that BaseViewModel. Then in your xaml files from xamarin forms, when you create a content page make it be MvxContentPage and you can attach a viewmodel to it via x:TypeArguments, there you have the reference to the AppResources index, so if want to use an string from the localizated resx, just use regular xamarin forms binding and pass the string name as index, something like:

<Label
    FontSize="Medium"
    TextColor="Black"
    Text="{Binding [Hello]}" />

Or use mvx binding system

<Label
    FontSize="Medium"
    TextColor="Black"
    mvx:Bi.nd="Text [Hello]" />

You could check the Star Wars Sample project to see how many things (like localization strings) works ;)