Quantcast
Viewing latest article 5
Browse Latest Browse All 10

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”.

Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

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.

Image may be NSFW.
Clik here to view.

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

Image may be NSFW.
Clik here to view.

5. Replace the language management code

CrossMultilingual.Current.CurrentCultureInfo


by


Thread.CurrentThread.CurrentUICulture

Now we are ready :).

Image may be NSFW.
Clik here to view.

You can check the full source code here.

Happy coding! :).

The post Handle Multilingual in Xamarin Forms (Without any plugin) appeared first on XamGirl.


Viewing latest article 5
Browse Latest Browse All 10

Trending Articles