Xcode 6.3.2 or 6.4beta Localization failed to read

2019-02-10 23:55发布

问题:

Yes, I know there are many discussions about this failure. But after checking them out, I think what I have now is different.

In here Error while "Export For Localization.." Xcode 6.3, it says the single quote (') should be replaced as (\') in the strings file.

In here Xcode 6 localization failed to read a strings file, it says that the NSLocalizedString should be commented out (if I understand it right).

To test the problem, I created a new project with 6.3.2 and 6.4(6E23). I can have single quotes in the strings file, but as long as there is NSLocalizedString (e.g. var str = NSLocalizedString("Hi", tableName: nil, bundle: NSBundle.mainBundle(), value: "Hi", comment: "Hi")) in the view controller file, the Xcode always pops up with

Then I did

xcodebuild -exportLocalizations -localizationPath ./xliff -project testtest.xcodeproj -exportLanguage en

and I get

Bad entry in file ViewController.swift (line = 26): Argument is not a literal string.

2015-06-15 11:08:22.177 xcodebuild[31272:1150546]

[MT] DVTAssertions: Warning in /SourceCache/IDEFrameworks/IDEFrameworks-7718/IDEFoundation/Localization/IDELocalizationWork.m:434

Details: Failed to read strings file "/var/folders/1s/_d08hx4j2gn9t6wlrc5_7gq00000gn/T/Xcode3SourceStringsAdaptor-48CAE246-EBA5-4326-B3C5-B5032A4027D8/Localizable.strings", underlying error:

The data couldn’t be read because it isn’t in the correct format.

Object: IDELocalizationWork

Method: +readStringsWorkForContext:

Thread: {number = 1, name = main} Please file a bug at http://bugreport.apple.com with this warning message and any useful information you can provide.

xcodebuild: error: Localization failed to read a strings file

This error persists even if I comment out Line 26, which is

var str = NSLocalizedString("Hi", tableName: nil, bundle: NSBundle.mainBundle(), value: "Hi", comment: "Hi")

But if I delete Line 26, the error is gone.

So, I guese, the XLIFF generator somehow does not allow "NSLocalizedString" to appear whatsoever??

Have you ever found this like I did?

回答1:

There is a several problems, that cause this error:

1. Empty Localizable.strings file.

Solution: Just add comment

    /** no localizable strings **/

to this file and problem is gone.

2. The 2-nd problem in escape symbol: \ (sometimes)

Solution: Remove it and figure out something with your special chars. And probably your export process will complete without errors.

Also \ produce another bug:

Sting to export:

"ACCOUNT" = "Foo\\\'Bar\'bazz\"";

String after importing just recently exported file:

"ACCOUNT" = "\\''";

Bonus:

If you will try to import localization during exporting Xcode will crash!

Welcome to localization hell by Apple!


P.S.

  • Still actual at least for Version 6.4 (6E35b)
  • Related openradar bug-report


回答2:

I've the same problem with Swift 3 on Xcode 8.1 because I had an extension

func  localized() -> String {
    return NSLocalizedString(self, comment: self)
} 

I fixed it removing the extension and using directly NSLocalizedString("", comment: ""). I really don't know why the extension didn't work when I try to export the .xliff file.



回答3:

I've seen this issue in Xcode 6.4 (6E35b). It appears related to including bundle: NSBundle.mainBundle() parameter in the method call.

Taking that parameter out (it has a default value) appears to fix the error.

So change from this:

var str = NSLocalizedString("Hi", tableName: nil, bundle: NSBundle.mainBundle(), value: "Hi", comment: "Hi")

to this:

var str = NSLocalizedString("Hi", tableName: nil, value: "Hi", comment: "Hi")

This is obviously some kind of bug in Xcode's string localization scanner (I think it is a naive approach where they are expecting people to only use NSLocalizedString(key, comment: comment) and therefore expecting only strings as parameters to that method).



回答4:

The reason you get the same behavior whether or not the line is commented out is that localized string detection does not exclude comments and so your NSLocaluzedString call gets discovered and therefore your .strings file is processed and so on.



回答5:

I have fixed this by just redefining the NSLocalizedString function like that:

public func NSLocalizedString(key: String, tableName: String? = nil, bundle: NSBundle = NSBundle.mainBundle(), value: String = "", comment: String) -> String
{
    return yourBundleHere.localizedStringForKey(key, value: value, table: tableName)
}

Replace yourBundleHere with NSBundle.mainBundle() or what ever you want.



回答6:

i have the same problem, and it was because i used an apostroph charachter ' by in unicode format like this \U0027 and when i replaced the u char to small \u0027 the error message gone.

also, you must not comment NSLocalizedString because it hangs the parser, you must delete it from code.