Skip to content

Instantly share code, notes, and snippets.

@jorgenisaksson
Last active April 3, 2017 08:54
Show Gist options
  • Save jorgenisaksson/aed825a91a9508d3a439a4e863b3fa94 to your computer and use it in GitHub Desktop.
Save jorgenisaksson/aed825a91a9508d3a439a4e863b3fa94 to your computer and use it in GitHub Desktop.
String extension to localize strings with parameter support
extension String
{
public static func localize(_ key: String, comment: String) -> String {
return NSLocalizedString(key, comment: comment)
}
func format(parameters: CVarArg...) -> String {
return String(format: self, arguments: parameters)
}
}
// use
let helloWorld = String.localize("Hello world!", comment: "")
// use with parameters
let country = "Sweden"
let helloCountry = String.localize("Hello %@", comment: "").format(parameters: country!)
// add a script build-phase in xcode to export all strings to file for localization
# generate strings for swift files
find . -name "*.swift" ! -name "StringExtensions.swift" -print0 | xargs -0 genstrings -s localize -o "MyProj/Base.lproj"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment