Question

Currently im getting the current device language by this code:

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];

Now I need to update this language whenever the user change to other language in device by NSCurrentLocaleDidChangeNotification

can you suggest me how to update the language if user change to other language in device.

Était-ce utile?

La solution

There is no need to do by your self, iOS will take care for u

for to know which language -> suppose when u change the language in device->settings all the apps in background will be terminated, and u need to launch it once again in the method


your case is pretty different, so how you can implement notification for changing language, it is not possible becz app is terminated so u dont get any notification,

better u need use user defaults try this

EDITED FOR CONDITION


   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
   {


   //  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(defaultsChanged) name:NSCurrentLocaleDidChangeNotification object:nil];


    NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSLog(@"%@",language);
    NSUserDefaults *standardDefaults =  [NSUserDefaults standardUserDefaults];
    NSString *currecntLan = [standardDefaults objectForKey:@"CURRENTLAN"];
    if([currecntLan isEqualToString:language])
    {
        NSLog(@"language does't change");
    }
    else
    {
       NSLog(@"CHANGED");
      //your saving code
    }
  }


 - (void)applicationDidEnterBackground:(UIApplication *)application
 {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.


   //since u need to update the userdefaults 
   //hear u can set the defaults with current language
   NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
   NSUserDefaults *standardDefaults =  [NSUserDefaults standardUserDefaults];
   [standardDefaults setObject:language forKey:@"CURRENTLAN"];


}


END EDITE FOR CONDITION

  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

  NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
  NSLog(@"%@",language);//it prints "en" for english

  ..update it to server heare 


  • select the your project->info->localisation->"add the languages that u want to suppourt"
  • info.plist file is added to your project, in that select the language u want to suppourt
  • edit that file in the format key = value; //heare key is the base language for your help and the value is the supported language.

for example, heare i added the "spanish" language its stringfile look like below

 "Automatic Play"    = "juego automático";
 "Manual Play"       = "juego manual";
 "Settings"          = "Ajustes";


for another string file for japanese language it will look like below


"Automatic Play"    = "自動再生";
"Manual Play"       = "手動遊び";
"Settings"          = "設定";


after that u need to use some macros in your programming file for example

   [aButton setTitle:NSLocalizedString(@"Automatic Play", nil) forState:UIControlStateNormal]; //use like this hear "Automatic Play" is the key that u specify

...and for others also

just use like this and the value for each key will be updated when you changes the language in your device there is no extra work that you need to do

there are lots blogs out there u can follow it.

Autres conseils

Need not worry, just check the language when app launched.

Every time you change device language, you app will be relaunched.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top