NSLocalizedString 只检索密钥,而不是 Localizable.strings (IOS) 中的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9251177/
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
NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS)
提问by Smiden
I've made a strings file named "Localizable.strings" and added two languages to it, like so:
我制作了一个名为“Localizable.strings”的字符串文件,并向其中添加了两种语言,如下所示:
"CONNECTIONERROR" = "Check that you have a working internet connection.";
"CONNECTIONERRORTITLE" = "Network error";
I have also converted the files to Unicode UTF-8 However, when I create a UIAlertView like this:
我还将文件转换为 Unicode UTF-8 但是,当我创建这样的 UIAlertView 时:
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(@"CONNECTIONERRORITLE",nil)
message:NSLocalizedString(@"CONNECTIONERROR",nil)
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
the alert view only shows the key text, not the value. It works if I, for example, set a UITextviews text to NSLocalizedString(@"CONNECTIONERROR",nil), but the alert view only displays the key. Anyone know what's wrong?
警报视图仅显示键文本,而不显示值。例如,如果我将 UITextviews 文本设置为 NSLocalizedString(@"CONNECTIONERROR",nil),它会起作用,但警报视图仅显示密钥。有谁知道怎么了?
采纳答案by Smiden
Tested the app on an actual device and it worked
在实际设备上测试了该应用程序并且它工作正常
回答by Jim Rota
In my case it was because I had mistakenly named the file "Localization.strings" and hadn't noticed (it has to be named Localizable.strings). As explained previously the symptom is because the compiler cannot find the string. Otherwise the cause could be any number of things but usually it's a missing semi colon or quotation mark. These are hard to find when you're doing a lot of localizations at once. The lesson learned is to start building your localization file early on in your development process and build it as you go, so that they are easier to spot.
就我而言,这是因为我错误地将文件命名为“Localization.strings”而没有注意到(它必须命名为 Localizable.strings)。如前所述,症状是因为编译器找不到字符串。否则原因可能有很多,但通常是缺少分号或引号。当您同时进行大量本地化时,很难找到这些。吸取的教训是在开发过程的早期开始构建本地化文件,并在进行时构建它,以便更容易发现它们。
回答by Pelanes
Same problem, solved using the filename: Localizable.strings
同样的问题,使用文件名解决:Localizable.strings
回答by alicanozkara
Change your .strings
file name to Localizable.strings
, it worked for me.
将您的.strings
文件名更改为Localizable.strings
,它对我有用。
回答by virsunen
Double check that the Localizable.strings file is being added to
仔细检查 Localizable.strings 文件是否被添加到
Targets -> BuildPhases -> Copy Bundle Resources
目标 -> BuildPhases -> 复制捆绑资源
It hadn't been added automatically for me.
它没有为我自动添加。
回答by aiwis31
I have been searching the solution for 5 hours, I tried everything I could to make my app localization working.
我一直在寻找解决方案 5 个小时,我尽我所能让我的应用程序本地化工作。
The problem was that one of the Pods of my project had a Localizable.strings file (actually it was Parse pod that had not renamed it). Therefore my Localizable.strings file was not recognized by my app.
问题是我项目的一个 Pod 有一个 Localizable.strings 文件(实际上是 Parse Pod 没有重命名它)。因此,我的应用程序无法识别我的 Localizable.strings 文件。
I fixed the issue by changing the name of the file to "MyappnameLocalizable.strings" and using NSLocalizedString this way:
我通过将文件名更改为“MyappnameLocalizable.strings”并以这种方式使用 NSLocalizedString 来解决该问题:
NSLocalizedString("key", tableName: "MyappnameLocalizable", comment: "comment")
回答by Andrew Lewis
I was having problems with this on the iOS Simulator. I ended up deleting the Localization.strings file in the simulator directory
我在 iOS 模拟器上遇到了这个问题。我最终删除了模拟器目录中的 Localization.strings 文件
( /Users/(me)/Library/Application Support/iPhone Simulator/5.0/Applications/(etc)/(project)/(application.app)
( /Users/(me)/Library/Application Support/iPhone Simulator/5.0/Applications/(etc)/(project)/(application.app)
cd to there and delete all copies of Localization.strings found there.
cd 到那里并删除在那里找到的 Localization.strings 的所有副本。
For some reason the usual rubber chicken voodoo of build clean, quit iOS Simulator, quit XCode, etc wasn't working, but this did. At least for me, today.
出于某种原因,构建清洁、退出 iOS 模拟器、退出 XCode 等通常的橡皮鸡巫术无法正常工作,但确实如此。至少对我来说,今天。
回答by DarkDust
This is happening when the runtime can't find the specified key, for whatever reason. In your case, it's most likely due to a typo: CONNECTIONERRORITLE
is missing a T
for TITLE
. Also pay attention to any warnings/error when compiling regarding the Localizable.strings
file: if there are unbalanced "
or missing ;
the file cannot be compiled/read correctly.
无论出于何种原因,运行时都找不到指定的键时,就会发生这种情况。在您的情况下,很可能是由于打字错误:CONNECTIONERRORITLE
缺少T
for TITLE
。在编译有关Localizable.strings
文件时还要注意任何警告/错误:如果文件不平衡"
或丢失;
,则无法正确编译/读取文件。
回答by Nick Greg
Change the name of the file to Localizable.strings, make sure the target in the file inspector is set. To avoid syntax errors right click on Localizable.strings file->open as->ASCII property list Also, cleaning the project and building again helped in my case.
将文件名更改为 Localizable.strings,确保设置了文件检查器中的目标。为了避免语法错误,右键单击 Localizable.strings 文件->打开为->ASCII 属性列表此外,清理项目并再次构建对我的情况有帮助。
回答by Pelanes
Rename the InfoPlist.strings file to Localizable.strings (double clic) and then you will get the correct string for that key.
将 InfoPlist.strings 文件重命名为 Localizable.strings(双击),然后您将获得该键的正确字符串。