OGeek|极客世界-中国程序员成长平台

标题: ios - 试图读取 InfoPlist.strings 文件...获取 key (?) [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:32
标题: ios - 试图读取 InfoPlist.strings 文件...获取 key (?)

许多人不知道,在 iOS 和 OSX 上本地化应用程序名称的方法是将 InfoPlist.strings 文件添加到包中并本地化该文件。人们将此文件与 Info.Plist 文件混合使用。本地化不是在 Info.plist 中完成的,而是在 InfoPlist.strings 文件中完成的。

也就是说,我用两个键创建了这个文件:CFBundleDisplayNameCFBundleName,就像我一直做的那样。这非常有效,您可以在不同的本地化中为您的应用程序定义不同的名称。该文件可以无缝运行。您无需执行任何操作,只需将文件添加到项目中并进行本地化即可。

InfoPlist.strings 是这样的字符串文件:

"CFBundleDisplayName" = "My Localized App Name";
"CFBundleName" = "My Localized App Name";

对于这个项目,我还必须在运行时读取 CFBundleDisplayName 的值。

我已尝试使用此代码:

NSString *appName = [[NSBundle mainBundle] localizedStringForKey"CFBundleDisplayName" value:nil table"InfoPlist"];

读取 CFBundleDisplayName 键,但我得到的值是 CFBundleDisplayName。换句话说,我提供 key 并接收返回的 key ,而不是值。我应该会收到 My Localized App Name

我错过了什么?



Best Answer-推荐答案


我在 Swift 中完全做到了这一点,它按预期工作。

let appname = NSBundle.mainBundle().localizedStringForKey("CFBundleDisplayName", value: nil, table: "InfoPlist")

但是,如果我从相应的 InfoPlist.strings 文件中删除本地化字符串,它会返回 "CFBundleDisplayName"

因此,请检查您的 .strings 文件中是否缺少某些内容

After trying several possibilities..

BUG 是“项目中的 InfoPlist.strings 的第二个副本”

关于ios - 试图读取 InfoPlist.strings 文件...获取 key (?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35348982/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4