{"id":35050,"date":"2018-02-08T15:10:34","date_gmt":"2018-02-08T23:10:34","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=35050"},"modified":"2019-04-04T08:47:33","modified_gmt":"2019-04-04T15:47:33","slug":"add-languages-to-your-apps-with-xamarin-and-multilingual-app-toolkit","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/add-languages-to-your-apps-with-xamarin-and-multilingual-app-toolkit\/","title":{"rendered":"Add Languages to Your Xamarin Apps with Multilingual App Toolkit"},"content":{"rendered":"<p>With Xamarin, you can extend your cross-platform apps with support for native speakers, reaching markets that might otherwise be overlooked. However, managing multiple languages and keeping everything current can be challenging, especially if you\u2019re not fluent in\u00a0those languages.<\/p>\n<p><img decoding=\"async\" class=\"alignright wp-image-35418\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/MultiLangApp.png\" alt=\"\" width=\"125\" height=\"134\" \/>This is where the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=MultilingualAppToolkit.MultilingualAppToolkit-18308\">Multilingual App Toolkit (MAT)<\/a> can help by automating\u00a0the management of your language support, so you can focus on what you do best: build great cross-platform apps!<\/p>\n<p>Before getting started, it&#8217;s important to note that most project templates are <em>not<\/em> localization enabled. Fortunately, adding localization support is straightforward. Once you do the initial setup, your app will be ready to travel the world.<\/p>\n<h2>Getting Started with Multilingual App Toolkit<\/h2>\n<p>Let\u2019s look quickly at this process using a Xamarin.Forms sample. The <a href=\"https:\/\/github.com\/jamesmontemagno\/localizationsample\">LocalizationSample<\/a> project has all the plumbing and is available on GitHub.<\/p>\n<p>Wiring up localization can be done quickly using a NuGet called <a href=\"https:\/\/xamgirl.com\/multilingual-localization-plugin-for-xamarin\/\"><em>Multilingual (Localization) Plugin for Xamarin and Windows<\/em><\/a>. The documentation is straightforward, with the core being the addition of the <code>TranslationExtension<\/code> class to provide XAML binding for your app\u2019s resources. Here&#8217;s the code-snippet <code>Plugin.Multilingual<\/code> used to wire up access to the resources:<\/p>\n<pre class=\"lang:c# decode:true \">[ContentProperty(\"Text\")]\r\npublic class TranslateExtension : IMarkupExtension\r\n{\r\n    const string ResourceId = \"LocalizationSample.Resources.AppResources\";\r\n\r\n    static readonly Lazy resmgr = \r\n        new Lazy(() =&gt; \r\n            new ResourceManager(ResourceId, typeof(TranslateExtension)\r\n                    .GetTypeInfo().Assembly));\r\n\r\n    public string Text { get; set; }\r\n\r\n    public object ProvideValue(IServiceProvider serviceProvider)\r\n    {\r\n        if (Text == null)\r\n            return \"\";\r\n\r\n        var ci = CrossMultilingual.Current.CurrentCultureInfo;\r\n        var translation = resmgr.Value.GetString(Text, ci);\r\n\r\n        if (translation == null)\r\n        {\r\n            translation = Text; \/\/ returns the key, which GETS DISPLAYED TO THE USER\r\n        }\r\n        return translation;\r\n    }\r\n}<\/pre>\n<p>Of course, you&#8217;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 \u201cResources\u201d to the SampleLocalization project. In this folder, add <code>AppResources.resx<\/code> and \u201cHelloWorld\u201d with the value \u201cHello from App Resource\u201d to the new RESX file:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35396\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti1.png\" alt=\"\" width=\"500\" height=\"259\" \/><img decoding=\"async\" class=\"aligncenter wp-image-35397\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti2.png\" alt=\"\" width=\"600\" height=\"123\" \/><\/p>\n<p>The final step before all the wiring is complete is to replace the hard-coded text in the XAML code with bindings that will retrieve the text from the resource file. All that remains is adding a reference to the <code>TranslateExtension<\/code> and applying the binding, like so:<\/p>\n<pre class=\"lang:xhtml decode:true \">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\r\n&lt;ContentPage xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\"\r\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"\r\n             xmlns:i18n=\"clr-namespace:LocalizationSample.Helpers;assembly=LocalizationSample\" \r\n             xmlns:local=\"clr-namespace:LocalizationSample\"\r\n             x:Class=\"LocalizationSample.MainPage\"\r\n             Title=\"Let's Translate!\"&gt;\r\n \r\n    &lt;StackLayout Padding=\"20\"&gt;\r\n\t    &lt;Label Text=\"{i18n:Translate HelloWorld}\" \r\n               LineBreakMode=\"WordWrap\"\/&gt;\r\n \r\n\t    &lt;Label \r\n                Margin=\"0,60,0,0\"\r\n                Text=\"Translate this automatically\"\r\n                LineBreakMode=\"WordWrap\"\r\n               x:Name=\"LabelTranslate\"\/&gt;\r\n \r\n\t    &lt;Picker ItemsSource=\"{Binding Languages}\"\r\n                ItemDisplayBinding=\"{Binding DisplayName}\"\r\n                x:Name=\"PickerLanguages\"\/&gt;\r\n \r\n    &lt;\/StackLayout&gt;\r\n \r\n&lt;\/ContentPage&gt;<\/pre>\n<p>Running the app should now display the English string \u201cHello from App Resource\u201d as well as the still hard-coded text values:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35398\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti3.png\" alt=\"\" width=\"282\" height=\"500\" \/><\/p>\n<p>Traditionally, after ensuring the localization is wired up and displaying correctly, you would finish adding all the app\u2019s 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 be a challenge with only one or two additional languages, but becomes daunting very quickly as more languages are added.<\/p>\n<p>It&#8217;s best to validate that your app\u2019s language support is working correctly throughout the development process to avoid bugs that may delay your app\u2019s release, or worse, cause you to reduce market support by cutting additional language support.<\/p>\n<p>This is where the <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=MultilingualAppToolkit.MultilingualAppToolkit-18308\">Multilingual App Toolkit (MAT)<\/a> comes in. It manages your target REXS files by applying on-demand Machine Translations throughout the development process. Once development nears completion, you can have the translations proofed and adjusted by family, friends, co-workers, or translation vendors.<\/p>\n<h2>Install and Enable<\/h2>\n<p>To Install the Multilingual App Toolkit(MAT), just use Visual Studio\u2019s <em>Tools -&gt; Extensions and Updates<\/em> menu:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35399\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti4.png\" alt=\"\" width=\"500\" height=\"129\" \/><\/p>\n<p>After the installation is completed, you will need to ensure your app has defined the default app language. This is the language displayed if the app does <em>not<\/em> support the language of the device. It&#8217;s also the language MAT uses to know how to translate into other languages.<\/p>\n<p>To check the <strong>Neutral Language<\/strong>, right-click the <code>LocalizationSample<\/code> project and select <strong>Properties<\/strong>. On the package tab, the <em>Assembly Neutral Language<\/em> value determines the app&#8217;s default language. This should be set to the language you use in your <code>AppResources.resx<\/code> file. In this example, it will be English (en-US):<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35400\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti5.png\" alt=\"\" width=\"600\" height=\"378\" \/><\/p>\n<p>MAT needs to be enabled for any project that has localizable text. This is done by selecting the project in the <strong>Solution Explorer<\/strong>, then the <em>Tools -&gt; Multilingual App Toolkit -&gt; Enabled selection<\/em>:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35401\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti6.png\" alt=\"\" width=\"600\" height=\"153\" \/><\/p>\n<p>Adding target languages is as simple as right-clicking on the project and selecting <em>Multilingual App Toolkit -&gt; Add translation languages<\/em>. Remember to set up the <a href=\"https:\/\/multilingualapptoolkit.uservoice.com\/knowledgebase\/articles\/1167898-microsoft-translator-moves-to-the-azure-portal\">Azure Cognitive Service configuration<\/a> for MAT\u2019s Microsoft Translator provider first.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35402\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti7.png\" alt=\"\" width=\"600\" height=\"280\" \/><img decoding=\"async\" class=\"aligncenter wp-image-35403\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti8.png\" alt=\"\" width=\"410\" height=\"500\" \/><\/p>\n<p>After selecting the desired language and clicking &#8220;OK&#8221; and MAT will add two files for every language selection, an <strong>.XLF file<\/strong> and the corresponding <strong>.RESX file<\/strong>. The .XLF file is an XLIFF 1.2 industry standard file that is designed to support translation data. The related RESX files are managed by MAT and used by Visual Studio to add the desired language support to your app.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35404\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti9.png\" alt=\"\" width=\"500\" height=\"429\" \/><\/p>\n<p>Selecting one or more .XLF file enables the menus that allow for Machine Translation or even exporting for external localization. Select <strong>Generate machine translations<\/strong> to apply machine-generated translations to your project:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35405\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti10.png\" alt=\"\" width=\"600\" height=\"248\" \/><img decoding=\"async\" class=\"aligncenter wp-image-35406\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti11.png\" alt=\"\" width=\"500\" height=\"204\" \/><\/p>\n<p>Note: Before building, ensure the Package Action is set to <code>XliffResource<\/code>. This setting is used by the build task to update related RESX with the translation in the .XLF file. Without this setting, your app will not contain the translation:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35407\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti12.png\" alt=\"\" width=\"500\" height=\"171\" \/><\/p>\n<p>Now it&#8217;s time to build and run your app! Language picking in the sample is configured dynamically; you can switch the text for easy testing. For more application, let the device\u2019s language pick the app\u2019s language automatically.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-35408\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/XamandMulti13.png\" alt=\"\" width=\"600\" height=\"578\" \/><\/p>\n<h2>See It In Action<\/h2>\n<p>This week I joined <a href=\"http:\/\/twitter.com\/jamesmontemagno\">James Montemagno<\/a> on <a href=\"https:\/\/channel9.msdn.com\/Shows\/XamarinShow\/Translating-Mobile-Apps-With-The-Multilingual-App-Toolkit-with-Cameron-Lerum\">The Xamarin Show<\/a> to show off the Multilingual App Toolkit in action:<\/p>\n<p><iframe src=\"https:\/\/channel9.msdn.com\/Shows\/XamarinShow\/Translating-Mobile-Apps-With-The-Multilingual-App-Toolkit-with-Cameron-Lerum\/player\" width=\"650\" height=\"370\" allowFullScreen frameBorder=\"0\"><\/iframe><\/p>\n<h2>Wrapping Up<\/h2>\n<p>You&#8217;re now ready to create great cross-platform apps that work in many languages! To download, visit the Visual Studio Marketplace and search <a href=\"https:\/\/marketplace.visualstudio.com\/items?itemName=MultilingualAppToolkit.MultilingualAppToolkit-18308\">Multilingual App Toolkit (MAT)<\/a>. You can find the code for the LocalizationSample <a href=\"https:\/\/github.com\/jamesmontemagno\/localizationsample\">on Github<\/a>.<\/p>\n<p><a href=\"https:\/\/forums.xamarin.com\/122246\/https:\/\/forums.xamarin.com\/\">Discuss this post on the Xamarin Forums<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With Xamarin, you can extend your cross-platform apps with support for native speakers, reaching markets that might otherwise be overlooked. However, managing multiple languages and keeping everything current can be challenging, especially if you\u2019re not fluent in\u00a0those languages. This is where the Multilingual App Toolkit (MAT) can help by automating\u00a0the management of your language support, [&hellip;]<\/p>\n","protected":false},"author":565,"featured_media":35422,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-35050","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>With Xamarin, you can extend your cross-platform apps with support for native speakers, reaching markets that might otherwise be overlooked. However, managing multiple languages and keeping everything current can be challenging, especially if you\u2019re not fluent in\u00a0those languages. This is where the Multilingual App Toolkit (MAT) can help by automating\u00a0the management of your language support, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35050","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/565"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=35050"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/35050\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=35050"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=35050"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=35050"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}