2

I have added this file in my project with the code:

Language.h:

@interface Language : UIViewController

@end

@implementation Language

static NSBundle *bundle = nil;

+(void)setLanguage:(NSString *)l {
    NSLog(@"preferredLang: %@", l);
    NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
    bundle = [[NSBundle bundleWithPath:path] retain];
}

+(void)initialize {
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString *current = [[languages objectAtIndex:0] retain];
    [self setLanguage:current];

}

Here this method +(NSString *)get: is not getting called

+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
NSLog(@"Bundle path: %@", [bundle bundlePath]);

    return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end

And in my viewController.m file where I have two buttons for change of language Turkish and English Language:

-(IBAction)englishLang:(id)sender{

[Language setLanguage:@"en"];
NSLog(@"language to english");

}

-(IBAction)turkishLang:(id)sender{

[Language setLanguage:@"tr"];
NSLog(@"language to turkish");
}

The issue is when I click on turkish lang button, the language should be changed and the next nib file should be turkish localized, picked up by the application. But I am not getting that behavior from my application.

It only loads the english localized nib file.

What can be the issue ?

Omer Waqas Khan
  • 2,423
  • 4
  • 33
  • 62

3 Answers3

0

Okay a few things here:

1) Why do you retain in +initialize the current string? That's going to leak.

2) What did you expect? You're only changing the language on a very high level. The app definitely doesn't observe these settings to instantly adjust the language. It might work after relaunching the app but it's not the way to solve this.

3) Why would you want to change the language within an app? Nobody will ever expect this feature in your app. So I'd just ditch this feature because it's unnecessary.

lbrndnr
  • 3,361
  • 2
  • 24
  • 34
  • actually its my requirement, I have to implement this, I have got this code from this question http://stackoverflow.com/questions/6185627/internationalization-in-iphone-application, please help me resolving this – Omer Waqas Khan Jan 31 '12 at 14:31
0

If you want to use an app language that isnt based on the phones language you need to do a couple of things:

  • make your own version of NSLocalizedString which will return the string based on your language, not the phones (it sounds like this is the main problem, if you have NSLocalizedStrings through the app, the value returned will only ever be the same as the language of the phone)
  • post notifications when the language is changed so that your views can refresh with the new content
wattson12
  • 11,176
  • 2
  • 32
  • 34
  • have made localized strings but still the issue is same – Omer Waqas Khan Jan 31 '12 at 15:10
  • have you tried logging the bundle path on the get method to see if its the right one? – wattson12 Jan 31 '12 at 15:17
  • i didn't get that, how to do that – Omer Waqas Khan Jan 31 '12 at 18:39
  • to confirm that you have changed the bundle to be the turkish, add this line at the start of the get:alter method: NSLog(@"%@", [bundle bundlePath]); – wattson12 Jan 31 '12 at 19:43
  • I'd say you are not refreshing your view controllers, so the data is not being gotten again. You should send out a notification after setting the language to tell the view controller to refresh – wattson12 Feb 01 '12 at 09:33
  • can you help me with code snippet ? that how can I do that, because I have tried but did succeeded. – Omer Waqas Khan Feb 01 '12 at 09:39
  • its a bit beyond a code snippet. read up on [notifications](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html) and view controllers, its too complicated to answer in a chat. ask more questions if you have specific problems – wattson12 Feb 01 '12 at 09:50
0

Your issue is that the nib loading doesn't know that it should use your own i18n method. So you need to fill labels etc programmatically in the viewDidLoad method

JeanLuc
  • 4,783
  • 1
  • 33
  • 47