问题
i am building up a project in ios platform and want to use two language in this application and also want . the user can change the language on Runtime suppose we can take two language button button 1 for english and button 2 for german and the user can change the application language at any time by using these button . any help or tutorials
Thanks in advance
回答1:
Assuming that you have created localizations for English and German, your app will use the language selected in Settings (this is the preferred option).
If you want to set the language directly from your application:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", nil] forKey:@"AppleLanguages"];
will set the language for your application only to English (for German, I assume the key is "de"). You should also:
[[NSUserDefaults standardUserDefaults] synchronize];
This will not take effect until your application restarts.
回答2:
Have a class "LanguageUtils" with that method :
- (NSString *) localizedString:(NSString *)key
{
if (self.currentLanguage == nil) {
self.currentLanguage = @"en";
}
NSString* path = [[NSBundle mainBundle] pathForResource:[self.currentLanguage lowercaseString] ofType:@"lproj"];
NSBundle* languageBundle = [NSBundle bundleWithPath:path];
return [languageBundle localizedStringForKey:key value:@"" table:nil];
}
And the property NSString currentLanguage.
Then in your .pch do :
#undef NSLocalizedString
#define NSLocalizedString(key, _comment) [[Language sharedInstance] localizedString:key]
Works like a charm and you can redefine the current language at runtime.
Just keep in mind that your view should be refreshed by yourself. It won't automatically do it for you.
回答3:
I came up with a solution that allows you to use NSLocalizedString
. I create a category of NSBundle
call NSBundle+RunTimeLanguage
. The interface is like this.
// NSBundle+RunTimeLanguage.h
#import <Foundation/Foundation.h>
@interface NSBundle (RunTimeLanguage)
#define NSLocalizedString(key, comment) [[NSBundle mainBundle] runTimeLocalizedStringForKey:(key) value:@"" table:nil]
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName;
@end
The implementation is like this.
// NSBundle+RunTimeLanguage.m
#import "NSBundle+RunTimeLanguage.h"
#import "AppDelegate.h"
@implementation NSBundle (RunTimeLanguage)
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSString *path= [[NSBundle mainBundle] pathForResource:[appDelegate languageCode] ofType:@"lproj"];
NSBundle *languageBundle = [NSBundle bundleWithPath:path];
NSString *localizedString=[languageBundle localizedStringForKey:key value:key table:nil];
return localizedString;
}
@end
Than just add import NSBundle+RunTimeLanguage.h
into the files that use NSLocalizedString
.
As you can see I store my languageCode in a property of AppDelegate
. This could be stored anywhere you'd like.
This only thing I don't like about it is a Warning that NSLocalizedString
marco redefined. Perhaps someone could help me fix this part.
回答4:
This is very good and the language is changed within the app and also when device language changes. I used this in many apps.
For localization we generally use
NSLocalizedString(@"hello",@"Hello World");
in the custom implementation they have something similar like this
AMLocalizedString(@"hello",@"Hello World");
回答5:
Here is demo tutorial
https://github.com/object2dot0/Advance-Localization-in-ios-apps
Just remove
[self dealloc];
from
-(IBAction) languageSelection:(id)sender
so it wont be crash!!! enjoy!
回答6:
You have to use localization for this . Just store the key and value in side localizable.string and load the appropriate .string file as per your requirement . You can refer following tutorial for this : http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
回答7:
SWIFT 2.0 version We can do this on run time without restarting the app in Swift 2.0 as follows
Make a function within a class, lets say LanguageManager is the name of the class
class LanguageManager
{
static let sharedInstance = LanguageManager()
//Make a function for Localization. Language Code is the one which we will be deciding on button click.
func LocalizedLanguage(key:String,languageCode:String)->String{
//Make sure you have Localizable bundles for specific languages.
var path = NSBundle.mainBundle().pathForResource(languageCode, ofType: "lproj")
//Check if the path is nil, make it as "" (empty path)
path = (path != nil ? path:"")
let languageBundle:NSBundle!
if(NSFileManager.defaultManager().fileExistsAtPath(path!)){
languageBundle = NSBundle(path: path!)
return languageBundle!.localizedStringForKey(key, value: "", table: nil)
}else{
// If path not found, make English as default
path = NSBundle.mainBundle().pathForResource("en", ofType: "lproj")
languageBundle = NSBundle(path: path!)
return languageBundle!.localizedStringForKey(key, value: "", table: nil)
}
}
}
How to Use this.
Now Assume you have two Languages for the application (English and Spanish)
English - en
Spanish - es
Suppose you have to place something for a label
//LOGIN_LABEL_KEY is the one that you will define in Localizable.strings for Login label text
logInLabel.text = LanguageManager.sharedInstance.LocalizedLanguage("LOGIN_LABEL_KEY", languageCode: "es")
//This will use the Spanish version of the text.
//"es" is the language code which I have hardcoded here. We can use a variable or some stored value in UserDefaults which will change on Button Click and will be passed in loginLabel
For more info on language codes see this link below
https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html
or
http://www.ibabbleon.com/iOS-Language-Codes-ISO-639.html
来源:https://stackoverflow.com/questions/16032207/how-to-change-application-language-on-run-time-using-two-button-in-iphone-sdk