Skip to content

Instantly share code, notes, and snippets.

@bill1m
Last active February 26, 2020 17:27
Show Gist options
  • Save bill1m/acb564cccaa59b4fae53ae0e02e6ddd5 to your computer and use it in GitHub Desktop.
Save bill1m/acb564cccaa59b4fae53ae0e02e6ddd5 to your computer and use it in GitHub Desktop.
Xcode playground localizedStringWithFormat
import Cocoa
import PlaygroundSupport
//: ### Put Localizable.stringsdict in ~/Documents/Shared Playground Data
let bundle = Bundle(url: playgroundSharedDataDirectory)!
let format1 = NSLocalizedString("beers_on_wall", tableName: nil, bundle: bundle, value: "", comment: "")
let format2 = NSLocalizedString("next_step", tableName: nil, bundle: bundle, value: "", comment: "")
let format3 = NSLocalizedString("after_next_step", tableName: nil, bundle: bundle, value: "", comment: "")
let beers = 0...99
for beer in beers.reversed() {
let initial = String.localizedStringWithFormat(format1, beer, beer)
let nextStep = String.localizedStringWithFormat(format2, beer)
var nextBeer = beer - 1
if nextBeer < 0 {
nextBeer = beers.max()!
}
let afterNext = String.localizedStringWithFormat(format3, nextBeer)
print("\(initial)\n\(nextStep) \(afterNext)\n")
}
<?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>beers_on_wall</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@beers@</string>
<key>beers</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>other</key>
<string>%d bottles of beer on the wall, %d bottles of beer!</string>
<key>one</key>
<string>%d bottle of beer on the wall, %d bottle of beer!</string>
<key>zero</key>
<string>No bottles of beer on the wall, no bottles of beer!</string>
</dict>
</dict>
<key>next_step</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@next@</string>
<key>next</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>other</key>
<string>Take one down, pass it around,</string>
<key>one</key>
<string>Take one down, pass it around,</string>
<key>zero</key>
<string>Go to the store and buy more beer,</string>
</dict>
</dict>
<key>after_next_step</key>
<dict>
<key>NSStringLocalizedFormatKey</key>
<string>%#@after_next@</string>
<key>after_next</key>
<dict>
<key>NSStringFormatSpecTypeKey</key>
<string>NSStringPluralRuleType</string>
<key>NSStringFormatValueTypeKey</key>
<string>d</string>
<key>other</key>
<string>%d bottles of beer on the wall.</string>
<key>one</key>
<string>%d bottle of beer on the wall.</string>
<key>zero</key>
<string>no bottles of beer on the wall.</string>
</dict>
</dict>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment