Skip to content

Instantly share code, notes, and snippets.

@yannickl
Created November 7, 2016 18:30
Show Gist options
  • Save yannickl/f009af4e934bfb45f8aaf334e6b7c497 to your computer and use it in GitHub Desktop.
Save yannickl/f009af4e934bfb45f8aaf334e6b7c497 to your computer and use it in GitHub Desktop.
Variadic parameters for localization in swift
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>total_word</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@value@</string>
<key>value</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>zero</key>
<string>No word</string>
<key>one</key>
<string>%d word</string>
<key>other</key>
<string>%d words</string>
</dict>
</dict>
</dict>
</plist>
extension String {
func localized(from: String) -> String {
return NSLocalizedString(self, tableName: from, value: self, comment: "")
}
func localizedFormat(from: String, _ arguments: CVarArg...) -> String {
return String.localizedStringWithFormat(localized(from: from), arguments)
}
}
let result2 = "total_word".localizedFormat(from: "MyLocalizable", 8)
// 200 233 989 words
let result2 = String.localizedStringWithFormat("total_word".localized(from: "MyLocalizable"), 8)
// 8 words
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment