iOS-App(iPhone和iPad)中的手动语言select

我的问题:

我的iPhone应用程序如何告诉iOS,用户确实在应用程序首选项中select了一种与常规设置中设置的语言不同的语言?

同样问题的其他表述:

我怎么能告诉系统, NSLocalizedString (@"text", @"comment"); 不应该访问全系统select的语言,而是应用程序内select的语言?

背景,例如:

请以这种情况为例:德国移民的一个儿子生活在法国东北部,靠近卢森堡和德国。 他的母语是法语,因此他将iPhone的用户界面语言设置为法语(设置 – >常规 – >国际 – >语言 – >Français)。 但是由于他的文化背景,而且他所居住的地区是双语的,所以他也讲德语。 但是他不会说十个英语单词。 在iPhone(也是iPad)上,他没有机会select第二种语言,所以电话只知道他是法国人。 它不了解其他语言的用户技能。

现在来我的应用程序:我用英语和德语开发它(德语是我的母语,英语是IT的标准语言)。 我根据所有关于多语种iOS应用程序的规则和最佳实践进行了开发。 我的应用程序的“第一”语言(默认语言)是英语。

意即:

如果某人在其“设置”中select了英语或德语,那么应用程序用户界面将自动使用所选语言。 用户甚至不会注意到有其他语言可用。

但是,如果他在一般设置中select了其他语言(如中文,波兰文或法文),他将获得应用程序默认语言,在我的情况下,它是英语。 但对于我的法国朋友来说,这不是最好的select。 他想使用现有的德语版本,但似乎没有办法让用户select这个版本。

添加法语翻译可以为我们的法语和德语朋友解决问题,但不能为其他两种语言的语言(如意大利语和德语)解决问题,而且我无法支持在这个星球上使用所有语言的应用程序。 将默认语言设置为德语也不是最佳的,因为这对于讲法语(母语)和英语(作为第二语言)的人会产生同样的问题。

所以我认为我的应用程序必须有可能手动select一种与预选语言不同的语言。 添加语言select到应用程序设置面板不是问题。 但是我怎么能告诉系统, NSLocalizedString (@"text", @"comment"); 不应该访问全系统select的语言,而是应用程序内select的语言?

在此期间,我确实为自己find了解决问题的办法:

我创build了一个新的类“LocalizeHelper”:


头文件LocalizeHelper.h

 //LocalizeHelper.h #import <Foundation/Foundation.h> // some macros (optional, but makes life easy) // Use "LocalizedString(key)" the same way you would use "NSLocalizedString(key,comment)" #define LocalizedString(key) [[LocalizeHelper sharedLocalSystem] localizedStringForKey:(key)] // "language" can be (for american english): "en", "en-US", "english". Analogous for other languages. #define LocalizationSetLanguage(language) [[LocalizeHelper sharedLocalSystem] setLanguage:(language)] @interface LocalizeHelper : NSObject // a singleton: + (LocalizeHelper*) sharedLocalSystem; // this gets the string localized: - (NSString*) localizedStringForKey:(NSString*) key; //set a new language: - (void) setLanguage:(NSString*) lang; @end 

iMplementation LocalizeHelper.m

 // LocalizeHelper.m #import "LocalizeHelper.h" // Singleton static LocalizeHelper* SingleLocalSystem = nil; // my Bundle (not the main bundle!) static NSBundle* myBundle = nil; @implementation LocalizeHelper //------------------------------------------------------------- // allways return the same singleton //------------------------------------------------------------- + (LocalizeHelper*) sharedLocalSystem { // lazy instantiation if (SingleLocalSystem == nil) { SingleLocalSystem = [[LocalizeHelper alloc] init]; } return SingleLocalSystem; } //------------------------------------------------------------- // initiating //------------------------------------------------------------- - (id) init { self = [super init]; if (self) { // use systems main bundle as default bundle myBundle = [NSBundle mainBundle]; } return self; } //------------------------------------------------------------- // translate a string //------------------------------------------------------------- // you can use this macro: // LocalizedString(@"Text"); - (NSString*) localizedStringForKey:(NSString*) key { // this is almost exactly what is done when calling the macro NSLocalizedString(@"Text",@"comment") // the difference is: here we do not use the systems main bundle, but a bundle // we selected manually before (see "setLanguage") return [myBundle localizedStringForKey:key value:@"" table:nil]; } //------------------------------------------------------------- // set a new language //------------------------------------------------------------- // you can use this macro: // LocalizationSetLanguage(@"German") or LocalizationSetLanguage(@"de"); - (void) setLanguage:(NSString*) lang { // path to this languages bundle NSString *path = [[NSBundle mainBundle] pathForResource:lang ofType:@"lproj" ]; if (path == nil) { // there is no bundle for that language // use main bundle instead myBundle = [NSBundle mainBundle]; } else { // use this bundle as my bundle from now on: myBundle = [NSBundle bundleWithPath:path]; // to be absolutely shure (this is probably unnecessary): if (myBundle == nil) { myBundle = [NSBundle mainBundle]; } } } @end 

对于您想要支持的每种语言,您需要一个名为Localizable.strings的文件。 这与Apples文档中描述的本地化完全相同。 唯一的区别是:现在你甚至可以使用诸如印地文或者世界语之类的语言,这些语言不被苹果公司支持。

举个例子,下面是我的英文和德文版Localizable.strings的第一行:

英语

 /* English - English */ /* for debugging */ "languageOfBundle" = "English - English"; /* Header-Title of the Table displaying all lists and projects */ "summary" = "Summary"; /* Section-Titles in table "summary" */ "help" = "Help"; "lists" = "Lists"; "projects" = "Projects"; "listTemplates" = "List Templates"; "projectTemplates" = "Project Templates"; 

德语

 /* German - Deutsch */ /* for debugging */ "languageOfBundle" = "German - Deutsch"; /* Header-Title of the Table displaying all lists and projects */ "summary" = "Überblick"; /* Section-Titles in table "summary" */ "help" = "Hilfe"; "lists" = "Listen"; "projects" = "Projekte"; "listTemplates" = "Vorlagen für Listen"; "projectTemplates" = "Vorlagen für Projekte"; 

要使用本地化,您必须在应用程序中使用一些设置例程,并在调用macros的语言select中使用:

 LocalizationSetLanguage(selectedLanguage); 

之后,你必须保证,以旧语言显示的所有内容现在都以新语言重新绘制(隐藏文本只要再次可见,就必须重新绘制)。

要为每种情况提供本地化的文本,您永远不要将修订文本写入对象标题。 总是使用macrosLocalizedString(keyword)

别:

 cell.textLabel.text = @"nice title"; 

做:

 cell.textLabel.text = LocalizedString(@"nice title"); 

并在Localizable.strings的每个版本中都有一个“不错的标题”条目!

只需将以下内容添加到屏幕上,并select语言:

  NSString *tempValue = //user chosen language. Can be picker view/button/segmented control/whatever. Just get the text out of it NSString *currentLanguage = @""; if ([tempValue rangeOfString:NSLocalizedString(@"English", nil)].location != NSNotFound) { currentLanguage = @"en"; } else if ([tempValue rangeOfString:NSLocalizedString(@"German", nil)].location != NSNotFound) { currentLanguage = @"de"; } else if ([tempValue rangeOfString:NSLocalizedString(@"Russian", nil)].location != NSNotFound) { currentLanguage = @"ru"; } [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:currentLanguage, nil] forKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults]synchronize]; 

然后让他们重新启动应用程序,该应用程序将使用其他语言。

希望能帮助到你

下面是一个关于如何在Swift 3中使用Novarg的方法的准备使用分步指南


步骤#1:实现语言select器

如何做到这一点取决于你,取决于项目。 但是使用

 Bundle.main.localizations.filter({ $0 != "Base" }) // => ["en", "de", "tr"] 

以编程方式获取所有支持的语言环境语言代码的列表 。 也可以使用

 Locale.current.localizedString(forLanguageCode: "en") // replace "en" with your variable 

以应用程序当前语言显示语言名称

作为一个完整的例子,你可以在点击这样一个button后展示一个popover动作表

 @IBOutlet var changeLanguageButton: UIButton! @IBAction func didPressChangeLanguageButton() { let message = "Change language of this app including its content." let sheetCtrl = UIAlertController(title: "Choose language", message: message, preferredStyle: .actionSheet) for languageCode in Bundle.main.localizations.filter({ $0 != "Base" }) { let langName = Locale.current.localizedString(forLanguageCode: languageCode) let action = UIAlertAction(title: langName, style: .default) { _ in self.changeToLanguage(languageCode) // see step #2 } sheetCtrl.addAction(action) } let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) sheetCtrl.addAction(cancelAction) sheetCtrl.popoverPresentationController?.sourceView = self.view sheetCtrl.popoverPresentationController?.sourceRect = self.changeLanguageButton.frame present(sheetCtrl, animated: true, completion: nil) } 

步骤#2:解释用户该做什么+重新启动,更改语言

您可能已经注意到,步骤#1中的代码调用名为changeToLanguage(langCode:) 。 无论您如何deviseselect器当用户select一种新语言进行更改时,也应该这样做 。 这是它的实现 ,只需将其复制到您的项目:

 private func changeToLanguage(_ langCode: String) { if Bundle.main.preferredLocalizations.first != langCode { let message = "In order to change the language, the App must be closed and reopened by you." let confirmAlertCtrl = UIAlertController(title: "App restart required", message: message, preferredStyle: .alert) let confirmAction = UIAlertAction(title: "Close now", style: .destructive) { _ in UserDefaults.standard.set([langCode], forKey: "AppleLanguages") UserDefaults.standard.synchronize() exit(EXIT_SUCCESS) } confirmAlertCtrl.addAction(confirmAction) let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) confirmAlertCtrl.addAction(cancelAction) present(confirmAlertCtrl, animated: true, completion: nil) } } 

这将会询问并通知用户他是否想要改变以及如何去做。 另外它使用下面的开始设置应用程序的语言:

 UserDefaults.standard.set([langCode], forKey: "AppleLanguages") UserDefaults.standard.synchronize() // required on real device 

步骤3(可选):本地化string

您可能想通过使用NSLocalizedStringmacros(或任何其他增强的方法)来本地化string,如“立即closures”。


我在iOS 10的应用程序中使用了这个确切的实现,我可以在模拟器和设备上确认它适用于我。

它非常简单,易于手动更改语言。 首先,您需要本地化您的应用程序,然后您可以使用下面的代码在应用程序中手动更改语言。

 UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"App restart required", @"App restart required") message:NSLocalizedString(@"In order to change the language, the App must be closed and reopened by you.", @"In order to change the language, the App must be closed and reopened by you.") preferredStyle:UIAlertControllerStyleActionSheet]; [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { [self dismissViewControllerAnimated:YES completion:^{ }]; }]]; [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Restart", @"Restart") style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) { [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"ar", nil] forKey:@"AppleLanguages"]; [[NSUserDefaults standardUserDefaults]synchronize]; exit(EXIT_SUCCESS); }]]; [self presentViewController:actionSheet animated:YES completion:nil]; } 

本地化 – Swift – Swift友好的本地化和国际化与应用程序语言切换

我的回答可能是一个偏好的问题,因为我会鄙视在应用程序手动语言select:你将不得不覆盖所有系统提供的button,并确保不要使用它们。 这也增加了另一层复杂性,可能导致用户混淆。

但是,由于这必须是一个答案,所以我认为你的用例是在没有黑客select的情况下解决的。

在iOS偏好设置中,您可以设置其他语言:

iOS语言选择首选项

你的移民儿子的例子可以设置法语为主要语言,德语为另一种语言。

那么,当你的应用程序被本地化为英语和德语时,那个年轻人的iPhone就会select德国的资源。

这能解决问题吗?