How to force an app to change language in iOS/Objective-C?
I think that this will solve your problem.
I had the same problem 1 month ago.
You can follow this tutorial.
Example:
In your .strings
file you put translation: "hello" = "HOLA MUNDO";
#import "LocalizationSystem.h"
LocalizationSetLanguage(@"Spanish");
CCLabel* label = [CCLabel labelWithString:AMLocalizedString(@"hello",@"Hello World") fontName:@"Marker Felt" fontSize:32];
EDIT: This is additional information as user asks in comment:
If you want to translate whole view where your language option is changed, I propose you to make method that will be called everytime you change language:
-(void)translateOnChoose {
self.label.text = AMLocalizedString(@"hello",@"Hello World");
}
Change iOS app language without rebooting the device
You can't change the language of the device in your application but you can change it just for your application by changing the AppleLanguages
property in NSUserDefaults
. Please note however I believe this still requires restarting the app itself but doesn't require a device restart.
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"nl", @"en", nil] forKey:@"AppleLanguages"];
Here's another similar question that also maybe able to help you
Why language change required app to restart in Objective C
Muju I created sample project and I worked getting solution for your question.I got the solution perfectly.
In my below sample I want to change "Welcome to Thailand" to "ยินดีต้อนรับสู่ประเทศไทย".I use localization concept for this.
Before going to steps, I want you to see my storyboard designing
Please follow the below steps.
STEP 1:Click Project->info->Localization->Click +
Now it shows the drop down list of Language.From that we should select Thai
STEP 2:Once we choose or select the Language from drop down list,it shows the below window and we need to click Finish button
Now it looks like below
STEP 3:Create the String File for the localization and set the name.
above I set String file name as LocalizationThai
STEP 4:Click the LocalizationThai.strings also click the File Inspector.Click the Localization inside the File Inspector.Now it shows the below pop up box.
STEP 5:Click Localize.Once you Localize,it shows below like this
STEP 6:Click 3 Checkboxes
Now in bundle we have 3 files under LocalizationThai.strings
STEP 7:Write your required changing text in string files.
i.In LocalizationThai.strings(Thai) file I write below text
ii.In LocalizationThai.strings(English) file I write below text
iii.In LocalizationThai.strings(Base) file I write below text
STEP 8:Create the Header File for the multiple Languages.
STEP 9 : set the Header name(I set header name as LocalizationHeader) and define Languages in the Header file like below
LocalizationHeader.h
#ifndef LocalizationHeader_h
#define LocalizationHeader_h
#define ENGLISH 0
#define THAI 1
#endif /* LocalizationHeader_h */
STEP 10:Implement the below coding part
Localization.h
#import <Foundation/Foundation.h>
#import "LocalizationHeader.h"
@interface Localization : NSObject
+(Localization *)sharedInstance;
+(NSString*) strSelectLanguage:(int)curLang;
+(NSString*) languageSelectedStringForKey:(NSString*) key;
@end
Localization.m
#import "Localization.h"
int currentLanguage,selectedrow;
@implementation Localization
+(Localization *)sharedInstance
{
static Localization *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[Localization alloc] init];
});
return sharedInstance;
}
+(NSString*) strSelectLanguage:(int)curLang{
if(curLang==THAI){
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"th", nil]forKey:@"AppleLanguages"];
}
else{
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil]forKey:@"AppleLanguages"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
currentLanguage=curLang;
NSString *strLangSelect = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
return strLangSelect;
}
+(NSString*) languageSelectedStringForKey:(NSString*) key
{
NSString *path;
NSString *strSelectedLanguage = [[[NSUserDefaults standardUserDefaults]objectForKey:@"AppleLanguages"] objectAtIndex:0];
//When we check with iPhone,iPad device it shows "en-US".So we need to change it to "en"
if([strSelectedLanguage hasPrefix:@"en-"])
strSelectedLanguage = [strSelectedLanguage stringByReplacingOccurrencesOfString:@"en-US" withString:@"en"];
if([strSelectedLanguage isEqualToString:[NSString stringWithFormat: @"en"]]){
currentLanguage=ENGLISH;
selectedrow=ENGLISH;
path = [[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"];
}
else{
currentLanguage=THAI;
selectedrow=THAI;
path = [[NSBundle mainBundle] pathForResource:@"th" ofType:@"lproj"];
}
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
NSString* str=[languageBundle localizedStringForKey:key value:@"" table:@"LocalizationThai"];
return str;
}
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "Localization.h"
@interface ViewController : UIViewController{
Localization *localization;
}
@property (strong, nonatomic) IBOutlet UILabel *lblWelcome;
- (IBAction)actionChangeLanToThai:(id)sender;
- (IBAction)actionChangeLangToEng:(id)sender;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize lblWelcome;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
localization = [Localization sharedInstance];
lblWelcome.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionChangeLanToThai:(id)sender {
[Localization strSelectLanguage:THAI];
lblWelcome.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
- (IBAction)actionChangeLangToEng:(id)sender {
[Localization strSelectLanguage:ENGLISH];
lblWelcome.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
@end
When I run the app first
Then When I change the Language from English to Thai
Again when I change it to English
You have to follow the same steps for XIB
Below is for XIB
I create the ViewController with XIB.ViewController name is RootViewController
Now see the designing part
AppDelegate.h
#import <UIKit/UIKit.h>
#import "RootViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) RootViewController *viewController;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[navController setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
#import "Localization.h"
@interface RootViewController : UIViewController{
Localization *localization;
}
@property (strong, nonatomic) IBOutlet UILabel *lblWelcomeThaiLang;
- (IBAction)actionChangeLangToThai:(id)sender;
- (IBAction)actionChangeLangToEng:(id)sender;
@end
RootViewController.m
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
@synthesize lblWelcomeThaiLang;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)actionChangeLangToThai:(id)sender {
[Localization strSelectLanguage:THAI];
lblWelcomeThaiLang.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
- (IBAction)actionChangeLangToEng:(id)sender {
[Localization strSelectLanguage:ENGLISH];
lblWelcomeThaiLang.text = [Localization languageSelectedStringForKey:@"Welcome to Thailand"];
}
@end
Now see the result
Change Language in the app programmatically in iOS
Usually when you support official languages that Apple supports in iOS, there is no reason to provide language switching within the app, just properly set up translations in your project and interface language will automatically switch with the system. But since you want it from the app, there are few ways to go about this:
1) You could force a specific language for just your app with the following code:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", @"en", @"fr", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
I would suggest putting this code inside main.m file in "int main" function just before the "return UIApplicationMain". But this method requires you to kill the app or tell user to restart the app for it to take effect.
You can kill the app without having user to force quit the app using exit(0)
, but make sure user has chance to abort the action with UIAlertView
or similar, or Apple might reject your app.
2) Alternative is implementing your own localization logic, where you just take translations from your own language file. One way is this example which takes translations from official lproj files. This way you can change the language on the fly without a restart, but you have to manually load all label texts from the code. When you change the translation, you have to repopulate the text on screen.
Related Topics
Sharing an Image Between Two Viewcontrollers During a Transition Animation
iOS - Swift - Function That Returns Asynchronously Retrieved Value
Centering a View in Its Superview Using Visual Format Language
iOS Uialertview Button to Go to Setting App
How to Draw a Transparent Stroke (Or Anyway Clear Part of an Image) on the Iphone
How to Listen for All Notifications Sent to the iOS Nsnotificationcenter's Defaultcenter
How to Retrieve Facebook Response Using Facebook iOS Sdk
Change Push Notification Sound
Swift: Long Press Gesture Recognizer - Detect Taps and Long Press
Setting "Applelanguages" Doesn't Change App Language
How to Convert Bytes to a Float Value in Swift
Swiftui Generic Pull to Refresh View
Multiline Label in Uistackview
How to Get Hour and Minutes from Nsdate
Could Not Launch Process Launch Failed: Timed Out Waiting for App to Launch
Alamofire Download in Background Session
Blocks on Swift (Animatewithduration:Animations:Completion:)