Detailed explanation of multi-language support for iPhone applications

Source: Internet
Author: User

IPhoneApplicationsMultiple LanguagesSupported operations are described in this article. There are several concepts: multi-language support, that is, a program supports interfaces in several languages; localization/InternationalizationIs to translate the original language support into another language.

Here we will export several other questions,

1. Support Programs with files in several languages

Ii. How to switch the interface during running

3. Can I retrieve the corresponding characters in the code?

First, let's take a look.IphoneLanguage settings. Open Settings --> International --> Language. You can switch the Language on the interface. You can switch the current Language interface to a different Language.

How does an application perform corresponding switching? If the program has the corresponding language support. Then switch over. If not. The default language is used.

In another case, when the iphone is set to a Chinese interface, the program is manually switched to English. What should we do?

I. language files of the iphone Project

In iphone xcode projects, there are usually several languages supported.
 
The supported texts of each language are stored in <language type>. lproj/Localizable. strings. For example

In English. lproj/Localizable. strings, the simplified Chinese is in Zh_cn.lproj/Localizable. strings

Localizable. strings can be formatted in either of the following formats:

 
 
  1. "_ LocaleLanguage" = "zh ";
  2. "Cancel" = "cancel ";

The other is the plist file in XML format.

 
 
  1. <? Xml version = "1.0" encoding = "UTF-8"?>
  2.  
  3. <! DOCTYPE plist PUBLIC "-// Apple Computer // dtd plist 1.0 // EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  4.  
  5. <Plist version = "1.0">
  6. <Dict>
  7. <Key> button </key>
  8. <String> buttons </string>
  9. </Dict>
  10. </Plist>

2. Operate the strings file in XCode.

What are the detailed operations in XCode?

Http://www.skylarcantu.com/blog/2009/08/19/localization-your-iphone-os-applications-in-xcode/

In the File --> New file on the main menu or the right of the Project window, select Add New ..

In the other file, select create strings File

New files can be stored in any directory, but it is best to write the name Zh_cn.lproj/Localizable. strings as follows:

Manually add translation entries to new files


 
Repeat the preceding steps for the new language.

3. Use international strings in applications

In a program, you can call NSLocalizedString to retrieve the strings of the corresponding language.

If your strings file is not the standard Localizable. strings file, you can use

NSLocalizedStringFromTable (@ "button", @ "my", nil );

For example, I change the title to an international language.

 
 
  1. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"title",nil)   
  2.                             message:self.textField.text   
  3.                                                  delegate:nil                                          cancelButtonTitle:@"OK!"   
  4. otherButtonTitles:nil]; 

4. display on the Interface

The display on the Interface depends mainly on two points. One is the display language of the current iphone. If it matches, the corresponding language is automatically called.



If the current language application is not supported, the language specified by Localization native development re in Info. plist is automatically displayed.

If the language support in the current application does not match the current one. For example, if the app is in French, German, and Italian, but the iphone is in Spanish, then the app can specify the language at any time to show the French.

How to display the specified language in the program? (To be tested)

Here is a solution. The following is a Language class for setting the current Language. The setting method is setLanguage.

Initialize is used to obtain the current language

 
 
  1. @implementation Language  
  2.  
  3. static NSBundle *bundle = nil;  
  4.  
  5. +(void)initialize {  
  6.  NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];  
  7.  NSArray* languages = [defs objectForKey:@"AppleLanguages"];  
  8.  NSString *current = [[languages objectAtIndex:0] retain];  
  9.  [self setLanguage:current];  
  10.  
  11. }  
  12.  
  13. /*  
  14.   example calls:  
  15.   [Language setLanguage:@"it"];  
  16.   [Language setLanguage:@"de"];  
  17. */  
  18. +(void)setLanguage:(NSString *)l {  
  19.  NSLog(@"preferredLang: %@", l);  
  20.  NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];  
  21.  bundle = [[NSBundle bundleWithPath:path] retain];  
  22. }  
  23.  
  24. +(NSString *)get:(NSString *)key alter:(NSString *)alternate {  
  25.  return [bundle localizedStringForKey:key value:alternate table:nil];  
  26. }  
  27. @end 

Class calls in the program

Summary:IPhoneApplicationsMultiple LanguagesThe content of the supported operations has been introduced. I hope this article will help you!

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.