Quantcast
Channel: Microsoft Dynamics 365 Community
Viewing all articles
Browse latest Browse all 17314

Android App Localization Using Xamarin Forms and resx

$
0
0

If you are developing Android mobile applications, chances are that you are going to need some kind of localization and internationalization. And if you are targeting multiple mobile platforms - you would most likely prefer to do that job in one place.

The use of Xamarin Forms, PCL projects and paradigms such as MVVM allow for a great code sharing between all the different platform projects. Ideally our solution for localization:

  • Should be based on resource files
  • Resource files should be maintained in only one project

Here is when resx files come into the game. Resx is very simple and powerful resource management solution, where you can add or remove string, image, audio and video effortlessly.

PLC Project

Depending on your architecture you will have one or more PCL files where you keep platform independent code. Here we have separate PCL project for view layer.


Follow these steps:

  • In the PCL project, create a Resources folder
  • Create and add to the folder AppResources.resx file
  • Make sure that the custom tool used for this added file is PublicResXFileCodeGenerator, and that the resource file also has a designer file associated with it
  • Create resx files for languages and cultures other than default. In this example resx file for Spanish translation is added

Here are examples of entries in resx files:

  • Go to you Xaml file and include Resources namespace declaration:

  • Start using resource as shown on labels bellow:

x:Static markup accesses a public static field, static property, or constant defined by a class or structure, or an enumeration member. In our case we use it to access resource properties.

That’s all we need to do in PCL project. Let’s see how it looks like on Android.

Android

Here is part of the page described in code above:

String values for “Work Date” and “Date” labels are obtained from AppResources.resx file. In order to see strings from other files we need to change device language.

  • On your Android simulator or device go to Settings and in Language & input section select language for which you have resx file defined (in this example Espana)

  • Now when we go to the same page in our application we see that Spanish translation strings are applied:

As you can see, we haven’t done any platform specific coding for Android in order to apply localization. Implementation for other platforms (iOS and Windows Phone) should be fairly similar. This way we have strings in one place which are shared across multiple files and platforms, and application that could be easily translated in multiple languages.

Viewing all articles
Browse latest Browse all 17314

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>