Created
July 26, 2016 12:47
-
-
Save mekanics/776e74eeecb365df5f849383b27d5386 to your computer and use it in GitHub Desktop.
Localizing Plurals in Swift
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
print(~"foo") | |
// bar | |
print("LOCALIZED STRING KEY"~[0]) | |
// FORMAT OF STRING WITH ZERO HERE | |
print("LOCALIZED STRING KEY"~[1]) | |
// FORMAT OF STRING WITH ONE HERE | |
print("LOCALIZED STRING KEY"~[7]) | |
// FORMAT OF STRING WITH 7 HERE |
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
// the strings without format can be included here | |
"foo" = "bar"; |
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
<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>LOCALIZED STRING KEY</key> | |
<dict> | |
<key>NSStringLocalizedFormatKey</key> | |
<string>FORMAT OF STRING WITH %#@VARIABLE@ HERE</string> | |
<key>VARIABLE</key> | |
<dict> | |
<key>NSStringFormatSpecTypeKey</key> | |
<string>NSStringPluralRuleType</string> | |
<key>NSStringFormatValueTypeKey</key> | |
<string>zd</string> | |
<key>zero</key> | |
<string>ZERO</string> | |
<key>one</key> | |
<string>ONE</string> | |
<key>two</key> | |
<string></string> | |
<key>few</key> | |
<string></string> | |
<key>many</key> | |
<string></string> | |
<key>other</key> | |
<string>%lu</string> | |
</dict> | |
</dict> | |
</dict> | |
</plist> |
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
import UIKit | |
prefix func ~ (key: String) -> String { | |
return NSLocalizedString(key, comment: "No Comment") | |
} | |
infix operator ~ {associativity left precedence 150} | |
func ~ (key: String, args: [CVarArgType]) -> String { | |
let localizedString = NSLocalizedString(key, comment: "No Comment") | |
return String(format:localizedString, arguments:args) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment