iOS 应用程序的非英语默认语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4072349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Non-English default language for iOS App?
提问by James J
I am developing an app for a client in Europe. I am an English-speaker in the US. Our app is going to support a number of languages, but not English. I have all the strings in our app in translated Localized.strings files, set up correctly for the different languages, and they all work fine when the device is set to the correct language (device's language is German = app is correctly localized for German).
我正在为欧洲的客户开发应用程序。我在美国说英语。我们的应用程序将支持多种语言,但不支持英语。我的应用程序中的所有字符串都在翻译的 Localized.strings 文件中,为不同的语言正确设置,当设备设置为正确的语言时,它们都可以正常工作(设备的语言是德语 = 应用程序已正确本地化为德语) .
There is a problem when the device is not set to one of the languages we support, for example, on my phone which is set to English. We want the phone to fall back to German in cases like this, but that is not happening. What we are seeing is that the phone is using the language that appears highest on the Language list in the International section of Settings.app. For my phone, the highest non-English language on the list is French, so when I run the app, it is localized for French. If I change my phone to German and then back to English (which changes the order in the Languages list), the app then localizes to German.
当设备未设置为我们支持的语言之一时会出现问题,例如,我的手机设置为英语。在这种情况下,我们希望手机回退到德语,但这并没有发生。我们看到的是,手机正在使用 Settings.app 的“国际”部分的“语言”列表中显示最高的语言。对于我的手机,列表中最高的非英语语言是法语,因此当我运行该应用程序时,它已本地化为法语。如果我将手机更改为德语,然后再更改回英语(这会更改语言列表中的顺序),则该应用程序将本地化为德语。
How can I ensure the app defaults to German for non-supported languages? I used this tutorialto set up the locales for the project. This includes removing the default "English" locale that is created when you first localize a file. In the project file, I've added:
对于不受支持的语言,我如何确保应用程序默认为德语?我使用本教程来设置项目的语言环境。这包括删除第一次本地化文件时创建的默认“英语”语言环境。在项目文件中,我添加了:
developmentRegion = de;
Also, in the Info.plist, I have
另外,在 Info.plist 中,我有
<key>CFBundleDevelopmentRegion</key>
<string>de</string>
With no success.
没有成功。
Any ideas would be appreciated!
任何想法,将不胜感激!
回答by Pascal
Defaulting to a particular language is not what you should strive for. Suppose a japanese user living in France and fluently speaking French has his phone set to Japanese. Your App would default to German, despite it having a French translation; I guess that's not what you want and it's certainly not what the user wants.
默认使用特定语言不是您应该努力的目标。假设居住在法国并且会说流利法语的日本用户将他的电话设置为日语。您的应用程序将默认为德语,尽管它有法语翻译;我想这不是您想要的,当然也不是用户想要的。
For this reason I suggest to respect the language prioritization as set by the user.
出于这个原因,我建议尊重用户设置的语言优先级。
Since you don't want to support English but have developed the App in English, the easiest thing you can do is to just get rid of en.lproj
right after compiling. This will leave the app with only the languages you plan to support, and the iPhone will chose the language best suited for the user, as set in iPhone's defaults.
既然你不想支持英文,而是用英文开发了这个App,那么你最简单的办法就是en.lproj
编译后直接去掉。这将使应用程序仅保留您计划支持的语言,而 iPhone 将选择最适合用户的语言,如 iPhone 的默认设置。
There is a pretty straightforward solution to deleting a specific localization:
删除特定本地化有一个非常简单的解决方案:
Let Xcode build the App with all the present localizations and just before code-signing kicks in delete the en.lproj
folder and all localized files for that language are gone. You can easily do this by adding a Run Script build phase to the target, containing this one line of code (it's a Bash Script):
让 Xcode 使用所有现有本地化构建应用程序,并在代码签名开始之前删除该en.lproj
文件夹,该语言的所有本地化文件都消失了。您可以通过向目标添加一个运行脚本构建阶段来轻松地做到这一点,其中包含这一行代码(它是一个 Bash 脚本):
rm -r "${TARGET_BUILD_DIR}/${PRODUCT_NAME}.app/en.lproj"
Code Signing always kicks in after all build phases have completed, so just put that build phase at the end of your current phases.
代码签名总是在所有构建阶段完成后开始,所以只需将该构建阶段放在当前阶段的末尾。
Notethat this increases the time to build because Xcode recreates all English files, so you might want to disable this step during testing.
请注意,这会增加构建时间,因为 Xcode 会重新创建所有英文文件,因此您可能希望在测试期间禁用此步骤。
I hope that's a viable solution for you.
我希望这对您来说是一个可行的解决方案。
Concerning "the app must fall back to German in the case that an appropriate localization doesn't exist":
关于“在不存在适当本地化的情况下,应用程序必须回退到德语”:
Question is what is an appropriate localization? If the user's third choice is French (after 2 unsupported languages), this solution will make the app fall back to French, which isan appropriate localization. More appropriate than setting the user's fifth choice, German, by hand.
问题是什么是合适的本地化?如果用户的第三个选择是法语(在 2 种不受支持的语言之后),此解决方案将使应用程序回退到法语,这是一个合适的本地化。比手动设置用户的第五个选择德语更合适。
What happens when an app launches is simple: The OS descends the users language list, as set in Preferences, until it finds a matching localization, and uses this language. The reason many apps default to English and not German is simply because English appears on most user language lists above German. There is no inherent language preference to the system. If you delete the English translation, only the languages you want to support are there, and of these languages the one higher on a user's list is taken.
应用程序启动时发生的事情很简单:操作系统会按照“首选项”中的设置降低用户语言列表,直到找到匹配的本地化并使用该语言。许多应用默认使用英语而不是德语的原因仅仅是因为英语出现在德语之上的大多数用户语言列表中。系统没有固有的语言偏好。如果您删除英文翻译,则只有您想要支持的语言存在,并且会采用这些语言中用户列表中较高的一种。
回答by Pascal
I don't like posting this answer since it runs againstApples Human Interface Guidelines. Localizations are something the user should set, not something management should decide, so managers requiring a certain default language don't understand nothing and should change business.
我不喜欢发布这个答案,因为它违反了Apples 人机界面指南。本地化是用户应该设置的东西,而不是管理层应该决定的东西,所以需要某种默认语言的经理什么都不懂,应该改变业务。
That being said, there is a way to force iOS using certain languages in a certain order. I mainly use this to debug localizations without the need to set the device language to something else, whether it is possible to use this to solve the nonexistent problem in this question I can't tell.
也就是说,有一种方法可以强制 iOS 以特定顺序使用特定语言。我主要用它来调试本地化,而无需将设备语言设置为其他语言,是否可以使用它来解决这个问题中不存在的问题,我不知道。
There is a key AppleLanguages
in the user defaults that dictates the order of languages to be used, normally set to the order the user sets in iOS's preferences. If you change this order, the app will try to find localizations in that order first. What you could do is read the current language preferences and decide to default to German as follows:
AppleLanguages
用户默认值中有一个键指定要使用的语言的顺序,通常设置为用户在 iOS 首选项中设置的顺序。如果您更改此顺序,应用程序将首先尝试按该顺序查找本地化内容。您可以做的是阅读当前的语言首选项并决定默认为德语,如下所示:
NSArray *langOrder = [NSArray arrayWithObjects:@"de", nil];
[[NSUserDefaults standardUserDefaults] setObject:langOrder forKey:@"AppleLanguages"];
Note that you need to do this in main.m
before UIApplicationMain
, otherwise you need to restart the app for this change to take effect.
请注意,您需要在main.m
before 中执行此操作UIApplicationMain
,否则您需要重新启动应用程序才能使此更改生效。
I cannot state enough that nobody should do this in a real app, unless you go all the way and implement a preference setting that allows the user to change the language manually. If you just default to a given language, the user has no option to change yourchoice.
我不能说没有人应该在真正的应用程序中这样做,除非你一路走来实现一个允许用户手动更改语言的首选项设置。如果您只是默认使用给定的语言,则用户无法更改您的选择。
回答by geser
Try this in your didFinishLaunchingWithOptions() function
在你的 didFinishLaunchingWithOptions() 函数中试试这个
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"de_DE", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
int retVal = UIApplicationMain(argc, argv, nil, nil);
回答by Chowdhury Md Rajib Sarwar
To make the default language as a particular one this following code works like a charm which is already described in this page. Here is the swift version: (Swift 4.2)
为了使默认语言成为特定语言,以下代码就像本页中已经描述的魅力一样。这是快速版本:(Swift 4.2)
let arr = NSArray(objects: "ja")
UserDefaults.standard.set(arr, forKey: "AppleLanguages")