Hub*_*ast 70 localization appsettings selection ios
我的问题:
我的iPhone应用程序如何告诉iOS,用户确实在应用程序首选项中选择了一种语言,这与一般设置中设置的语言不同?
同一问题的其他表述:
我如何告诉系统,不NSLocalizedString (@"text", @"comment");应该访问系统范围内选定的语言,而是应用程序选择的语言?
背景,例如:
请以这种情况为例:德国移民的儿子住在法国东北部,毗邻卢森堡和德国.他的母语是法语,因此他确实将iPhone的用户界面语言设置为法语(设置 - >常规 - >国际 - >语言 - >Français).但由于他的文化背景以及他所居住的地区是双语的,他也会说德语.但他不会说十个英语单词.在iPhone(以及iPad)上,他没有机会选择第二语言,因此手机只知道他会说法语.它不了解其他语言的用户技能.
现在来了我的应用程序:我用英语和德语开发了它(德语是我的母语,英语是IT的标准语言).我根据多语言iOS应用程序的所有规则和最佳实践进行了开发.我的应用程序的"第一"语言(默认语言)是英语.
这意味着:
如果有人在他的"设置"中选择了英语或德语,则应用程序用户界面将自动使用所选语言.用户甚至不会注意到还有其他语言可用.
但如果他在常规设置中选择了任何其他语言(如中文,波兰语或法语),他将获得应用程序默认语言,在我的例子中,它是英语.但对于我的法德朋友来说,这不是最好的选择.他想使用现有的德语版本,但似乎没有办法让用户选择这个版本.
添加法语翻译可以解决我们法国 - 德国朋友的问题,但对于说其他两种语言(如意大利语和德语)的人不会,我不能用这个星球上所有语言支持我的应用程序.将默认语言设置为德语也不是最佳选择,因为对于说法语(母语)和英语(作为第二语言)的人来说,这会引起同样的问题.
所以我认为我的应用程序必须能够手动选择与预选语言不同的语言.将语言选择添加到应用程序设置面板不是问题.但是,我如何告诉系统,不NSLocalizedString (@"text", @"comment");应该访问系统范围内选定的语言,而应该使用应用内选择的语言?
Hub*_*ast 83
与此同时,我确实找到了解决我自己问题的方法:
我创建了一个新类"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
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
对于您要支持的每种语言,您需要一个名为的文件Localizable.strings.这完全与Apples文档中描述的本地化一样.唯一的区别是:现在你甚至可以使用Apple不支持的hindi或esperanto等语言.
举个例子,这里是我的英文版和德文版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";
Run Code Online (Sandbox Code Playgroud)
德语
/* 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";
Run Code Online (Sandbox Code Playgroud)
要使用本地化,您必须在应用中使用一些设置例程,并在语言选择中调用宏:
LocalizationSetLanguage(selectedLanguage);
Run Code Online (Sandbox Code Playgroud)
之后,您必须确保以旧语言显示的所有内容现在都以新语言重新绘制(隐藏文本必须在再次可见时重新绘制).
要为每种情况提供本地化文本,您永远不必将修复文本写入对象标题.总是使用宏LocalizedString(keyword).
别:
cell.textLabel.text = @"nice title";
Run Code Online (Sandbox Code Playgroud)
做:
cell.textLabel.text = LocalizedString(@"nice title");
Run Code Online (Sandbox Code Playgroud)
并且在Localizable.strings的每个版本中都有一个"好标题"条目!
Nov*_*arg 39
只需在语言选择中将以下内容添加到屏幕:
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];
Run Code Online (Sandbox Code Playgroud)
然后让他们重新启动应用程序,应用程序将使用其他语言.
希望能帮助到你
Jee*_*hut 22
这里有一个现成的使用和一步一步的指导,就如何使用诺维格的做法,斯威夫特3:
如何做到这一点取决于你,取决于项目.但是用
Bundle.main.localizations.filter({ $0 != "Base" }) // => ["en", "de", "tr"]
Run Code Online (Sandbox Code Playgroud)
以编程方式获取所有受支持的语言环境语言代码的列表.你也可以使用
Locale.current.localizedString(forLanguageCode: "en") // replace "en" with your variable
Run Code Online (Sandbox Code Playgroud)
以应用程序当前语言显示语言名称.
作为一个完整的示例,您可以在单击如下按钮后显示弹出操作表:
@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)
}
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到步骤#1中的代码调用了一个名为的方法changeToLanguage(langCode:).这是你应该做的,也是当用户选择一个新的语言更改为,不管你如何设计你的选择器.这是它的实现,只需将其复制到您的项目中:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
这将询问并告知用户他是否想要进行更改以及如何进行更改.此外,它还使用以下方式设置应用程序语言:
UserDefaults.standard.set([langCode], forKey: "AppleLanguages")
UserDefaults.standard.synchronize() // required on real device
Run Code Online (Sandbox Code Playgroud)
您可能希望使用NSLocalizedString宏(或任何其他增强方法)本地化字符串,如"立即关闭" .
我在针对iOS 10的应用程序中使用了这个确切的实现,我可以确认它在模拟器和设备上都适用于我.该应用程序实际上是开源的,因此您可以在此处找到分布在不同类中的上述代码.