Skip to content

Instantly share code, notes, and snippets.

@gelendir
Created October 24, 2010 01:40
Show Gist options
  • Save gelendir/642953 to your computer and use it in GitHub Desktop.
Save gelendir/642953 to your computer and use it in GitHub Desktop.
translate string into klingon stupidly
"""
Stupid Klingon Translator (SKT)
hacked together in 5 minutes by :
Gregory Eric Sanderson Turcot Temlett MacDonnell Forbes
Hubert and Mathieu gave me the brilliant idea of implementing a gettext klingon translator
for python projets that use internationalization. Instead of diving in to the
gettext API (BOOOOORING) I hacked together a small function that scrapes Mr.Klingon's
website for translated strings. (http://mrklingo.freeshell.org/uta/index.php)
Somebody who REALLY wanted to internationalize his project into klingon
can probably do something like :
from skt import translate as _
and use the usual gettext tools.
This program is licensed under the WTFPL license.
"""
import urllib
import re
URL = "http://mrklingo.freeshell.org/uta/index.php"
regex = re.compile(r"-{12}\s+(.+?)\s+\-{12}", re.MULTILINE)
cache = {}
def translate(phrase):
try:
return cache[phrase]
except KeyError:
pass
handle = urllib.urlopen(URL, urllib.urlencode({'eng' : phrase, 'language' : 'klingon'}))
try:
text = handle.read()
except:
return phrase
match = regex.search(text)
if not match:
return phrase
cache[phrase] = match.group(1)
return match.group(1)
if __name__ == '__main__':
print translate("Today is a good day to die")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment