How to change Language in iPhone?

In this example we will see how to change the language in the iPhone using .xib file. If we change the language into the French from the settings of iPhone, then run the application it will translate the text into the French language. So let see how it will be worked.
Step 1: Create a View base application using template. Give the application name “LanguageChange”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Expand classes and notice Interface Builder created the LanguageChangeViewController class for you. Expand Resources and notice the template generated a separate nib, LanguageChangeViewController.xib, for the “LanguageChange”.
Step 4: In the LanguageChangeAppDelegate.. file make the following changes in the file:
(void)applicationDidFinishLaunching:(UIApplication *)application {

  UINavigationController
 *navigationController = [[UINavigationController   alloc] initWithRootViewController:viewController];
 
 [window addSubview:navigationController.view];
 
 [window makeKeyAndVisible];
}
Step 5: Select the NIB files from the Groups & Files, right click on LanguageChangeViewController.xib file and select Get Info . Select General tab and click on “Add Localization” in bottom left. In popup type fr then select Add button. You will see fr will be added in Info window.Then you will see there are two .ib files are available one LanguageChangeViewController.xib(en) and another LanguageChangeViewController.xib(fr) file added in the NIB file.
Step 6: Double click Your LanguageChangeViewController.xib(en) file and open it to the Interface Builder. Open the View window and drag the label from the library and place it to the View window. Select the label from the View window and bring up Attribute Inspector and change the text(See the figure1). Connect File’s Owner icon to the View icon and select the view. Now save the .xib(en) file, close it and go back to
Step 7: Now open the LanguageChangeViewController.xib(fr) file and open it to the Interface Builder. Open the View window and drag the label from the library and place it to the View window. Select the label from the View window and bring up Attribute Inspector and change the text into french language (See the figure2) (You can use google translator). Connect File’s Owner icon to the View icon and select the view. Now save the .xib(fr) file, close it and go back to the Xcode.
Step 8: Open the LanguageChangeViewController.m file and make the following changes in the file:
(void)viewDidLoad {
  NSLocale *language = [NSLocale currentLocale];
  NSString *titleOFApplication = [language   displayNameForKey:NSLocaleIdentifier   value:[language localeIdentifier]];
  self.title = titleOFApplication;
  [super viewDidLoad];
}

Step 9: Its all done , now just compile and run the application in the Simulator (See the figure 3). Simulator will show you the text in english.




 
Step 10: If you want to see output in french language then select the setting application from the iPhone -> General ->International ->  Language -> select french language and save it. Now run the application, you will see the output in French language.




Popular posts from this blog

How to use nsxmlparser in Iphone SDk?

About Xcode 4 features

Blocks in iPhone?