iPhone 应用程序使用的语言与操作系统/设备中设置的语言不同?

发布于 2024-09-12 14:55:22 字数 151 浏览 7 评论 0原文

是否可以让我的应用程序以不同于操作系统中设置的语言运行?我想在我的应用程序的设置菜单中进行语言切换,例如,用户可以在其中为应用程序选择德语,而他的系统以英语运行。

从我已经读过的内容来看,如果没有我自己的本地化机制,这似乎是不可能的......

你觉得怎么样?

Is it possible to have my app running in a different language than the one that is set in the OS? I want to have a language switch in my app’s setting menu, where the user can e.g. select german for the app, while his system is running in english.

From what I’ve already read, it seems it’s not possible without having my own localization mechanism…

What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

少钕鈤記 2024-09-19 14:55:22

它可以很容易地完成,尽管我花了几周的时间才弄清楚如何做;-)

我有一个名为 iDEX 的 iPhone 应用程序,它是一本罗马尼亚语词典。在 iPad 上,罗马尼亚语不可用作 UI 语言,因此我想为用户提供在应用程序设置中选择罗马尼亚语的选项,因为设备设置不包含罗马尼亚语。

您所要做的就是像往常一样国际化应用程序并本地化它。然后,在主方法中,将标准 NSUserDefaultsAppleLanguages 键设置为您的语言和国家/地区选择,就像这样

int main(int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSArray arrayWithObject:@"ro_RO"] forKey:@"AppleLanguages"];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool drain];
    return retVal;
}

在运行 UIApplicationMain() 之前必须设置 AppleLanguages 键,因为它此时 UIKit 框架已加载并确定了默认语言。

同样重要的是,您指定语言和国家/地区(例如,@“ro_RO”而不仅仅是@“ro”),否则您的应用程序可能会在设置密钥时崩溃。

通过这样做,对支持本地化的 NSBundle 的所有调用都将查找您以编程方式建立的语言,而不是设备上设置的语言。

希望有帮助...

it can be done easily although it took me weeks to work out how ;-)

I have an iPhone app called iDEX which is a Romanian dictionary. On the iPad, Romanian is not available as a UI language, so I wanted to give the user the option of selecting Romanian in the application settings, as the device settings do not include it.

What you have to do is internationalize the app and localize it as usual. Then, in the main method, set the AppleLanguages key of the standard NSUserDefaults to the language and country of your choice, like so

int main(int argc, char *argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSArray arrayWithObject:@"ro_RO"] forKey:@"AppleLanguages"];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool drain];
    return retVal;
}

It is imperative that you set the AppleLanguages key before you run UIApplicationMain(), because it is at that point that the UIKit framework is loaded and the default language determined.

It also important that you specify both language and country (eg, @"ro_RO" and not just @"ro") otherwise your app might crash when setting the key.

By doing this, all calls to NSBundle that support localization will look for the language that you have programatically established, as opposed to the one set on the device.

Hope that helps...

怎樣才叫好 2024-09-19 14:55:22

您需要实现自定义资源加载机制来实现这一目标。例如,您将无法再使用 NSLocalizedString 宏或 [NSBundle pathForResource:]。您始终必须指定包括本地化的路径(如 de.lproj/MyStrings.strings 而不仅仅是 MyStrings.strings)。除非绝对必要,否则我建议不要这样做。

You need to implement your custom resource loading mechanism to achieve that. For example you will no longer be able to use NSLocalizedString macro or [NSBundle pathForResource:]. You will always have to specify paths including the localization (as in de.lproj/MyStrings.strings instead of just MyStrings.strings). I would suggest against doing this unless absolutely necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文