Add Languages to Your Xamarin apps with multilingual App Toolkit

Source: Internet
Author: User
Tags i18n

With Xamarin, you can extend your Cross-platform apps with support for native speakers, reaching the might Ise be overlooked.  However, managing multiple languages and keeping everything current can is challenging, especially if you ' re not fluent in Those languages.

This is where the multilingual App Toolkit (MAT) can help by automating the management of your language n Focus on how do best:build great Cross-platform apps!

Before getting started, it's important to note the most project templates is not localization enabled. Fortunately, adding localization support is straightforward. Once do the initial setup, your app is ready for the world.

Getting Started

Let's look quickly in this process using a xamarin.forms sample. The Localizationsample project have all the plumbing and are available on GitHub.

Wiring up localization can is do quickly using a NuGet called Multilingual (localization) Plugin for Xamarin and Win Dows. The documentation is straightforward and the core being the addition of the TranslationExtension class to provide XAML binding for your App s resources. Here's the code-snippet used to wire up access to the Plugin.Multilingual resources:

123456789101112131415161718192021222324252627 [contentproperty("Text")] public class translateextension : imarkupextension { const string ResourceId = "LocalizationSample.Resources.AppResources" ; static readonly Lazy resmgr = New Lazy(() => New ResourceManager(ResourceId, typeof(Translateextensi On)                     . GetTypeInfo(). Assembly)); Public string Text { get; set; }Public object ProvideValue(iserviceprovider serviceprovider)     { if (Text = = null) return "";         var ci = crossmultilingual. Current. Currentcultureinfo var translation = resmgr. Value. GetString(Text, ci); if (translation = = null)         { translation = Text; //Returns the key, which GETS DISPLAYED to the USER         } return translation;     }}

Of course, you'll need a place to store the localizable text. Using the familiar RESX file with xamarin.forms makes this a snap. Simply Add a folder named "Resources" to the Samplelocalization project. In this folder, add and AppResources.resx ' HelloWorld ' with the value ' Hello from App Resource ' to the new RESX file:

The final step before all the wiring are complete are to replace the hard-coded text in the XAML code with bindings that wil L Retrieve the text from the resource file. All that remains are adding a reference TranslateExtension to the and applying the binding, like so:

12345678910111213141516171819202122232425 <? XML Version="1.0" encoding="Utf-8" ?> <contentpage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="Http://schemas.microsoft.com/winfx/2009/xaml" xmlns:i18n="Clr-namespace:localizationsample.helpers;assembly=localizationsample" xmlns:local="Clr-namespace:localizationsample" x:Class="Localizationsample.mainpage" Title="Let ' s translate!" > <stacklayout Padding="> " <label Text="{i18n:translate HelloWorld}" Linebreakmode="WordWrap"/> <label Margin="0,60,0,0" Text="Translate This automatically" Linebreakmode="WordWrap" x:Name="labeltranslate"/> <picker ItemsSource="{Binding Languages}" itemdisplaybinding="{Binding DisplayName}" x:Name="pickerlanguages"/> </StackLayout> </ContentPage>

Running The app should now display the Chinese string "Hello from App Resource" as well as the still hard-coded text value S

Traditionally, after ensuring the localization are wired up and displaying correctly, you would finish adding all the app ' s Resource data before considering adding additional languages to avoid managing the changes to the resource or the related Translation during the ongoing development process. This can is a challenge with only one or both additional languages, but becomes daunting very quickly as more languages is Added.

It's best to validate this your app ' s language support are working correctly throughout the development process to avoid BU GS-May delay your app's release, or worse, cause you-to-reduce market support by cutting additional language.

This is where the multilingual App Toolkit (MAT) comes in. It manages your target rexs files by applying On-demand machine translations throughout the development process. Once development nears completion, you can has the translations proofed and adjusted by family, friends, co-workers, or T Ranslation vendors.

Install and Enable

To Install the multilingual App Toolkit (MAT), just use Visual Studio's Tools-Extensions and Updates menu:

After the installation are completed, you'll need to ensure your apps has defined the default app language. The language displayed if the app does not the language of the device. It ' s also the language MAT uses to know how to translate into other languages.

To check the Neutral Language, right-click the LocalizationSample project and select Properties. On the Package tab, the Assembly Neutral Language value determines the app ' s default Language. This should is set to the language you use in your AppResources.resx file. In this example, it'll be 中文版 (en-US):

MAT needs to is enabled for any project, which has localizable text. This is do by selecting the project in the solution Explorerand then the Tools--- multilingual App Toolkit -Enabled Selection:

Adding target languages is as simple as right-clicking on the project and selecting multilingual App Toolkit, ADD Translation languages. Remember to set up the Azure cognitive Service configuration for MAT ' s Microsoft Translator provider first.

After selecting the desired language and clicking "OK" and MAT would add both files for every language selection, an . XLF fileand the corresponding . RESX file. The. XLF file is a XLIFF 1.2 industry standard file, which is designed to support translation data. The related RESX files is managed by MAT and used by Visual Studio to add the desired language the Your app.

Selecting one or more. The XLF file enables the menus that is allow for machine translation or even exporting for external localization. Select Generate Machine translations to apply machine-generated translations to your project:

Note:before Building, ensure the package Action was set to XliffResource . This setting are used by the build task to update related RESX with the translation in the. XLF file. Without this setting, your app won't contain the translation:

Now it's time to build and run your app! Language Picking in the sample is configured dynamically; You can switch the text to easy testing. For more application, let the device ' s language pick the app ' s language automatically.

Add Languages to Your Xamarin apps with multilingual App Toolkit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.