By Changing Language settings in Settings >General >International
>Change Language. We can achieve the localization to the Phone and Application.
If we want to handle the localization only for the
application but not to the Phone.
Then we can customize application as follows
We know how to use the localization by using
NSLocalizedString,
which returns a localized version of a string. by using
following command
NSString *NSLocalizedString(NSString *key, NSString
*comment)
We have localization for Image as follows
NSString* imageName = [[NSBundle mainBundle]
pathForResource:@"sample" ofType:@"png"];
self.image = [UIImage imageWithContentsOfFile:imageName];
But if you want to localize Image using your custom settings
then we can try the following snippet code.
For Eg: If you’re App supports English and German.
But, If your Phone default language is using French
and
Your app should use as per your language settings.
How to achieve following use case?
Provide Language Settings in your app using settings.bundle
By Using the above changes in your settings.bundle file,
we can see as follows language settings in your app.
Now, How to get the selected language?
There is code snippet. one way, where we can achieve localization
/*
* languageSelectedStringForKey used to get the localized value using key.
*/
NSUserDefaults* defaults;
- (NSString *)getLocalizationPath
{
NSString *path;
defaults = [NSUserDefaults standardUserDefaults];
[defaults synchronize];
NSString *defPhoneLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
NSString *langCode = [defaults stringForKey:@"testLanguage"];
if([langCode isEqualToString:@"0"])
{
if([defPhoneLanguage isEqualToString:@"de"] || [defPhoneLanguage isEqualToString:@"en"])
{
path = [[NSBundle mainBundle] pathForResource:defPhoneLanguage ofType:@"lproj"];
}
else
{
path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
}
}
else if([langCode isEqualToString:@"German"] || [langCode isEqualToString:@"de"])
{
path = [[NSBundle mainBundle] pathForResource:@"de" ofType:@"lproj"];
}
else
{
path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
}
return path;
}
-(NSString*) getLocaleStringForKey:(NSString*) key
{
NSString *path = [self getLocalizationPath];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:nil];
return str;
}
-(NSString*) getLocaleImageForKey:(NSString*) key
{
NSString *path = [self getLocalizationPath];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString *str = [[languageBundle resourcePath] stringByAppendingPathComponent:key];
return str;
}
Custom Locale Image:
[UIImage imageWithContentsOfFile:[self getLocaleImageForKey:@"sample.png"]]
Have a nice day... Enjoy :)