I want to have an option in my application to change my application language (just application language not the whole system), would you please give me some hints?
Thanks in advance,
I know about localisation!
I want to have an option in my application to change my application language (just application language not the whole system), would you please give me some hints?
Thanks in advance,
I know about localisation!
You can do this by using custom build flags that you set up from Xcode. This way you can run an application under a localisation rather than changing all the settings for the device.
I wrote a blog post about it at http://abizern.org/2012/03/18/simple-localisation-testing/
And there is an example project on Github that you can use to see it in action: https://github.com/Abizern/SimpleLocalisationTesting
I've made custom localization made by .plist filled with array of dictionaries. Like this:
kYes
en = @"Yes"
ru = @"Да"
kNo
en = @"No"
ru = @"Нет"
In my project I made a singleton class for reading this strings. In options user selects needed language (at first start app select language from current system state or sets default one). Selected language must be saved in file or in settings to use on the next start.
In singleton I have +getStringWithKey:
. So I pass there @"kYes" and get string for current localization for app only. Also, you will need to add standard localization files to project anyway, even empty, because if you don't, all languages you support will not be shown in iTunes.
I was actually thinking about another idea on how to change the language in your apps ui, though it forces you to 1. set all strings manually a 2. provide a method (notification) that updates all ui elements when the language setting has changed. The idea is based on the same method iOS uses to find the localized version of a string. So as a prerequisite you have to provide a bundle (e.g. AppNameStrings.bundle) with a folder structure language_id.lproj where language_id is the designator for the languages you want to provide. You can create such a bundle by creating a settings bundle, open it in Finder and the delete the root.plist file. Next you need a function you can use to read from your bundle. Here are 2 methods I use to read localized strings. Put the following methods in your GlobalDefinitions.h file
#ifdef __OBJC__
NSBundle *yourNameBundle(void);
NSString *YourNameLocalized(NSString *stringToken, NSString *language);
#endif
and this code in your GlobalDefinitions.m file
NSBundle *yourNameBundle(void) {
static NSBundle* bundle = nil;
if (!bundle) {
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YouNameBundle.bundle"];
bundle = [NSBundle bundleWithPath:path];
}
return bundle;
}
NSString *YourNameLocalized(NSString *stringToken, NSString *language) {
if (yourNameBundle()) {
return [yourBundle localizedStringForKey:stringToken value:@"" table:@"Root" language:language];
} else {
return stringToken;
}
}
You have to create a category on NSBundle for
[yourBundle localizedStringForKey:stringToken value:@"" table:@"Root" language:language]
as the standard method misses the language parameter. In your method you open the corresponding language.lproj/Root.strings file and scan it for the stringToken. If you can't find it return the token itself otherwise the text string. Whenever you need a localized version of some text you write e.g. label.text = YourNameLocalized(@"Token", [self provideLanguageIdentifier); and you are done. I haven't done it so far but it should work.