Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSLocalizedString should be used directly for exporting XLIFF?

I used to use NSLocalizedString by custom function.

For example, to access Profile.strings, I define this function:

func LocalizedProfile(key: String, comment: String?) { NSLocalizedString(key, tableName: "Profile", comment: comment ?? "") }

And, called like this:

let localized = LocalizedProfile("Submit", comment: "For registration")

This method works fine except for exporting XLIFF.

On the Xcode 6.3.2, executting Export for localizationthrows error:

enter image description here

To get error information, I executed via command line:

xcodebuild -exportLocalizations -localizationPath ./xliff -project MyApp.xcodeproj -exportLanguage ja

And, I got this error:

Bad entry in file /Users/mono/Documents/Git/MyApp/Localization.swift (line = 29): Argument is not a literal string.

Defining custom localization method is very useful for me, but I also want to use exporting XLIFF feature.

Are there any methods to resolve this demands?

like image 607
mono Avatar asked Jun 27 '15 01:06

mono


1 Answers

Export For Localization and xcodebuild -exportLocalizations both generate strings files by looking for invocations of NSLocalizedString(_:tableName:bundle:value:comment:) in code and uses the static values passed into the parameters to create the appropriate strings files.

This means that the only values you can pass into key, comment, value, and tableName are string literals.

Since you're using a wrapper function around NSLocalizedString(_:comment:) to localize your strings, the only time Xcode sees you calling NSLocalizedString(_:comment:) is in that one wrapper function with non-string-literal values, which is invalid.

What you really want to do instead is call NSLocalizedString(_:tableName:comment:) directly.

Alternatively, you can call Bundle.localizedString(forKey:value:table:) in your wrapper function, but then you have to manually create your own strings files for those key-value pairs.

/// - parameter comment: This value is ignored, but should be provided for 
///   context at the call site about the requested localized string.
func LocalizedProfile(key: String, comment: String?) -> String {
    return Bundle.main.localizedString(forKey: key, value: nil, table: "Profile")
}
like image 95
sbrun Avatar answered Oct 28 '22 04:10

sbrun