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 localization
throws error:
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?
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With