Sunday, 17 April 2011

Advance Localization in ios apps

Localization in ios is usually done using this way. But the main problem with this approach is that to view the application in other language the language has to be changes from the settings of the ios device.
In this post i will present an approach in which you can set the language of the application from within the application.
but before starting lets understand the current localization procedure. we use NSLocalizedString() to get the localized version of any string, which acts as a key to match the key value pair in the localized version of Localizable.strings file.
what infact NSLocalizedString() calls in background is [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] where [NSBundle mainBundle] is the default main bundle.
The trick to use is to get the value of localizedStringForKey from the bundle of your choice. So what we will do is that depending on the language selected we will select the specific bundle and use the [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] to get the localized value of the string.
so lets get started


1- Localize your application using the method describe here. i recommend using this method just so that you can use "genstrings" to create string table.
2- put the localized version of string values in the localized Localizable.strings.
3- Replace the NSLocalizedString with any function name of your choice, i will be using  languageSelectedStringForKey .
4- Write a method definition and implementation of -(NSString*) languageSelectedStringForKey:(NSString*) key where it is accessible in the scope where the localized string is needed.
5- in the languageSelectedStringForKey method select the appropriate bundle depending on the language selected and call [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil] on the bundle.
e.g.
for NSString *path= [[NSBundle mainBundle] pathForResource:@"fr" ofType:@"lproj"];  
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
and then get the string like this
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
str here is the localized string.


again all which i described above have been put in this code and i have placed it on github for any reference.

28 comments:

  1. This is a great post .
    It help me solved my problem..
    Ananthasri.

    ReplyDelete
  2. Wow, thank you so much! Finally a solution that works just like I want it! I've condensed your main function down to the essentials in this pastie:

    http://www.pastie.org/1976594

    ReplyDelete
  3. how to use this method to localize a button text?

    ReplyDelete
  4. You can also set the text of button programatically
    [myButton setTitle: @"myTitle" forState: UIControlStateNormal];

    here set the @"myTitle" using the above method.

    ReplyDelete
  5. It was very useful for me dude!
    Thank you very much!

    ReplyDelete
  6. Why bother when http://www.traductoapp.com/traductopro automates the entire localization for you?

    ReplyDelete
  7. Hello, how are you?
    His method works, however I have a doubt. How do I get the whole application to be "located", I say, we have a variable LocalizedText.h, but if I have another view? How do I call the method - (NSString *) languageSelectedStringForKey: (NSString *) key in the other views?

    I tried putting this in a separate class and could not.

    My mail is: asrsantos@gmail.com

    Thanks

    ReplyDelete
  8. Hi Adriano,

    You can make a singleton. This singleton will implement the "languageSelectedStringForKey" method. Which than you can call from anywhere within your project.
    Read here how to make a singleton class.
    http://iphone.galloway.me.uk/iphone-sdktutorials/singleton-classes/

    ReplyDelete
  9. Very good, that fasten. Thanks very much.

    ReplyDelete
  10. Thank you for the post. Are you aware of how to switch between localized xib files? For an example if you compare Arabic and English languages there is a huge difference of rendering the content. Arabic content should be displayed from right to left. So we need to create the user interface specific to it. How can we switch to the Arabic specific localized XIB user interface upon clicking the language button?

    ReplyDelete
  11. @Amilla In that case above solution may not work for you, Make two nibs and switch between them using
    + (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner

    More description at: http://stackoverflow.com/questions/4498822/how-to-change-dynamically-the-xib-of-a-uiviewcontroller

    ReplyDelete
    Replies
    1. Thank you very much for your reply.

      Delete
  12. sorry i can't get it .. how to use loadNibnamed to load different nibs using your methods? can you give me a little more details

    ReplyDelete
    Replies
    1. ok i got it myself :D :D

      - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
      {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
      NSString *path= [[NSBundle mainBundle] pathForResource:@"ar" ofType:@"lproj"];
      NSBundle* languageBundle = [NSBundle bundleWithPath:path];
      NSArray *nibObjs = [languageBundle loadNibNamed:@"MainViewController" owner:self options:nil];
      UIView *aView = [nibObjs objectAtIndex:0];
      self.view = aView;
      }
      return self;
      }


      i put it here for others when they reach your page :D

      Delete
  13. This comment has been removed by the author.

    ReplyDelete
  14. but i found a problem in this approche that i can't find why it happens .. when i used this approach of calling this in initWithNibName the viewDidLoad is never get called ..why is that..

    ReplyDelete
  15. Thanks for the post..I;m seeing that UIKeyboard is not localized..it comes in english..which should not be the case..could you check this out..thanks

    ReplyDelete
  16. Unfortunately there is no way to control the keyboard language programmatically.

    ReplyDelete
  17. Or you can use an online localization tool such as https://poeditor.com/. It does its job perfectly well.

    ReplyDelete
  18. What will be happen with jailbreak for getting more apps in the iPhone devices?
    read more

    ReplyDelete
  19. Very Informative. Thanks for Sharing. Keep it up.

    iPhone Applications Development

    ReplyDelete
  20. The method described in http://www.iphonesdkarticles.com/2008/11/localizing-iphone-apps.html isn`t available anymore... Can you give us a hint?

    ReplyDelete
  21. Nice post, thanks for sharing. Any idea how could I have the same functionality with the Storyboard's strings?

    ReplyDelete
  22. This translation tool could help a lot in the iOS localization process: https://poeditor.com/. It offers possibilities of automatic translation for strings and also of collaborative translator work. It has API and Github integration also.

    ReplyDelete
  23. How to localization more than one languages. ?
    can i localization 5 languages ?

    ReplyDelete
  24. thanks a lot sir...superb article

    ReplyDelete