Skip to content

Instantly share code, notes, and snippets.

@jul
Created October 21, 2012 17:44
Show Gist options
  • Select an option

  • Save jul/3927784 to your computer and use it in GitHub Desktop.

Select an option

Save jul/3927784 to your computer and use it in GitHub Desktop.
light trad system with serialized dict (any format that serialize dicts
import os
import codecs
import json
""" $ cat *json
{"welcome": { "fr": "bienvenue", "es": "holla " }}
"""
class Translator(object):
def __init__(self,path=".",_format="json"):
try:
thaw = __import__(_format)
except Exception as e:
raise Exception("Error while loading format <%s> %s" % (_format,e))
self._translation_table = dict({})
for f in os.listdir(path):
if f.endswith(_format):
with codecs.open(f,"r","utf-8") as frozen:
self._translation_table.update(thaw.load(frozen))
def __call__(self, word, lang="en"):
return self._translation_table.get(word, {word:lang}) .get(lang,word)
_=Translator()
print _("welcome", "fr")
print _("welcome", "es")
print _("welcome", "de")
@mouuff
Copy link

mouuff commented Oct 22, 2012

Traducteur mot a mot....en gros tu peux pas traduire de phrases

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