NSLocalizedString should be used directly for exporting XLIFF?
Asked Answered
G

1

6

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?

Galle answered 27/6, 2015 at 1:28 Comment(1)
Did you find a solution? I'm experiencing the same issue... I've also tried to substituted keys with string constants.... but it doesn't work. The only solution seems to use a string directly as key.Lowerclassman
T
3

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")
}
Threefold answered 15/6, 2019 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.