iOS app Internationalization

Source: Internet
Author: User
Tags time zones home screen

Rotor Cocoachina Address: http://www.cocoachina.com/industry/20140526/8554.html

iOS app internationalization tutorial (version 2014)

This tutorial will take you through an app called Ilikeit to understand the most basic internationalization concepts and add international support for your app. The example app has a label and a you-like? button, the user whenever you click on the like?, some optimistic sales number

This article was originally written by Sean Berry and was updated by Ali Hafizji for iOS 7.

It's great to develop a great iOS app, but there are more things to do than good code, gorgeous design, and intuitive interaction. Being in the forefront of the App Store leaderboard also requires a timely product marketing, the ability to expand the user base, practical tools, and the widest possible access to user technology. For many developers, the international market is an afterthought, but since the App Store offers a seamless global sharing model, any iOS developer can publish the app to more than 150 countries with one click. Asia and Europe represent a growing market for potential customers, many of whom are not native speakers of English, but in order to make the app take full advantage of the market's potential, you should at least internationalize the application language. This tutorial will take you through an app called Ilikeit to understand the most basic internationalization concepts and add international support for your app. The example app has a label and a you-like? button, the user whenever you click on the like?, some optimistic sales data and the corresponding picture will fade from the button below the display. Now, however, the app is only available in English.
Note: Another important aspect of internationalization is the use of auto layout to change the size of text. However, to make this tutorial as simple as possible, we will not focus primarily on auto Layout. For the topic of auto layout, we have another tutorial.
Internationalization vs Localization ( Internationalization vs Localization )Before you begin this tutorial, it is important to understand the differences between internationalization and localization, and many people often confuse the two concepts. To put it simply, internationalization is a process of international compatibility design for applications, such as: 1. Process text input and output in the user's native language; 2. Handle different dates, times, and number formats; 3. process data using appropriate calendars and time zones; internationalization is an activity that you and a developer can achieve by leveraging the API provided by the system and make some additions and modifications to the code to make it as good as the Chinese, Arabic, and English versions of the app. In contrast, localization is simply translating the app's user interface and resources into different languages, which you can and should give to others, unless you can be proficient in every language that the app should support. Start Now ( Getting Started )The first step is to Downloadilikeit the project and we'll use it throughout the tutorial.  To open the project in Xcode 5 and run it on the emulator, you will see the following interface: From there, you need to localize four items: UI elements: Hello labelui elements: you like? Button sales Data text: Yesterday you sold 1000000 apps picture text: I like IT spends a little time browsing through files and folders to familiarize yourself with the project structure. Main.storyboard contains a single screen, which is an instance of the Viewcontroller class. Detach text from codeCurrently, all the text shown in the app is in Main.storyboard and viewcontroller with hard-coded strings. In order to localize these strings, you need to put them in a separate file. Instead of hard-coding in your method, you will simply refer to these strings in the package. Xcode uses a file with a ". Strings" extension to store and retrieve all the strings used in the app to support each language. Depending on the language currently used by the iOS device, a simple method call in the code will find and return the required number of characters. Try, open File>new>file, select Resource Strings Fils, click Next, Name the file localizable.strings, then click Save.
Note: localizable.strings is the default file name that iOS uses to localize text. Suppress the urge to name it with something else, or you will enter it again and again each time you reference a localized string. strings file name.
Now that you have created the Localizable.strings file, you need to add all the text--the current hard-coded text in the app. You need to follow a specific but simple format:
    1. "KEY" = "CONTENT";
These key/content pairs function like nsdictionary, and the Convention is to use the default content translation as the key for the content: for example, you should write:
    1. "Like?" = "Do you like ?";
A key/content pair can also contain a formatted string:
    1. "Yesterday sold%@ apps" = "Yesterday You sold%@ apps";
Now switch to VIEWCONTROLLER.M and find the Viewdidload method, now the app will set the text for Likebutton and Salescountlabel, as shown below:
    1. _salescountlabel.text = [NSString stringwithformat:@"Yesterday You sold%@ apps", @ (1000000)];
    2. [_likebutton settitle:@"You like?" Forstate:uicontrolstatenormal];
Instead, you need to read the string from the Localizable.strings file you created earlier. Use a macro named Nslocalizedstring to modify the two lines of code as follows:
    1. _salescountlabel.text = [NSString stringwithformat:nslocalizedstring (@"Yesterday you sold%@ apps", nil), @ (1000000 )];
    2. [_likebutton settitle:nslocalizedstring (@"You like", nil) forstate:uicontrolstatenormal];
The macro package wraps a slightly longer code fragment into a more manageable length, which is created using the # define directive. If you want to know what a nslocalizedstring macro is, you can hold down the control key and click on nslocalizedstring to see it defined as follows:
    1. #define Nslocalizedstring (key, comment)
    2. [[NSBundle Mainbundle] Localizedstringforkey: (key) value:@"" Table:nil]
In the current language, the Nslocalizedstring macro uses the Localizedstringforkey method to find a string for a given key value. It passes nil for the table name, so it uses the default string file name (localizable.strings). For more details, see Apple's NSBundle Class Reference.
Note: This macro takes a comment as a parameter, but it doesn't seem to work. Unlike the previous need to manually type each key/value pair into the localizable.strings, you can also use the iOS SDK with a tool called Genstrings for Automatic Processing (great for large projects).
If you use this method, you can add a comment to each string, and the comment will appear on the default string edge as an aid to the translator. For example, you can add a comment that indicates where the string is used. Already have enough background information, now let's start! Create and run your project, and it should show the same text on the home screen as before, but where is the Spanish? Now that your app has been localized, adding translations is a trivial matter. add Spanish localization ( Adding a Spanish Localization )To add support for another language, you can click the Ilikeit project folder in the left pane, select Project (not Target) in the pane next to it, and then under the Info tab you will see a localizations partition. Click on "+" and select Spanish (es).   Next screen will ask you which files need to be localized. Select All files and click Finish. Note: Localizable.strings is not shown in this list, but don't panic!   At this point, Xcode has set up some directories behind the scenes that contain different versions of Infoplist.strings and Main.storyboard for the language you choose. You can use the Finder to open the project folder to see what you see: see En.lproj and Es.lproj? They contain a specific language version of the file.  en is the localized code for 中文版, and ES is the localized code for Spanish. For other languages, refer to the full list of language codes.   From now on, when your app wants to get an English version of a file, it goes to en.lproj, and when it wants the Spanish version of a file, it goes to es.lproj. Very simple! Put your resource files in the appropriate folder, and the rest will be in the hands of iOS.   But wait, what about localizable.strings? If you want Xcode to know that you want to localize it, you can select the file in the left pane and then open it in the right pane. Inspector.   You will see a localize tag, click and select English (because it is currently in English), and finally click Localize.   Now the File Inspector panel will show the language to which the files belong. Currently, as you can see, the files are localized only in English. You can add Spanish localization by clicking the box on the left side of Spanish.   Go back to the left pane and click the arrow next to Localizable.strings, which will show the child elements. There are now two versions of the file: one in English and one in Spanish. To change the text in Spanish, you can choose Localizable.strings (Spanish) and replace it with the following content:
    1. "Yesterday You sold%@ apps" = "Ayer le vendió %@ Aplicaciones ";
    2. "You like?" = "~es bueno?~";
Congratulations! Now your app is supported in two languages! To test and verify that everything is working properly, you can change the display language to Spanish on the emulator/device by opening the Settings app and choosing: Espanol, Language, International, general. If Xcode debugger is still running, you can click "Stop" in Xcode and then click "Build & Run" and you will see: Region vs Language (locale vs Language)1 million is a pretty good sales data, we can add some formatting to it to make it look better! Open VIEWCONTROLLER.M and replace the line of code that sets the text for _salescountlabel with:
    1. Nsnumberformatter *numberformatter = [[Nsnumberformatter alloc] init];
    2. [Numberformatter Setnumberstyle:nsnumberformatterdecimalstyle];
    3. NSString *numberstring = [Numberformatter stringfromnumber:@ (1000000)];
    4. _salescountlabel.text = [NSString stringwithformat:nslocalizedstring (@"Yesterday you sold%@ apps", nil), Numberstring];
Compile and run the application, then the numbers will be easier to identify. It was great for Americans, but in Spain, 1 million wrote "1.000.000″ instead of" 1,000,000″. To run the application in a Spanish environment, you will see a comma separated by 0. In iOS, number formatting is based on region/country, not language, so in order to understand how a Spaniard can view sales data, open Settings.ap and navigate to change the area: International Spain, Spanish, format, and now you'll see the correct number format here: With a little extra upfront work, Nsnumberformatter will automatically format your numbers for the right area. If possible, refuse to reinvent the wheel, because on iOS, it's usually done in Apple's way to be rewarded. International Storyboards (internationalizing storyboards)Elements in storyboard, such as tags, buttons, and pictures, can be set in code or directly in storyboard. When setting up text programming, you've learned how to support multiple languages, but the "Hello" tab at the top of the screen has no iboutlet and can only set its text in Main.storyboard. You can add a iboutlet, connect it to a label in Main.storyboard, and then use nslocalizedstring to set its text properties, just as you would with Likebutton and Salescountlabel. But here's a simpler way to localize the storyboard element without any additional code. Open the small triangle on the left side of the Main.storyboard and you will see Main.storyboard (Base) and Main.storyboard (Spanish). Click Main.storyboard (Spanish) to open the editor and you will see the localized text in storyboard. You already have a Hello tag entry, as follows:
    1. /* Class = "Ibuilabel"; Text = "Hello"; ObjectID = "pup-yc-27w"; */
    2. "Pup-yc-27w.text" = "Hello";
Replace two "Hello" with "Hola" in Spanish translation:
    1. /* Class = "Ibuilabel"; Text = "Hola"; ObjectID = "pup-yc-27w"; */
    2. "Pup-yc-27w.text" = "Hola";
Note: Never change the auto-generated objectid directly, or copy and paste the upper line of code, because the Objectid of the label may have been different from the one shown above.
Internationalization of images (internationalizing Images)Because the application uses images that contain English text, you need to localize the images, because fragmented's English text will make your Spanish app look unprofessional, and will undermine the overall usability and market potential of your app. To localize a picture, you first need to download the Spanish version of the picture (in most browsers right click > Save As): Open images.xcassets, drag and drop the previously downloaded picture megusta.png and add to the left of the picture list to add the picture to the asset asset Catalog Asset catalogs cannot be internationalized, so you need to have a way to localize the picture. Open Localizable.strings (中文版) and add the following:
    1. "ImageName" = "ilike";
Add the following code to the Localizable.strings (Spanish) file:
    1. "ImageName" = "Megusta";
From now on, you will use ImageName key to retrieve the localized version of the picture. Open the VIEWCONTROLLER.M and add the following code to the Viewdidload method:
    1. [_imageview setimage:[uiimage imagenamed:nslocalizedstring (@"ImageName", nil)]];
If necessary, switch the emulator/device to Spanish, compile and run, and you will see the display of the localized picture. Now you have all the tools you need to localize your application for many different languages.
Note: This applies only if there is a different file name for each language. A better approach is to localize the resource folder, as described in this article.
Additional RewardsAs a final bonus, we're going to localize the app's name. Info.plist has a special file (infoplist.strings) that you can use to cover other languages in a string. To give the application a different Spanish name, you can open supporting Files > Infoplist.strings (Spanish) and insert the following code:
    1. "Cfbundledisplayname" = "Me gusta";
It changes the name of the app, as shown on Springboard. Exercise: internationalized audio files (exercise:internationalizing)Learning here, you should be familiar with the basic knowledge of internationalization. This is a simple exercise where you can test your new knowledge with two different audio files (one in English and one in Spanish) and play the appropriate files based on the language of your choice. The following is a brief description of the necessary steps: 1. Download the sample audio files. 2. Copy the Box-en.wav first audio file to the project. 3. Open the Audio file inspector, select the Localization button and make sure you have selected English and Spanish as the supported language. 4. Rename the second audio file (box-es.wav), just like the first name (Box-en.wav), and then copy it to the Es.iproj folder. 5. Make sure that you select Replace File in the Finder prompt. whitherFinal project contains all the code that you wrote in the tutorial. Now that you know the basic technology required to internationalize an iphone app, add a foreign language support for your existing app, or add a foreign language support when designing your next app. As you can see, this takes little time, and it will allow your app to reach a broader and more diverse audience, and your non-English speakers will thank you! For the actual translation, you can use Google's free translation Service (http://www.google.com/translate), but the results may not be tittle. If you can spend some money, then you can consider Apple's internationalization and localization page below to list the third-party service providers. If you have any questions or suggestions about internationalization, please join the discussion forum! Original: Internationalization Tutorial for IOS [Edition]

iOS app Internationalization

Related Article

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.