Localize your Xamarin Forms App with shared project.

When I’ve started with Xamarin.Forms, my first problem was about app’s localization. All tutorials and also Xamarin documentation that I found regard only about PCL’s projects (that now are deprecated in favor of .net standard).

However, my scenario was better with shared project so I started the project in this way, saying to myself: “It’s time to face off the problem”.

I’ve thinked about a solution about app’s localization and I’ve developed the DewXamarinLocalization library, a .net standard library for xamarin forms.

So, let’s see how we can localize a Xamarin.Form shared project.

How to use

My first though was about that this localization system must be easy, and (maybe it can be still better, who knows?) I think I’ve done it!

Install package

Your first step is to install the DewXamarinLocalization in your projects (all, you can use solution nuget package manager)

Files

The files are simple json dictionary, like this:

IMPORTANT! ————————–

These steps are important!

Tree example

Naming

This is really important, you must be carefull about the file names. Every filename MUST correspond to the CultureInfo Name property in lower case (en-us,it-it,it-ch,en-gb,fr-fr,etc.) and must be json. Obviously if your app support 3 languages, you should have 3 different culture and you must support all of them.

Path

The file MUST be placed in a folder called Localized in the shared project.

Embedded

When you create the file, you MUST set in the properties “Embedded resource” like Build Action

NOTE

If a file for a culture isn’t present you’ll get an exception when the app is opening only with that culture.

IMPORTANT! ————————–

XAML Code

This page is from the xamarin.forms base template for a new app.

How you can see, your first step is to import the namespace:

After this you can use it thanks the Markup extension

Another approach

You can also set the strings in this way

In code

If you need to acces to the dictionary in code behind you can this way:

If string doesn’t exists?

The localization system is based on aspnet core middleware DewLocalizationMiddleaware and works in the same way.

Files must be present otherwise you’ll get an exception, but strings doesn’t need this.

If a string doesn’t exists, the Localization class just return the key value.

This way if you want you can use the default word like key, so if in the translation it doesn’t exists it will return the key self.

An example

or

If the it-it.json file contains a voice for “My App’s name”, it will be translated, otherwise the app will print “My App’s name” (that is the key).

Change dictionary in runtime

If you want change the dictionary in runtime, you shouuld just do:

Scenario where you want change your language at app’s start

If you want to change the language in app start, your first approach is to place the ChangeCulture call into OnStart event but it won’t work. This because the ChangeCulture method is asyncronous, and the App will initialize via Xaml and this will create a conflict (with crashes sometimes).

Another approach is to change the culture in the OnAppaering event of the main page, and it works, but only for the next pages, because the mainpage will be loaded with the currentculture language.

If you try to call ChangeCulture method into App constructor you’ll get a NullReferenceException because DewXamarinLocalization class depends from Application class.

A solution for this particular problem is done with the static property CultureStringOverride.

This property is mono-use (after set it, you should call LoadDictionary method, that will read it and will delete it) didn’t has dependencies, so you can call into App constructor without problem.

Like I’ve said, you SHOULD call LoadDictionary after set this property, but, in this particular case, will be XAML the creator of the DewXamarinLocalization class.

NOTE: If you set property and after call LoadDictionary you’ll get the same result of a call of ChangeCulture, so you should use this property only for this scenario.

Example:


Hope this can help you to localize your apps and, if you like it check the others DewLibraries on nuget.

I use these libraries at work, so they’re updated frequently.

Note: some of these libraries have docs under constructions.

You can find this (and the others) on GitHub.