3

Now, in standard behavior of localization, the iOS determines the currently set Language of iPhone and uses the Localizable.strings file to set the appropriate text.

My client, however, requires a multi-language iOS application in which the language is set within the application independent of the native iOS preferred language. i.e. the application may have different language to what the iOS is currently set to on the iPhone.

Anybody with any ideas about how to go about implementing this scenario?

My idea:

  • I could create a custom static class similar to the NSLocalizeString and hard code strings within that and return appropriate language string w.r.t language set within the app, and if that is a possible solution then any suggestions about how to structure that class)
atastrophic
  • 3,153
  • 3
  • 31
  • 50
  • RE: 'I hate my client'. I hope you're using a pseudonym on this site different to the one your client knows about ! – Ben Clayton Jun 19 '12 at 10:59
  • A class and tutorial for this approach can be found here: http://aggressive-mediocrity.blogspot.in/2010/03/custom-localization-system-for-your.html – Fluffhead Mar 06 '14 at 00:12

4 Answers4

4

You could:

  1. store your translation string in a .plist file (string_key/translation) for each language;

  2. read the appropriate plist (depending on the language currently set) in a NSDictionary;

  3. access the dictionary for each string you want to display (just like you would do with NSLocalizeString).

sergio
  • 68,819
  • 11
  • 102
  • 123
2

I once had to create a flashchards app and the client needed to change the language at will. I don't have the source code for it at my box right now, but I remember using this tutorial. Also check the sample code they use.

Side note - dissing your clients publicly is really unprofessional.

Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
  • pretty neat idea.. But i wonder which one's better, yours or Sergio's ?? – atastrophic Jun 21 '12 at 09:21
  • I guess it depends on your needs - I can easily see that the a single Dictionary approach as Sergio suggests could quickly become unwieldy for larger apps/number of languages. But if you just need a few strings in two languages, then it's probably simpler. – Peter Sarnowski Jun 21 '12 at 10:37
1

Create plist files with needed language. I use {LANG}_{CLASS NAME} and {CLASS NAME} for default with all localized strings. After that I made a method that checks if proper file exists, take it or default if missing and returns an NSDictionary object depending on the device language, called on class init. This idea can also be used, if you implement localized nibs by adding nib name to the strings file.

+ (NSDictionary *) getLocalized: (NSString *) contollerName andLang:(NSString *) lang {
    NSString *fullPath = nil;
    // You can use device lang if needed
    NSString * curLang=[[NSLocale preferredLanguages] objectAtIndex:0];
    fullPath = [[NSBundle mainBundle] pathForResource: [NSString stringWithFormat:@"%@_%@",contollerName,lang]  ofType: @"plist"];
    if (!fullPath) {
        fullPath = [[NSBundle mainBundle] pathForResource:contollerName ofType:@"plist"];
    }
    return [NSDictionary dictionaryWithContentsOfFile:fullPath];
}

So, it can be called by something like that:

[tools getLocalized:[Controller.class description] andLang:@"XX"];
ETech
  • 1,613
  • 16
  • 17
0

You can use standard localization (.strings files and localized .xibs) and force your app to use a language other than the iOS language setting. For details on how to achieve this, see this post: Change language of the ios application

Note that if you (or rather your client!) want to switch language on the fly while using the app, it will be more complicated--you would need to implement some sort of refresh feature and make sure the xibs are reloaded.

Community
  • 1
  • 1
Clafou
  • 15,250
  • 7
  • 58
  • 89