Xlifftool 1.2 Free Download For Mac
1 Cross-platform Localization for Native Mobile Apps with Xamarin Christopher Miller 2 Cross-platform Localization for Native Mobile Apps with Xamarin Christopher Miller 3 Cross-platform Localization for Native Mobile Apps with Xamarin Christopher Miller Slingerlands, New York USA ISBN-13 (pbk): ISBN-13 (electronic): DOI / Library of Congress Control Number: Copyright 2017 by Christopher Miller This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
33 Chapter 2 Working with Resource Files The first to change is the Access Modifier property. By default, it is set to Internal, and only classes within the same project and namespace can access the properties. When you are targeting multiple platforms and sharing the code, you want the resources defined in the shared code as much as possible. So change the Access Modifier from Internal to Public.
Add the string values to be used in the app in a table format, as shown in Figure 2-4. The name of the string value is what is exposed as code, so the name has to follow the same rules as any other property. After adding the Name, Age, and EyeColor strings, you should see something similar to Figure 2-4.
The.resx file with text values defined Once you save the.resx file, the.designer.cs file updates. If you open up the.designer.cs file, you'll see some code for managing the resource and then properties defined for each text string. For the EyeColor string, the following code is generated: /// /// Looks up a localized string similar to Eye color. /// public static string EyeColor get return ResourceManager.GetString('EyeColor', resourceculture); Note You never want to edit the code that appears in the.designer.cs file.
The code is generated every time you edit the.resx file. If you make any manual changes to the.designer.cs file, they are lost the next time you edit the.resx file. The process works this way: when you reference a resource string at runtime, it does a lookup based on the current language and country settings (i.e., the locale). If it finds a match for that locale, it uses that set of string resources. If it can't match that locale, it tries to match on just the language. If that fails, the default resource file is used. When you add resource files for additional languages, they get a.designer.cs file, but not the autogenerated properties.
The code defined for the default language will be used for the additional languages. Because you created a.resx resource named MyText, it will be the class name for your string resources.
If the.resx file is created in a folder, it has the folder name as part of its namespace. 24 34 Chapter 2 Working with Resource Files To reference the string resource in code, use a syntax like this: eyelabel.text = MyText.EyeColor; In an XAML page, use something like this: With the strongly typed resource file, you get full IntelliSense as you are typing (this applies to both the code-behind.cs file and with the XAML page). And you get compile time validation that the resource has been defined. If you remove or rename a string resource, the code will fail to compile, and the compiler errors will show where the string resources were previously referenced.
To add a second language, just add a new resource file and name the file with the language (and culture, if used). To add Spanish, you can add a resource file named MyText.es.resx.
Now you can define the same strings that were defined for the default language. This set of translated strings work for any locale (language + country) that uses Spanish. To use Spanish in Spain, the locale code is es-es; to use Spanish in Mexico, the locale string is es-mx. Users in both locales get the same translated results. If you want to support a language for multiple countries, you can create a.resx file for each language/ country combination. The Portuguese language has two popular dialects. European Portuguese is used in the country of Portugal, and Brazilian Portuguese is the dialect used in the county of Brazil.
To create the resource file for European Portuguese, the locale code is pt-pt, where the lowercase pt is the language code and the uppercase PT is the country code. For Brazil, use the same language code and replace the country code with the code for Brazil, which is BR, defining the local code as pt-br. You can then create the resource files as MyText.pt-PT.resx and MyText.pt-BR.resx. Table 2-1 shows examples of file names by locale. Some Sample RESX File Names by Locale Language (and Country) Spanish Spanish (Mexico) Spanish (Spain) Portuguese Portuguese (Portugal) Portuguese (Brazil) Chinese (Traditional) (Taiwan) Chinese (Simplified) (PRC) Japanese Folder Name MyText.es.resx MyText.es-MX.resx MyText.es-ES.resx MyText.pt.resx MyText.pt-PT.resx MyText.pt-BR.resx MyText.zh-Hant-TW.resx MyText.zh-Hans-CN.resx MyText.ja.resx Note To quickly copy the string values from the default.resx file to the additional language file, open the.resx file in the designer. Select all the rows with the mouse, right-click on the rows, and select Copy.
Open the new.resx file in the designer, right-click in the designer, and select Paste. This process copies all names, values, and comments. Now you can just edit the values. 41 Chapter 3 Working with Multilingual App Toolkit Installation There are a couple of ways to install the MAT.
From within Visual Studio, you can install it by performing the following steps: 1. From the Tools menu, select Extensions and Updates. The Extensions and Updates dialog displays, which lets you install and update various addins for Visual Studio.
In the panel on the left side of the dialog, click Online. You see a list all available extensions that can be installed. In the search box located in the upper-right corner of the dialog, type multilingual.
This process filters the list of extensions and should show Multilingual App Toolkit at the top of the list (see Figure 3-1). At the time of this writing, version 4 is the latest edition. Selecting the Multilingual App Toolkit from the Extensions and Updates dialog 4. Select the Multilingual App Toolkit, making sure to get the latest version. A download button lights up. Press the download button and follow the prompts to download and install the MAT. Restart Visual Studio if prompted do so.
32 42 Chapter 3 Working with Multilingual App Toolkit Another way to download and install the MAT is from the Visual Studio Gallery page. Follow these steps: 1. Shut down Visual Studio if it running. With a web browser, go to 3. In the Find search box, enter Multilingual App Toolkit and press Enter.
The same list of extensions that was displayed from within Visual Studio is shown. Select the one labelled Multilingual App Toolkit v4.0 (or newer), which brings up the same extension that was displayed within Visual Studio, but with additional information. You can see reviews and a Q&A section. Press the download button to download the installer for the Multilingual App Toolkit. It will be in the.msi format. After downloading the.msi file, open up the folder that contains it.
Right-click on the file and select Install. Restart Visual Studio. Using the MAT Using the MAT is pretty straightforward. You create an app, put your text in a.resx resource file, enable the MAT, translate the text, test it, and finally ship it. To illustrate this process, you ll create a very simple Hello World app by using Xamarin.Forms. This app is a very simple one that shows how to use the resource files and how to change the language at runtime.
Setting the language at runtime is different in UWP than it is with Xamarin.Android and Xamarin.iOS. You can use the Xamarin.Forms DependencyService to call platform-specific functionality from the shared codes. The app displays a label and, with the tap of a button, you can change the language of the label from English, to Spanish, and then to Chinese. Being able to change the language at runtime is a great tool to have at your disposal because you can quickly test different languages without having to force the device or emulator into another language. The author once changed his Windows Phone at a conference into another language and could not read the other language to set it back to English. Fortunately for the author, one of the MAT developers was at the same conference and had the same phone. By comparing the phones side by side, the author could find the language setting and was able to use his phone again.
Start up Visual Studio and create a new project. Select the Blank Xaml App (Xamarin.Forms Portable) template, which creates a solution with multiple projects, one for each target platform. Plus one more project that will contain the shared code as Portable Class Library (PCL). You can do this with a Shared Asset Library, but you ll use PCL for this example.
The New Project dialog should look like Figure. 44 Chapter 3 Working with Multilingual App Toolkit Figure 3-3. Solution Explorer shows the files created by the Blank XAML Xamarin.Forms template The first thing that to do is edit the MainPage.xaml file. This page contains the XAML needed to render the page. The default code in that page should look something like this: You will make a few changes to this XAML file: replace the default label with a new label and some buttons. Replace the Label control with the following.
45 Chapter 3 Working with Multilingual App Toolkit VerticalOptions='Center' HorizontalOptions='Center' / The top-level StackLayout container organizes the controls from top to bottom. The first control in the StackLayout is another StackLayout container, which uses a horizontal layout and arranges its child controls from left to right. Inside the inner StackLayout are three button controls. To keep this example simple, you can assign click event handlers for the buttons at runtime. To do so, you have to assign the Name property of each control. After the inner StackLayout, a Label control is defined. Now you can add the resource files for the language translation.
Create a resources folder to keep the project neat and tidy. Then add a resource file named MainPageText.resx to the resources folder. Edit the.resx file to include a string resource named Welcome (see Figure 3-4). Resource file with the string value that will be localizede Save that.resx file. Now you can enable the MAT for the shared code project.
From the Tools menu, select Multilingual App Toolkit and then Enable selection, as shown in Figure 3-5. Enabling the MAT With the MAT enabled, you can add some languages to the project. From the Project menu, select Multilingual App Toolkit and then choose Add Translation Languages (see Figure 3-6). 46 Chapter 3 Working with Multilingual App Toolkit Figure 3-6.
Invoking the Add Translation Languages dialog You see a dialog that lists all the languages that are supported, as shown in Figure 3-7. Many of the languages will have translation providers listed to the left of the language name. Languages that have providers available can be machine translated by the MAT.
37 47 Chapter 3 Working with Multilingual App Toolkit Figure 3-7. Translation Languages dialog There are two providers available. The first one, Microsoft Language Portal Provider, makes use of the professionally translated strings from Microsoft products. The second provider is the Microsoft Translator Provider, which uses Microsoft Translations Services (the same engine that the web site translate.bing. Select the Spanish (es) and Chinese (Simplified) (zh-hans) languages and press OK. The MAT creates two XLIFF 1.2 files in a MultilingualResources folder, one for each language.
You should have a HelloWorld.es.xlf and a HellowWorld.zh-Hans.xlf file (see Figure 3-8). 48 Chapter 3 Working with Multilingual App Toolkit Figure 3-8. The XLF files are now part of the solution To keep the demo simple, you can delete the Windows 8.1 and Windows Phone 8.1 projects because the Android, ios, and UWP projects are sufficient to show how resource files work. Right-click the Windows 8.1 and Windows Phone 8.1 projects and select Remove. Have the MAT machine translate the English string resource to Spanish and Chinese. Right-click the MultilingualResources folder, select Multilingual App Toolkit and then choose Generate Machine Translations. A progress dialog displays as each of the.xlf files is translated (see Figure 3-9).
Progress is updated as each.xlf file is machine translated You can view the.xlf files with the Multilingual Editor that is part of the MAT. Select the HellowWorld.
Es.xlf file in Solution Explorer. The default action when you double-click the file is to open the.xlf file inside Visual Studio. That brings the file in as code and you'll see it in all its XML glory. To get the Multilingual Editor, right-click the file and select Open With.
You should see a list of Visual Studio file editors with Multilingual Editor in the list. 49 Chapter 3 Working with Multilingual App Toolkit If you don t see Multilingual Editor in the list, click the Add button.
Under Program, add the following: '%ProgramFiles% Multilingual App Toolkit MultilingualEditor.exe' Under Friendly Name, enter Multilingual Editor. Then click OK. You now have Multilingual Editor as an option when you select Open With for the.xlf files. After the.xlf file opens in the Multilingual Editor, you should see something similar to Figure Figure Multilingual Editor The Multilingual Editor provides an easy-to-use interface for the XML content stored in the.xlf file. It shows the resource ID, default language version of the text, and translated text. The bottom part of the editor is a grid that lists all the resource strings in the.xlf file.
This example app only has a single resource string, so the contents of the grid match the contents in the entry fields. Because the text was just machine translated, the State field is set to Needs Review. If you click the State field, you can set the set of values that can be selected (see Figure 3-11).
Figure Setting the state of the translated string resource 40 50 Chapter 3 Working with Multilingual App Toolkit Depending on workflow, you can change the value of State from Needs Review to Translated or Final. If you need an approval process for the translation, you may need to use both Translated and Final. You don t need to change the State value at all to use the translated text.
These fields are included to help you manage your development workflow, but have no impact on how the files are compiled into resources. If you used machine translation and want to correct or otherwise change the translated text, you can make your edits here.
Once the value of State has been changed to Translated or Final, the MAT skips over that string the next time that machine translation is used. If you make any changes to a string resource with the Multilingual Editor, remember to press Save in the command ribbon. Once you make a change, the Save button is enabled. If you forget to press it, the editor prompts you to save your changes when you close the editor.
Next, build the project to generate the.resx files. The MainPageText.es.resx and MainPageText.zh- Hans.resx files will be created if they do not yet exist or replaced if they do exist. Now that you have the resources, it s time to use them in the code. First, you can create an interface in the shared code for changing the language. This interface defines the signature of the method that is called to change the language. The actual code to change the language is defined in each platform project file.
In the project for the portable library, add a new class and name it ICultureOverride. Then replace all the existing code with the following: namespace HelloWorld public interface ICultureOverride void SetCultureOverride(string culture); You are defining a single method named SetCultureOverride that takes a locale as a string parameter. Now that the interface is defined in the PCL, you need to define the implementation in the platform projects. The Android and ios classes are essentially identical; the UWP project uses slightly different code.
By implementing an interface, you hide the platform-specific implementations from the portable code. In the Android project, add the CultureOverride class. Replace the default code with the following: using System.Globalization; using System.Threading; namespace HelloWorld.Droid public class CultureOverride: ICultureOverride public void SetCultureOverride(string culture) Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; 41 51 Chapter 3 Working with Multilingual App Toolkit The code is fairly simple. You are defining a new class and it will implement the ICultureOverride interface. This means it needs to provide a definition for the SetCultureOverride method.
This method has just two lines. The following line sets the user locale (i.e., language and country) to the specified locale string. It controls date/time, number, and currency formatting: Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); The second line sets the language used by the UI: Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; For Xamarin.Forms to use the platform code for the interface, you need to register the class via a metadata attribute. This registration allows the Xamarin.Forms DependencyService to locate the implantation class runtime.
Above the name space definition, add the following line: assembly: Xamarin.Forms.Dependency(typeof(CultureOverride)) Because the assembly attribute is referencing a class that is defined with a nondefault namespace, that namespace has to be added to the using list. Your class should now look like this: using System.Globalization; using System.Threading; using HelloWorld.Droid; assembly: Xamarin.Forms.Dependency(typeof(CultureOverride)) namespace HelloWorld.Droid public class CultureOverride: ICultureOverride public void SetCultureOverride(string culture) Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; For ios, follow the same steps. The only difference is in the addition of the nondefault namespace. You end up with the following: using System.Globalization; using System.Threading; using HelloWorld.iOS; assembly: Xamarin.Forms.Dependency(typeof(CultureOverride)) namespace HelloWorld.iOS public class CultureOverride: ICultureOverride public void SetCultureOverride(string culture) 42 52 Chapter 3 Working with Multilingual App Toolkit Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; For the UWP app, the syntax is slightly different. Instead of setting CurrentUICulture, you set ApplicationLanguages.PrimaryLanguageOverride. So the UWP version of CultureOverride.cs looks like this: using Windows.ApplicationModel.Resources.Core; using HelloWorld.UWP; assembly: Xamarin.Forms.Dependency(typeof(CultureOverride)) namespace HelloWorld.UWP public class CultureOverride: ICultureOverride public void SetCultureOverride(string culture) Windows.Globalization.ApplicationLanguages.PrimaryLanguageOverride = culture; ResourceContext.GetForCurrentView.Reset; This definition of the CultureOverride class also behaves differently; PrimaryLanguageOverride is a 'sticky' setting.

If you change the language to Chinese and then restart the app, the app comes up Chinese as the default language. If you are using code to change the language for testing, you have need to remember that.
To avoid that behavior, set PrimaryLanguageOverride to string.empty in your startup code. Now that you have language change implementation code, you have to add code to use the string resource and to wire up the buttons to code that will change the language. In the code-behind file for the XAML page, add the following method: private void UpdateText LabelWelcome.Text = resources.mainpagetext.welcome; This method updates the Text property to the string resource named Welcome.
By default, the string resource file named MainPageText.resx provides the value of the string resource named Welcome. The.NET Framework tries to match the CurrentUICulture to the right resource file. If the language is set to Spanish, it tries to load the values from MainPageText.es.resx. If it can t find that file, the default of MainPageText. Resx is used.
When you specify both the language and the culture with the locale string, the.net Framework tries to find a match based on language and culture. If a match is not found, it tries to match by just the language.
If that match fails, the default resource file is used. This search is done for each of the individual string resources, not the entire resource file. 43 53 Chapter 3 Working with Multilingual App Toolkit Now add the method used to change the language and refresh the display: private void ChangeCulture(string culture) DependencyService.Get.SetCultureOverride(culture); UpdateText; The Xamarin.Forms DependencyService finds the platform implementation for ICultureOverride and then calls the SetCultureOverride method while passing in the locale. It then calls UpdateText to refresh the display. The final step is to wire up the button click event handlers to call the ChangeCulture method: public MainPage InitializeComponent; BtnEnglish.Clicked += (sender, args) = ChangeCulture('en');; BtnSpanish.Clicked += (sender, args) = ChangeCulture('es');; BtnChinese.Clicked += (sender, args) = ChangeCulture('zh-Hans');; UpdateText; For each button press, ChangeCulture is called with the appropriate language passed in.
For more elaborate Xamarin.Forms apps, you can leverage the MVVM functionality that is provided with Xamarin.Forms and put more of the code in the XAML. Note MVVM, which stands for Model-View-ViewModel, is a software architecture pattern that separates the data and business logic from the UI code.
(MVVM is covered in greater detail in Chapter 4.) You should be able to compile and run the app. The next chapter builds an entire app with source that can be downloaded. When you run on Android, you should see a screen that looks like Figure. 97 Chapter 4 Island Menu Application The app should run just as it did before; the difference is the way the app appears on the Android launcher. Before localizing the Label of the MainActivity, the name of the app would always appear as Island Menu (see Figure 4-10). Figure Island Menu in Android launcher, English 89 98 Chapter 4 Island Menu Application After making that change, the name of the app will now be displayed with the translated name in the Android Launcher as shown in Figure Figure Island Menu in Android launcher, Spanish 90. 100 Chapter 4 Island Menu Application Windows UWP With UWP applications, you localize the name and the description of the app.
The structure is similar to the way Android and ios handle their string resources. Because this is an UWP app, you have to use the.resw resource format instead of the.resx format. Note Starting with Windows 8 Store Apps, Microsoft changed the way localized resources were bundled with the application. With that change, the.resw file extension was introduced. The.resw file format is identical to the.resx format, except that it stores only strings and file paths.
Select the IslandMenu.UWP app, right-click the project, and add a folder named Strings. Create the following folders inside the Strings folder: de, en-us, es, and zh-hans. In the en-us folder, right-click the folder; select Add and then New Item. Select the Resources File (.resw) template and use the default name Resources.resw. It should look like Figure Figure Creating a new.resw file 92. 101 Chapter 4 Island Menu Application Press Add to add the resource file.
After the file is created, double-click the file to edit it. The resource editor displays, which looks like the same editor that you used on the.resx files. Add two string resources and name them 'AppName' and 'AppDescription'. Use the same values from the.resx file in the PCL project. The end result should look like Figure Figure Resources.resw for Windows 10 UWP in English Next, add Resources.resw files for the de, es, and zh-hans folders. Use the same 'AppName' and 'AppDescription' strings and copy the values from the.resx files for each language in the PCL project.
For example, the Resources.resx file in the es folder should look like Figure Figure Resources.resw for Windows 10 UWP in Spanish Now that the fields are localized, you need to update the app manifest to use the values from the resource files. Right-click the project file and select Properties.
When the Properties page opens up, select the Application tab on the left side, and then press the Package Manifest button. A file named Package. Appxmanifest opens. On the Application tab, set the value of the Display Name field to ms-resource:appname and change the value of the Description field to ms-resource:appdescription. The manifest page should look like Figure. 113 Chapter 5 Additional Resources Figure 5-2.
Google Translate, English to Spanish If you are using XLIFF files, you need a way to edit the files and convert them to a format that your development tools can work with. These tools enable you to edit, import, and export the files. The code examples in this book use the MAT from Microsoft, which generates XLIFF files from resource files. Table 5-3 shows some sample items defined in a resource file.
Xlifftool 1.2 Free Download For Mac Free
Sample RESX Values Name Value Comment AppDescription Providing menus to the best of the restaurants One-line description of the app AppTitle Island Menu Title of the app as it should appear on the device Welcome Welcome Simple salutation 106 The XLIFF file for the Spanish translation starts out looking like this: providing menus to the best of the restaurants providing menus to the best of the restaurants the one line description of the app island Menu island Menu.