2

I have a requirement to switch UI language at run-time by opening and selecting a popup menu. I did some reading on cocoa's internationalization but found it not suitable for my project since it requires user to make changes in the system setting.

Post like "http://stackoverflow.com/questions/1669645/how-to-force-nslocalizedstring-to-use-a-specific-language" suggests setting the AppleLanguages would also require an app-restart.

I wonder if I can (a) create multiple NIBs and call initWithNib according to the current language or (b) use one NIB but create a plist of strings and make a function to set title string for all the texts in all my viewWillAppear. Which solution sounds better or please let me if there is a third way.

Thanks Leo

leo
  • 1,011
  • 12
  • 25

1 Answers1

0

The simplest way I can think of is reloading all your views, and updating the text property of all text components (such as UILabel, UITextField, etc.). The language specific text to be set can be returned by a method of yours, something like-

-(NSString*) localizedStringForKey: (NSString*)key andLanguage: (NSString*)lang
{
    if([lang isEqualToString:@"en"])
    {
         if(key == @"hello")
              return @"Hello";

         //and so on...
    }

    else if([lang isEqualToString:@"fr"])
    {
         if(key == @"hello")
              return @"Bonjour";

         //and so on...
    }

} 

P.S: Here I am returning hard-coded values, but you can read them from language-specific plists of yours.

halfer
  • 19,824
  • 17
  • 99
  • 186
Akshay
  • 5,747
  • 3
  • 23
  • 35