In my MainViewController I am using this code:
static NSBundle *bundle = nil;
+(void)setLanguage:(NSString *)l {
NSLog(#"preferredLang: %#", l);
NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:#"lproj" ];
bundle = [[NSBundle bundleWithPath:path] retain];}
+(void)initialize {
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:#"AppleLanguages"];
NSString *current = [[languages objectAtIndex:0] retain];
[self setLanguage:current];
}
+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
return [bundle localizedStringForKey:key value:alternate table:nil];}
And in application main:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"tr", #"en", nil] forKey:#"AppleLanguages"];
Its working fine and showing the turkish language in my application other than the iPhone native language. But, I want to allow the user to change the language through buttons, like if user clicks on english language button, a dialogue should open that application will be restarted and your lang will be changed.
How to do that?
I am using iOS 5....
Your app should not provide a custom language selector.
Users probably have prior knowledge on how to change the preferred language by navigating to General > International > Language in the Settings application.
Further details in Apple's Internationalization Programming Guide
Related
I have an app that requires 2 languages, English and French.
I have already set up the Localizable.strings files in their respective "en.lproj" and "fr.lproj" folders ... and when I change the iPad's language (in the native settings app), then launch my app, it does in fact load the correct text (i.e. either English copy or French copy).
I need to have a UISegmentedControl toggle between the 2 languages without having to restart the app.
How do I get the app to change the (current) language so that when I call a method that (re)sets all the UILabels' text and UIImageViews' images they read from the opposite .lproj folder's Localizable.strings file?!?
I know how to use UISegmentedControl, and that is not my question. I'm looking more for a line of code that sets the application's bundle language or locale or something (as I'm quite new to internationalization.localization).
-
Example of how I set the image for a UIImageView:
myUIImageView1.image = [UIImage imageNamed:NSLocalizedString(#"myUIImageView1", #"comment for the translator")];
Example of how I set the text of a UILabel:
myLabel1.text = NSLocalizedString(#"myLabel1", #"comment for the translator");
FOUND THE SOLUTION!!!
The following test app had a function that read the desired string from the correct 'Localizable.strings' file (based on the language selected):
https://github.com/object2dot0/Advance-Localization-in-ios-apps
-
I took this code, and the code required to set the app's primary language (found in the above answer posted by Brayden: How to force NSLocalizedString to use a specific language), and put them together.
Here's what my code looks like now (note - my UISegmentedControl calls a function in it's view's viewController [when the UISegmentedControl's 'Value Changed' method is triggered] that then calls the toggleLanguage function in the parent viewController):
-(void)toggleLanguage:(NSString *)primaryLanguage secondaryLanguage:(NSString *)secondaryLanguage
{
//set app's primary language
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSArray arrayWithObjects:primaryLanguage,secondaryLanguage,nil] forKey:#"AppleLanguages"];
[defaults synchronize];
//update UILabel and UIImageViews
[self setLanguageSpecificItems];
}
-(NSString *)languageSelectedStringForKey:(NSString *)key
{
//read primary language
NSArray *appleLanguagesArray = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [appleLanguagesArray objectAtIndex:0];
//get the path to the desired lproj file
NSString *path;
if(currentLanguage==#"en")
{
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
}
else
if(currentLanguage==#"fr")
{
path = [[NSBundle mainBundle] pathForResource:#"fr" ofType:#"lproj"];
}
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
//find and return the desired string
NSString* str=[languageBundle localizedStringForKey:key value:#"" table:nil];
return str;
}
-(void)setLanguageSpecificItems
{
myUIImageView1.image = [UIImage imageNamed:[self languageSelectedStringForKey:#"myUIImageView1"]];
myLabel1.text = [self languageSelectedStringForKey:#"myLabel1"];
}
-
Thanks for the help everyone!!!
-Chris Allinson
How do I change my applications default language within my application? I am trying to change my applications language to Arabic, and I'm not sure how to accomplish this.
There is a way:
First make a different folder named as ar.lproj and put localizable.String
May following sample code help you. You can call this function in viewWillAppear with the key for which you need to get value.
-(NSString*) languageSelectedStringForKey:(NSString*) key
{
NSString *path;
NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
if([[userDefault valueForKey:#"language_Selected"] intValue] == 0)
path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
else if([[userDefault valueForKey:#"language_Selected"] intValue] == 1)
path = [[NSBundle mainBundle] pathForResource:#"ar" ofType:#"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[[languageBundle localizedStringForKey:key value:#"" table:nil] retain];
return str;
}
Hope you will understand the concept.
Is it possible to change the Three20 language/localization at runtime without restarting the app?
Currently, I managed to change the language via altering the value of AppleLanguages in the main.m
There's a "hack" for it. You can load your own NSBundle with the localized text and use that NSBundle instead. Note that if the localized language file is missing, the app won't run, so make sure you set a correct language.
Above your AppDelegate implementation, add a custom NSBundle declaration:
static NSBundle *bundle = nil;
And then load the language you desire into that bundle:
[[NSUserDefaults standardUserDefaults] setObject: [NSArray arrayWithObjects:#"he", nil] forKey:#"AppleLanguages"];
NSLocale* locale = TTCurrentLocale();
NSString *path = [[NSBundle mainBundle] pathForResource:[locale localeIdentifier] ofType:#"lproj" ];
bundle = [[NSBundle bundleWithPath:path] retain];
You will add a custom function in your AppDelegate to get the localized text too (instead of NSLocalizedString)
///////////////////////////////////////////////////////////////////////////////////////////////////
+ (NSString*)get:(NSString*)key {
return [bundle localizedStringForKey:key value:nil table:nil];
}
To make things easier, you can add a static function in the pch file:
#import "AppDelegate.h"
#define MyLocalizedString(key, alt) [AppDelegate get:key]
my application run vary well but now when UISwitch button is on at that time i have to convet whole application in spanish when off then convert in to english how it possible plz give any replay for that.
i18n
Create the following structure:
resources/i18n/en.lproj/Localizable.strings
resources/i18n/es.lproj/Localizable.strings
Create an additional directory with the corresponding two letter code for each additional language supported.
It's recommended to encode Localized.strings in UTF-16. You can convert between encodings in the inspector pane of XCode.
If the files are recognized as i18n resources, they will be presented like this:
A sample file has the following content:
"hello"="hola";
Then use the following in your program:
NSString *string = NSLocalizedString(#"hello", nil);
Choose language dynamically
To change the language for your application dynamically use this code:
#implementation Language
static NSBundle *bundle = nil;
+(void)initialize {
NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
NSArray* languages = [defs objectForKey:#"AppleLanguages"];
NSString *current = [[languages objectAtIndex:0] retain];
[self setLanguage:current];
}
/*
example calls:
[Language setLanguage:#"es"];
[Language setLanguage:#"en"];
*/
+(void)setLanguage:(NSString *)code {
NSLog(#"preferredLang: %#", code);
NSString *path = [[ NSBundle mainBundle ] pathForResource:code ofType:#"lproj" ];
// Use bundle = [NSBundle mainBundle] if you
// dont have all localization files in your project.
bundle = [[NSBundle bundleWithPath:path] retain];
}
+(NSString *)get:(NSString *)key alter:(NSString *)alternate {
return [bundle localizedStringForKey:key value:alternate table:nil];
}
#end
Then translate your strings like this:
NSString *hello [Language get:#"hello", nil, nil];
The code above was originally posted by Mauro Delrio as an answer to How to force NSLocalizedString to use a specific language.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I display the application version revision in my application's settings bundle?
I have an iPhone application that displays the current version as a Settings constant (just like Skype does).
When I released the first version of the application, I use this code to set the app Settings:
- (void)registerDefaultsFromSettingsBundle {
NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:#"Settings" ofType:#"bundle"];
if(!settingsBundle) {
NSLog(#"Could not find Settings.bundle");
return;
}
NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:#"Root.plist"]];
NSArray *preferences = [settings objectForKey:#"PreferenceSpecifiers"];
NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]];
for(NSDictionary *prefSpecification in preferences) {
NSString *key = [prefSpecification objectForKey:#"Key"];
if(key) {
[defaultsToRegister setObject:[prefSpecification objectForKey:#"DefaultValue"] forKey:key];
}
}
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister];
[defaultsToRegister release];
}
And this worked fine and dandy.
The problem I face now is that if you update the application, these defaults (Settings) are nor re-written, so the application version is not updated.
How can I force that an specific Settings is set on every install?
Thanks
Gonso
You can use the following 'Run Script' Build Phase:
CFBundleShortVersionString=`/usr/libexec/PlistBuddy -c "Print :CFBundleShortVersionString" ${SRCROOT}/YourApp/YourApp-Info.plist`
CFBundleVersion=`/usr/libexec/PlistBuddy -c "Print :CFBundleVersion" ${SRCROOT}/YourApp/YourApp-Info.plist`
/usr/libexec/PlistBuddy -c "Set :PreferenceSpecifiers:3:DefaultValue '${CFBundleShortVersionString} (${CFBundleVersion})'" ${SRCROOT}/YourApp/Settings.bundle/Root.plist
All you need to do, is to replace YourApp with your app's name and set the appropriate keypath.
In my case, I have 4 items in the PreferenceSpecifiers array, and I need to set the value for the last item from the array and that's why I used ':PreferenceSpecifiers:3:DefaultValue'
I have this code in my application delegate's -applicationDidFinishLaunching:
#if DDEBUG // debugging/testing
NSString *versionString = [NSString stringWithFormat:#"v%#",[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"]];
#else
NSString *versionString = [NSString stringWithFormat:#"Version %#",[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleShortVersionString"]];
#endif // DDEBUG
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:versionString forKey:#"version"];
printf("Version: = %s\n", [versionString cStringUsingEncoding:NSMacOSRomanStringEncoding]);
[defaults synchronize]; // force immediate saving of defaults.
But app has to be run before Settings 'version' updates to reflect the change.
Why wouldn't you set the version number from the mainBundle?
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleVersion"]];
This way you don't have to update the settings file for every version. If you want to compare existing versus new install version. You could write out the version number to a file on launch and compare the directory version with the launch version.