Last active
April 3, 2017 08:54
-
-
Save jorgenisaksson/aed825a91a9508d3a439a4e863b3fa94 to your computer and use it in GitHub Desktop.
String extension to localize strings with parameter support
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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