Add Languages to Your Xamarin Apps with Multilingual App Toolkit
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’re not fluent in those languages.
This is where the Multilingual App Toolkit (MAT) can help by automating the management of your language support, so you can focus on what you do best: build great cross-platform apps!
Before getting started, it’s important to note that most project templates are not localization enabled. Fortunately, adding localization support is straightforward. Once you do the initial setup, your app will be ready to travel the world.
Getting Started
Let’s look quickly at this process using a Xamarin.Forms sample. The LocalizationSample project has all the plumbing and is available on GitHub.
Wiring up localization can be done quickly using a NuGet called Multilingual (Localization) Plugin for Xamarin and Windows. The documentation is straightforward, with the core being the addition of the TranslationExtension
class to provide XAML binding for your app’s resources. Here’s the code-snippet Plugin.Multilingual
used to wire up access to the resources:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[ContentProperty("Text")]
public class TranslateExtension : IMarkupExtension
{
const string ResourceId = "LocalizationSample.Resources.AppResources";
static readonly Lazy resmgr =
new Lazy(() =>
new ResourceManager(ResourceId, typeof(TranslateExtension)
.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 AppResources.resx
and “HelloWorld” with the value “Hello from App Resource” to the new RESX file:
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 TranslateExtension
and applying the binding, like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?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="20">
<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 English string “Hello from App Resource” as well as the still hard-coded text values:
Traditionally, after ensuring the localization is 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 be a challenge with only one or two additional languages, but becomes daunting very quickly as more languages are added.
It’s best to validate that your app’s language support is working correctly throughout the development process to avoid bugs that may delay your app’s release, or worse, cause you to reduce market support by cutting additional language support.
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 have the translations proofed and adjusted by family, friends, co-workers, or translation vendors.
Install and Enable
To Install the Multilingual App Toolkit(MAT), just use Visual Studio’s Tools -> Extensions and Updates menu:
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 not support 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 be set to the language you use in your AppResources.resx
file. In this example, it will be English (en-US):
MAT needs to be enabled for any project that has localizable text. This is done by selecting the project in the Solution Explorer, 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 will add two files for every language selection, an .XLF fileand the corresponding .RESX file. 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.
Selecting one or more .XLF file enables the menus that 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 is set to XliffResource
. 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:
Now it’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’s language pick the app’s language automatically.
Add Languages to Your Xamarin Apps with Multilingual App Toolkit的更多相关文章
- WPF 应用程序使用 Multilingual App Toolkit
应用程序支持多语言,使用 Multilingual App Toolkit是一个不错的解决方案. Multilingual App Toolkit下载地址: https://visualstudiog ...
- Multilingual App Toolkit v2.2 release
Multilingual App Toolkit v2.2 release Today we released Multilingual App Toolkit v2.2. This release ...
- WPF 多语言解决方案 - Multilingual App Toolkit
1.首先安装Multilingual App Toolkit 2.新建项目,在VS中点击"工具" -> "Multilingual App Toolkit&qu ...
- webstorm 开新项目 setting 设置@目录别名 add @ (languages & Framewors - Javascript - Webpack 4. setting eslint enable
webstorm 开新项目 setting 设置@目录别名 add @ (languages & Framewors - Javascript - Webpack 4. setting esl ...
- 【Xamarin.Forms 2】App基础知识与App启动
系列目录 1.[Xamarin.Forms 1]App的创建与运行 引言 本篇文章将介绍Xamarin.Forms中 App 基础知识和 App的启动. 开发环境 Visual Studio 2019 ...
- Add AI feature to Xamarin.Forms app
Now, AI is one of important technologies.Almost all platforms have API sets of AI. Following list is ...
- Zero to Build: Create new Xamarin apps in minutes with AppMap
Creating a new Xamarin.Forms app can be an intimidating task, especially if you add in content pages ...
- 将packages/apps/下的app导入eclipse
当刚接触android自带的一个模块时,如何去熟悉它?相信不少人第一步都会尝试着去了解其内容的调用流程,而此时若能够单步调试则显得非常重要了,于是有了文章标题所说的尝试. 作者这里要导入的是Setti ...
- (C#)Xamarin.ios 发布到 App Store
项目做到尾声了,IOS要发布,程序猿力Max来了. 不过就公司开发者账号就弄了一个月多,期间因为申请过D-U-N-S客服联系要公司资料时我们中途说取消了,后来再申请不知多少次了都没再回复... 给美国 ...
随机推荐
- 蓝牙BLE设备断线回连分析
在 文章中分析了Hogp的连接的流程 ,这里分析一下回连的流程. 在使用ble设备的过程中,我们发现当设备和主机配对之后,如果没有解除配对,那么即便设备和主机断开,那么也是可以重新连接而不需要重新走配 ...
- npm install 安装报错:npm ERR! EPERM npm ERR! -4048 npm ERR! Error: EPERM: operation not permitted, unlink 'D:\test\demo\code\materialT\node_modules\.staging'
更新项目依赖包,删除掉package-lock.json.node_modules,运行npm install,报如上错误信息,查询资料说是没有权限,本人用管理员身份打开powershell,运行np ...
- 你知道Java的四种引用类型吗
关于java四种引用类型,我也是刚了解,特此记下! 在Java中提供了四个级别的引用:强引用,软引用,弱引用和虚引用.在这四个引用类型中,只有强引用FinalReference类是包内可见,其他三种引 ...
- web安全:通俗易懂,以实例讲述破解网站的原理及如何进行防护!如何让网站变得更安全。
本篇以我自己的网站为例来通俗易懂的讲述网站的常见漏洞,如何防止网站被入侵,如何让网站更安全. 要想足够安全,首先得知道其中的道理. 本文例子通俗易懂,主要讲述了 各种漏洞 的原理及防护,相比网上其它的 ...
- Dijkstra的应用
每次只涉及一边两端点的极值循环转移应用Dijkstra.
- iOS- 利用AFNetworking(AFN) - 实现文件断点下载
https://www.cnblogs.com/qingche/p/3500746.html 1. 定义一个全局的AFHttpClient:包含有 1> baseURL 2> 请求 3&g ...
- sqlserver2008 传入的表格格式数据流(tds)协议流不正确。
起因是在sql 2008 里使用 sql prompt 报了一个内部连接致命错误,原本应该想到是数据库连接问题的,奇怪的是能连接上数据库也能查询表仅仅是用不了工具没有智能提示. 几经查询无果,度娘上之 ...
- stark组件的分页,模糊查询,批量删除
1.分页组件高阶 2.整合展示数据showlist类 3.stark组件之分页 3.stark组件之search模糊查询 4.action批量处理数据 4.总结 1.分页组件高阶 1.分页的class ...
- 初次使用beego框架
安装beego框架以及bee工具 go get -u github.com/astaxie/beego go get github.com/beego/bee 创建一个新项目 bee new weba ...
- 【kindle笔记】之 《明朝那些事儿》-2018-7-1
[kindle笔记]读书记录-总 最近在读这本书.之前在微信读书里断断续续读过,读到深处还想蹦起来做笔记那种.后来种种原因断了,再没续上. 现在又开始啦.最近还在重八兄造反阶段,还很早呢,有时候晚上玩 ...