简体   繁体   English

Xcode本地化

[英]Xcode localization

I have a question on how to set the default language in an Xcode project. 我对如何在Xcode项目中设置默认语言有疑问。 My Mac OS X App supports German and English. 我的Mac OS X应用程序支持德语和英语。

Everytime English is not selected in Systemsettings, default language is German. 每次未在“系统设置”中选择英语时,默认语言为德语。 I want to switch default language to English, so that non-german users get an English UI. 我想将默认语言切换为英语,以便非德语用户获得英语界面。 Changing "Localization native development region" in Plist file to English didn't solve the problem. 将Plist文件中的“本地化本机开发区域”更改为英语并不能解决问题。 I tried to do it within the code but this is not what Apple recommends in their HIGs. 我尝试在代码中执行此操作,但这不是Apple在其HIG中建议的。

Sampleproject is hosted on Github. Sampleproject托管在Github上。

https://github.com/christian123456/xcodelocalization https://github.com/christian123456/xcodelocalization

Xcode Version is 5.1.1 Xcode版本是5.1.1

I recently added the screenshots to the repository. 我最近将屏幕截图添加到了存储库中。 As you can see in the "german incorrect.png" screenshot, French and Portuguese languages are chosen but Mac OS picked German as language. 如您在“ germancorrect.png”屏幕截图中看到的那样,选择了法语和葡萄牙语,但是Mac OS选择了德语作为语言。 I want the language to be English. 我希望该语言为英语。

Testing and analyzing 测试与分析

I've added -NSShowNonLocalizedStrings YES into scheme-editor > Run .app > Arguments > Arugments Passed On Launch . 我已经将-NSShowNonLocalizedStrings YES添加到scheme-editor> Run .app> Arguments> Arugments Pass on Launch中 This turns any string it cannot find a localized variant for into uppercase. 这会将找不到它的本地化变体的所有字符串变成大写。 The result is, I see all uppercase. 结果是,我看到所有大写字母。

To switch language for developing use -AppleLanguages (it) . 切换语言以进行开发使用-AppleLanguages(it) As result, the en localization appears. 结果,出现en本地化。

To switch the language of an app, you can also use the following app: https://itunes.apple.com/de/app/app-language-chooser/id451732904?l=en&mt=12 要切换应用程序的语言,您还可以使用以下应用程序: https : //itunes.apple.com/de/app/app-language-chooser/id451732904?l=zh_CN&mt=12

For more information read here . 欲了解更多信息, 请点击这里

Solution

After a little searching and testing, I found this solution: 经过一番搜索和测试,我找到了这个解决方案:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", @"de", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

This would set the language english as default. 这会将英语设置为默认语言。

Another option is to create your own localization class. 另一种选择是创建自己的本地化类。 Call your class, and it can force localization into any language you want: 调用您的课程,它可以强制将本地化为您想要的任何语言:

- (NSString*) MyLocalizedString:(NSString*) label;
{
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSString* preferredLanguage = [defs objectForKey:@"LocalizedLanguage"];
    NSString *path = [[NSBundle mainBundle] pathForResource:preferredLanguage ofType:@"lproj"];

    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    NSString *result = [NSString stringWithFormat:@"%@",[languageBundle localizedStringForKey:label value:@"" table:nil]];

    return(result);
}

- (NSLocale *) forceEnglishLocalization;
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];     
    [defaults setObject:@"en" forKey:@"LocalizedLanguage"];  

    NSLocale * currentLocale;

    currentLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en"];

    return(currentLocale);
}

Need help localizing your Xcode apps? 需要本地化Xcode应用程序的帮助?

https://itunes.apple.com/mg/app/generate-localizable-strings/id890673579?mt=12 https://itunes.apple.com/mg/app/generate-localizable-strings/id890673579?mt=12

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM