Reputation: 3143
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:
Upvotes: 3
Views: 1622
Reputation: 1607
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"];
Upvotes: 1
Reputation: 11970
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.
Upvotes: 2
Reputation: 15400
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.
Upvotes: 0
Reputation: 69027
You could:
store your translation string in a .plist file (string_key/translation) for each language;
read the appropriate plist (depending on the language currently set) in a NSDictionary;
access the dictionary for each string you want to display (just like you would do with NSLocalizeString).
Upvotes: 4