xcode Localizable.strings 无法加载所有其他构建?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11481348/
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
Localizable.strings failing to load every other build?
提问by beta
I have been having a strange problem with the localization system built into Cocoa. I used genstrings to create a localizable.strings file for my project, and the file loads and replaces the strings as expected in my app.
我在 Cocoa 中内置的本地化系统中遇到了一个奇怪的问题。我使用 genstrings 为我的项目创建了一个 localizable.strings 文件,该文件在我的应用程序中按预期加载和替换字符串。
However, it appears to only work every other build. I will build the code with XCode, test it on my device, and it will display the correct strings no problem. The next build, however, will fail to load the strings file (At least, that's what I'm assuming.) This is not random, but predictably every other build. I am doing nothing fancy with the Localizable.strings file.
但是,它似乎只适用于所有其他构建。我将使用 XCode 构建代码,在我的设备上测试它,它会显示正确的字符串,没问题。然而,下一个构建将无法加载字符串文件(至少,这是我的假设。)这不是随机的,而是可以预见的每个其他构建。我对 Localizable.strings 文件没有任何幻想。
I have no idea where to even start in diagnosing this issue, and I was wondering if anyone had experience with doing localizations on Cocoa.
我什至不知道从哪里开始诊断这个问题,我想知道是否有人有在 Cocoa 上进行本地化的经验。
I am using NSLocalizedString throughout my code base like so:
我在整个代码库中都使用 NSLocalizedString,如下所示:
NSLocalizedString(@"ReallyNewGame", @"Are you sure you want to start a new game?")
The corresponding entry in my Localizable.strings file:
我的 Localizable.strings 文件中的相应条目:
/* Are you sure you want to start a new game? */
"ReallyNewGame" = "Do you really want to start a new game?";
Here are the relavent parts of my Info.plist:
这是我的 Info.plist 的相关部分:
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
Here is a screenshot of what happens every other build of the app:
这是应用程序的每个其他版本发生的情况的屏幕截图:
Correct:
正确的:
Incorrect:
不正确:
I am baffled as to why this happens. I do not do anything manually with the Localizable.strings file and I have cleaned my project several times on XCode. Any pointers in the right direction would be greatly appreciated. If you need any more information, I will attempt to provide it.
我很困惑为什么会发生这种情况。我没有对 Localizable.strings 文件进行任何手动操作,而且我已经在 XCode 上多次清理了我的项目。任何指向正确方向的指针将不胜感激。如果您需要更多信息,我会尽力提供。
Thanks!
谢谢!
采纳答案by user2338215
In case it helps anyone else:
如果它可以帮助其他人:
I encountered exactly the same problem: every other build the localizations would not work. I found when I inspected the bundle contents that the Localizable.strings in the en.lproj were corrupt - the file was only 76 bytes long when it should have been 4k. The next build the corruption was gone, then back again, then gone...
我遇到了完全相同的问题:其他所有构建本地化都不起作用。我在检查包内容时发现 en.lproj 中的 Localizable.strings 已损坏 - 该文件只有 76 字节长,而本应为 4k。下一个构建腐败消失了,然后又回来了,然后消失了......
It turned out that I had copied over an extra Localizable.strings folder into my project when I had copied a folder from another project. When I deleted the extra Localizable.strings folder everything magically worked. Whew!
事实证明,当我从另一个项目中复制了一个文件夹时,我已经将一个额外的 Localizable.strings 文件夹复制到了我的项目中。当我删除额外的 Localizable.strings 文件夹时,一切都神奇地工作了。哇!
回答by David H
I cannot give you a straight out answer but can suggest some ways to proceed. I don't actually have a multi language app now, so most of this is what I've gleaned by reading up (I may have one soon so your issue is of interest to me):
我不能给你一个直接的答案,但可以建议一些方法来继续。我现在实际上没有多语言应用程序,所以大部分内容是我通过阅读收集的(我可能很快就会有一个,所以我对您的问题感兴趣):
1) Apple has deprecated the user of English.lproj in favor of en.lproj. In any case, its important that if you use "English" as your CFBundleDevelopmentRegion value, that the folder be named "English" and not "en".
1) Apple 已弃用 English.lproj 的用户,转而使用 en.lproj。在任何情况下,重要的是如果您使用“English”作为您的 CFBundleDevelopmentRegion 值,则该文件夹被命名为“English”而不是“en”。
2) Your strings file should be UTF16, and noted as such in the file inspector (that right most pane in Xcode)
2)您的字符串文件应该是 UTF16,并在文件检查器中注明(Xcode 中最右侧的窗格)
3) There is a nice previous questionthat has some graphical pointers on insuring that your localization files are correctly entered in Xcode (so Xcode knows about then, and knows it must process them).
3) 之前有一个很好的问题,它有一些图形指针,用于确保您的本地化文件在 Xcode 中正确输入(因此 Xcode 知道那时,并且知道它必须处理它们)。
4) Its possible your file has gotten corrupt, the Resource Guide says you can run "plutil -lint Localizable.strings" on it to verify correctness
4)你的文件可能已经损坏,资源指南说你可以在它上面运行“plutil -lint Localizable.strings”来验证正确性
5) As a side note, a number of people have pointed to Mac App Store Appas a nice utility to merge (not overwrite) strings files (as you make additions).
5) 作为旁注,许多人指出Mac App Store 应用程序是一个很好的工具来合并(而不是覆盖)字符串文件(当你添加时)。
6) If things still look good, then add the following to your AppDelegate "didFinishLaunchingWithOptions" (at the top) when the app first launches:
6) 如果看起来仍然不错,那么在应用程序首次启动时将以下内容添加到您的 AppDelegate "didFinishLaunchingWithOptions"(顶部):
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSLog(@"Strings file: %@", [bundel pathForResource:@"Localizable" ofType:@".strings"]);
NSLog(@"Localizations: %@" [bundle localizations]);
NSLog(@"Local Dict: %@", [bundle localizedInfoDictionary]];
NSLog(@"localizedStringForKey: %@", [bundle localizedStringForKey:@"ReallyNewGame"value:@"WTF???" table:nil];
NSLog(@"Localized String: %@", NSLocalizedString(@"ReallyNewGame", @"Are you sure you want to start a new game?"));
exit(0); // just testing the above for now
Run the app several times. The output should be the same. If its not add a comment to this answer and we can drill down further. If it is the same, well, then something is causing corruption further on in your app.
多次运行该应用程序。输出应该是一样的。如果它没有为此答案添加评论,我们可以进一步深入研究。如果它是相同的,那么有什么东西会导致您的应用程序进一步损坏。
回答by kajot
This issue occurs when:
在以下情况下会出现此问题:
- you have at least two files
*/<locale>.lproj/<table_name>.strings
, eg two files*/en.lproj/Localizable.strings
- you support more than one language with your
*.strings
files
- 你至少有两个文件
*/<locale>.lproj/<table_name>.strings
,例如两个文件*/en.lproj/Localizable.strings
- 您的
*.strings
文件支持不止一种语言
You probably wouldn't create two+ .strings
files with the same name. This problem usually occurs when you use external librarythat has Localizable.strings
file(s) in its resources. You probably have Localizable.strings
file(s) in yourresources - and that's the conflict that XCode fails to resolve.
您可能不会创建两个以上.strings
的同名文件。当您使用在其资源中包含文件的外部库时,通常会发生此问题Localizable.strings
。你可能有Localizable.strings
文件(S)在你的资源-就是这样的XCode未能解决冲突。
TL; DR;General tip: if you create a libraryto be used as a third party code by other developers, instead of creating Localizable.strings
file and using NSLocalizedString()
in it, create custom-named localizable strings table (e.g. MyLibName.strings
) and use NSLocalizedStringFromTable
.
TL; 博士;一般提示:如果您创建一个库以供其他开发人员用作第三方代码,而不是创建Localizable.strings
文件并NSLocalizedString()
在其中使用,请创建自定义命名的可本地化字符串表(例如MyLibName.strings
)并使用NSLocalizedStringFromTable
.
Problem example and detailed description:
问题示例及详细描述:
I've created a "problem-demo" repository: https://github.com/kajot/LocalizedStringsMergingFailure
我创建了一个“问题演示”存储库:https: //github.com/kajot/LocalizedStringsMergingFailure
Specifically with a test that fails every other run: https://github.com/kajot/LocalizedStringsMergingFailure/blob/master/LocalizedStringsMergingFailureTests/LocalizedStringsMergingFailureTests.m
特别是每次运行失败的测试:https: //github.com/kajot/LocalizedStringsMergingFailure/blob/master/LocalizedStringsMergingFailureTests/LocalizedStringsMergingFailureTests.m
│? ├── KJAppDelegate.h
│?? ├── KJAppDelegate.m
│?? ├── Localizations1
│?? │?? ├── de.lproj
│?? │?? │?? └── Localizable.strings
│?? │?? └── en.lproj
│?? │?? └── Localizable.strings
│?? ├── Localizations2
│?? │?? ├── de.lproj
│?? │?? │?? └── Localizable.strings
│?? │?? └── en.lproj
│?? │?? └── Localizable.strings
Every OTHERbuild, XCode will create a corrupted Localizable.strings
file in the bundle. Solution: do NOT create/add more than one LocalizedString
table with the same name to the same target.
每个OTHER构建,XCode 都会Localizable.strings
在包中创建一个损坏的文件。解决方案:不要LocalizedString
为同一目标创建/添加多个同名表。
LocalizedString
table is a set of */<locale>.lproj/<tableName>.strings
files. In the example above, there are two tables, each named Localizable
(default name for a table).
LocalizedString
table 是一组*/<locale>.lproj/<tableName>.strings
文件。在上面的示例中,有两个表,每个表都已命名Localizable
(表的默认名称)。
If a table is called Localizable
, you get localized strings from the table by using
如果调用了一个表Localizable
,则可以通过使用从该表中获取本地化的字符串
`NSLocalizedString(key, optionalComment)`.
Solution:
解决方案:
You can either merge those two tables (concatenate corresponding files with translations from the same languages) or change the name of one of the tables.
您可以合并这两个表(连接具有相同语言翻译的相应文件)或更改其中一个表的名称。
Example of the second approach (changed name of one of the tables):
第二种方法的示例(更改其中一个表的名称):
│? ├── KJAppDelegate.h
│?? ├── KJAppDelegate.m
│?? ├── Localizations1
│?? │?? ├── de.lproj
│?? │?? │?? └── Localizable.strings
│?? │?? └── en.lproj
│?? │?? └── Localizable.strings
│?? ├── Localizations2
│?? │?? ├── de.lproj
│?? │?? │?? └── NewTableName.strings
│?? │?? └── en.lproj
│?? │?? └── NewTableName.strings
Now you can get a translation from the new table (NewTableName
) by using NSLocalizedStringFromTable(key, @"NewTableName", optionalComment)
and from the "original" table (Localizable
) using NSLocalizedString(key, optionalComment)
.
现在,您可以NewTableName
通过使用NSLocalizedStringFromTable(key, @"NewTableName", optionalComment)
从“原始”表 ( Localizable
) 和使用NSLocalizedString(key, optionalComment)
.
回答by Anibal Itriago
I just found something that was killing me! I had the same issue: Depending on the STRINGS file, sometimes it Works, some times doesn't/
我刚刚发现了一些正在杀死我的东西!我遇到了同样的问题:取决于 STRINGS 文件,有时它有效,有时则无效/
What I found, is that if some line in your .strings file finishes with double semicolon
我发现,如果 .strings 文件中的某行以双分号结尾
;;
all the lines below ware ignored!
下面的所有行都被忽略了!
The upper image works great: As you can see, the NSLocalizationString commands replaces correctly the strings at the template But the other image shows that, before the double semicolon, it will ignore everything. It is a complete mess! No resources compilation error, nothing. Just a nightmare!
上图效果很好:如您所见,NSLocalizationString 命令正确替换了模板中的字符串,但另一幅图显示,在双分号之前,它将忽略所有内容。真是一团糟!没有资源编译错误,什么都没有。简直是噩梦!
Hope it helps someone to don't get insomnia!
希望能帮到大家不要失眠!
回答by Maverick1st
I had an similar issue regarding my app-icon. I had several targets in my app and all with different app-icons. For some reason one of the app-icons was corrupt and thus all of my targets switched through the app-icons of the other targets. which is pretty odd, because one target should not know of the other targets app-icon, if it is not used for both targets and marked for both targets in xcode.
我的应用程序图标也有类似的问题。我的应用程序中有几个目标,并且都有不同的应用程序图标。由于某种原因,其中一个应用程序图标已损坏,因此我的所有目标都通过其他目标的应用程序图标切换。这很奇怪,因为一个目标不应该知道其他目标 app-icon,如果它没有用于两个目标并且在 xcode 中标记为两个目标。
I solved this issue through deleting the corrupt png and adding it anew to the project. If you do this with your i18n file it might help, too. But be sure, to remove not only the reference from xcode but the complete file. Best would be to open the file in some external editor like textwrangler and copy the text to a new file and then use this instead.
我通过删除损坏的 png 并将其重新添加到项目中解决了这个问题。如果您使用 i18n 文件执行此操作,它也可能有所帮助。但请确保,不仅要删除 xcode 中的引用,还要删除整个文件。最好是在一些外部编辑器(如 textwrangler)中打开文件并将文本复制到一个新文件,然后改用它。
Good Luck.
祝你好运。
回答by holex
the behaviour,what you've experienced, is totally normal.why?
这种行为,你所经历的,是完全正常的。为什么?
if you check these lines, the relevant part of original macro definitionof the NSLocalizedString
:
如果你检查这些行,原来的宏定义的相关部分的NSLocalizedString
:
#define NSLocalizedString(key, comment) \
[[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]
you could see the comment
parameter is deceptive parameter, it is never used. (it is just for the developer who reads the code)
您可以看到该comment
参数是欺骗性参数,从未使用过。(仅供阅读代码的开发者使用)
futhermore, if you check the NSBundle
class Reference, you could read the following interesting thing about the return value of the -localizedStringForKey:value:table:
method:
此外,如果您检查NSBundle
类 Reference,您可以阅读以下有关该-localizedStringForKey:value:table:
方法返回值的有趣内容:
Return Value
返回值
If value isnil
or an empty string, and a localized string is not found in the table, returns key.
如果 value 是nil
一个空字符串,并且在表中找不到本地化的字符串,则返回key。
this is why you get back your keybecause probably there is no value for this keyin your Localizable.string
files.
这就是您取回密钥的原因,因为您的文件中可能没有该密钥的价值Localizable.string
。
sad news, but it is normal.
悲伤的消息,但这很正常。
the solution would be the following:
解决办法如下:
NSLocalizedString(@"Are you sure you want to start a new game?", @"");
and in your Localizable.string
file:
并在您的Localizable.string
文件中:
@"Are you sure you want to start a new game?" = "Do you really want to start a new game?";
回答by iXcoder
other thread may helps... Multiple Localizable.strings files in one iOS app
其他线程可能会有所帮助...一个 iOS 应用程序中的多个 Localizable.strings 文件
again, maybe you can cleanup and then build the app from xcode...
再次,也许你可以清理然后从 xcode 构建应用程序......