Skip to content

Instantly share code, notes, and snippets.

@designatednerd
Last active February 26, 2020 17:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save designatednerd/fdfb916cc4d4ad3f33c25e917a95a2be to your computer and use it in GitHub Desktop.
Save designatednerd/fdfb916cc4d4ad3f33c25e917a95a2be to your computer and use it in GitHub Desktop.
99 Bottles of Beer On The Playground
import Cocoa
let beers = 0...99
for beer in beers.reversed() {
let initial = String.localizedStringWithFormat(NSLocalizedString("beers_on_wall", comment: ""), beer, beer)
let nextStep = String.localizedStringWithFormat(NSLocalizedString("next_step", comment: ""), beer)
var nextBeer = beer - 1
if nextBeer < 0 {
nextBeer = beers.max()!
}
let afterNext = String.localizedStringWithFormat(NSLocalizedString("after_next_step", comment: ""), 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>
@designatednerd
Copy link
Author

To get this to run on a system where the language is English, put the Localizable.stringsDict at the path Resources/en.lproj/Localizable.stringsDict. If your system language is not in english, you'll need to put it in an lproj folder that corresponds with your language.

@bill1m
Copy link

bill1m commented Feb 26, 2020

I couldn't get it to work in Resources/en.lproj/Localizable.stringsdict or stringsDict ... but it works in the ~/Documents/Shared Playground Data/Localizable.stringsdict. Here's my version

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment