xcode iPhone:即使设置了另一个 lang,NSLocalizedString 也会从英文 .strings 文件中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2971513/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 19:25:09  来源:igfitidea点击:

iPhone: NSLocalizedString reading from English .strings file even when another lang is set

iphonexcodelocalization

提问by Ben Zotto

I have an app that has some non-US Localizable.strings files. They appear in the project as I expect them to: there's a Localizable.strings object, and sub objects for "en", "fr", etc. Each of those is a UTF16 text file, and I've verified that they get propagated into the build package as correct binary plist files.

我有一个包含一些非美国 Localizable.strings 文件的应用程序。它们出现在我期望的项目中:有一个 Localizable.strings 对象,以及“en”、“fr”等的子对象。每个都是一个 UTF16 文本文件,我已经验证它们得到了传播作为正确的二进制 plist 文件进入构建包。

When running my app, though, even when device settings are some other language, only the strings from my English Localizable.strings file come back

但是,当运行我的应用程序时,即使设备设置是其他语言,也只有来自我的英文 Localizable.strings 文件的字符串返回

Preferred language from NSLocale is e.g. "fr" AND I can see that the device setting is getting through somehow, because the system toolbar buttons ("undo", etc) are translated.

NSLocale 的首选语言是例如“fr”,我可以看到设备设置以某种方式通过,因为系统工具栏按钮(“撤消”等)被翻译。

But my strings are still in English. (Note that they are coming from the .strings file, as I've edited that to verify.)

但是我的字符串仍然是英文的。(请注意,它们来自 .strings 文件,因为我已经对其进行了编辑以进行验证。)

So: strings files seem OK, and get built and deployed OK.

所以:字符串文件看起来没问题,构建和部署也没问题。

Is there something else I need to do to "tell" the project that other locales are supported?

我还需要做些什么来“告诉”项目支持其他语言环境吗?

Thanks!

谢谢!

回答by ThomasRS

And if you run an application with the Localizable.strings file in the root of the project, cleaning is not enough, you also need to delete the application from the simulator.

而且如果你在项目的根目录下运行一个带有Localizable.strings文件的应用程序,清理还不够,你还需要从模拟器中删除该应用程序。

回答by Ben Zotto

This happens (apparently) when there is a Localizable.strings file in the root of the project in addition to in the individual .lproj directories. The app seems to always prefer that if it's present. The fact that it was there was likely an artifact of mucking around trying to get the localizations working in the project.

当项目的根目录中以及单独的 .lproj 目录中还有一个 Localizable.strings 文件时,就会发生这种情况(显然)。如果它存在,该应用程序似乎总是更喜欢它。事实上,它在那里很可能是试图让本地化在项目中发挥作用的工件。

I'm leaving this question here in case others run into this issue.

我把这个问题留在这里以防其他人遇到这个问题。

回答by Rob

It can also help to uninstall the application from the simulator after localizing a .NIB, as the localized version will have two (or more) new files in new locations (like, 'en.lproj', 'fr.lproj'). However, when you build to the simulator, it's too stupid to know to delete the old .xib file, so it'll keep loading the old stale xib file, not your new localized version until you clean the device out by reinstalling the app.

在本地化 .NIB 之后,它还可以帮助从模拟器卸载应用程序,因为本地化版本将在新位置(例如“en.lproj”、“fr.lproj”)有两个(或更多)新文件。但是,当您构建到模拟器时,知道删除旧的 .xib 文件太愚蠢了,因此它会继续加载旧的陈旧 xib 文件,而不是您的新本地化版本,直到您通过重新安装应用程序清除设备。

回答by Gianluca C

I solved this way: I deleted the app from the simulator and in xcode: Product -> Clean

我是这样解决的:我从模拟器和 xcode 中删除了应用程序:Product -> Clean

回答by Adam Eberbach

For the particular problem of en_US always coming back, you need to be querying preferredLanguagesfor the array of preferred languages currently, not currentLocale.

对于 en_US 总是回来的特定问题,您需要查询preferredLanguages当前的首选语言数组,而不是currentLocale.

Does NSLocalizedString does not return the keyed string from the correct file?

NSLocalizedString 是否不从正确的文件中返回键控字符串?

IN localization you have to be sure you are changing what you think you are changing. If you change Language in preferences then it will affect the string that comes back from your localizable.strings file. If you change settings under Region Format that's only date, currency symbol etc.

在本地化中,您必须确保正在更改您认为正在更改的内容。如果您在首选项中更改语言,那么它将影响从 localizable.strings 文件返回的字符串。如果您更改区域格式下的设置,则只有日期、货币符号等。