Created
October 21, 2012 17:44
-
-
Save jul/3927784 to your computer and use it in GitHub Desktop.
light trad system with serialized dict (any format that serialize dicts
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 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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Traducteur mot a mot....en gros tu peux pas traduire de phrases