I’ve previously looked at the MAT in the post Adventures in UWP – Globalization & Localization UWP – Globalization & Localization. I’ve also used this within mobile applications using Xamarin Forms which I was about to update, but then realised I’d forgotten how this stuff worked.
Requirements
Before we start, we need to install a couple of things.
- From Visual Studio 2019 (if not already installed) select Extensions | Manage Extensions and download the Multilingual App Toolkit. You’ll need to restart Visual Studio for the installation to take place.
- Install (if not already installed) the Multilingual app toolkit 4.0 Editor as this is extremely useful for managing our translated files, but is NOT required.
Note: This does not currently seem to be supported in Visual Studio for Mac.
Mobile Application
Let’s start by creating a Xamarin Forms application to see how things work.
- Create a Mobile App (Xamarin.Forms) in Visual Studio
- Add a Resources folder to the shared project and then add a Resource File item to this folder, named AppResources.resx
- Right mouse click on the shared project and select Properties, now select Package and towards the bottom of this screen, change the Assembly neutral language to your default language – for example mine’s English (United Kingdom). In other words our project culture will be set to en-GB
- Enable MAT by selecting Tools | Multilingual App Toolkit | Enable on your solutions
At this point we now have the basics in place to begin localizing our application. We can now start adding strings to our AppResources.resx or alternatively we could start adding new languages, so let’s do that.
- Right mouse click on your shared project and select Multilingual App Toolkit and from this select, Add translation languages…
Note: You may get and error regarding the need to install translation providers, this requires us to add an Azure subscription, but we don’t actually need this to use the tools, so just ignore this error for now.
From the Translation Languages check on any languages you want to support (you can also add languages at any point so don’t worry if you need to add more at a later date).
In my case I’m going to add German [de] and French (France) [fr-FR]. Once you press OK you’ll see two additional AppResources files added to the Resources folder, AppResources.de.resx for German and AppResources.fr-FR.resx for French.
You’ll also see the MultiligualResources folder with two .xlf files, one for German and one for French, these are used by MAT to generate our new AppResources files.
Note: Do not edit the localized AppResources.*.resx files by hand. Only AppResources.resx should be edited.
Editing our resources
Now that we have the xlf files and the default culture AppResources.resx file, we’ll want to start adding some strings.
Open the AppResources.resx file in Visual Studio and add some named strings, the Name being the identifier or key, the Value being the string to display and we can also add comments which might be useful for somebody who handles the subsequent translations.
So I’m going to create a basic login page, hence we’ll add the following
- Name: Welcome, Value: Welcome to the application
- Name: Login, Value: Login
- Name: Username, Value: Username or email address
- Name: Password, Value: Enter your password
Now build your application before we look to create some translations.
There’s a couple of ways to proceed with this, the simplest is as follows
- From Visual Studio. Select you MultilingualResources folder or each .xlf in turn.
- Right mouse click on the folder or file then select Multilingual App Toolkit | Generate Machine Translations.
You may find that some values are not translated, for example in my case Welcome to the application was not translated, we can edit the de.xlf file directly. In this case I change the target element to Willkommen bei der Anwendung and changed the target state to needs-review-translation
Once you’ve made your changes then build the project and MAT will generate the AppResources.*.resx files.
The second way to handle the translations and probably one that would be useful for people who are working on translations of your application. In this case we use the Multilingual Editor that was installed earlier. Open each of your xlf files in turn. For each one we’ll see a list of the strings as entered into the AppResources.rex file. So let’s concentrate on editing the .de.xlf German translation file.
- Having opened the .de.xlf file, select the Strings tab at the bottom of the screen to ensure all the strings were added, now here we could manually translate each row by selecting the each row in turn, then changing the Translation in the editor in the middle of the editor UI.
- If you edit the translation yourself then the State on the right of the screen will switch to Translated. This state will tell the MAT editor whether it should try to translate the value. If the user has marked the state as Translated we can assume this is correct and the editor will not attempt to translate the string using it’s generate translation option. You can change this State back to New if you want the auto-translation to take place.
- If you prefer or like me you’re simply going to use something like Bing or Google to do the translations, i.e. https://www.bing.com/translator/. Then the Editor offers the Translate button. Clicking this will generate translations for all the text and mark them with the State Needs Review which is basically saying that the translation was made but needs somebody to review whether this makes sense in each language. You can leave this State as is or change to Translated if you’re happy with these translations.
- Once completed, save the file(s) and build your solution to get the AppResources.*.rexr updated.
Displaying our translations in an application
Note: This post has been updated as the previously used Multilingual plugin is no longer required.
We can display translations is XAML using the AppResources generated code, for example
Title="{x:Static resources:AppResources.Settings}
and in code just as easily we use
var title = AppResources.Settings;
To test your translations you can set things up in code, i.e.
In your App.xaml.cx, after the InitializeComponent you can place the following code to set the culture to that of the device
var culture = CrossMultilingual.Current.DeviceCultureInfo;
AppResources.Culture = culture;
If you want to test your German translation, for example, replace these two lines with
var culture = new CultureInfo("de");
AppResources.Culture = culture;
CrossMultilingual.Current.CurrentCultureInfo = culture;
See https://github.com/putridparrot/blog-projects/tree/master/MatTestMobile for a sample application.
If you prefer to use the Android Emulator to set your language – which ofcourse makes a lot of sense, then go to Android Settings | System | Languages & input | Languages then Add a language if the one you want is not listed, then drag and drop that language to the top of the Languages list and Android will switch to that language.