-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed template system and moved cache into Translator class
- Loading branch information
Showing
4 changed files
with
24 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
__pycache__/ | ||
*.pyc |
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,29 @@ | ||
import csv | ||
|
||
|
||
_translators = {} | ||
|
||
|
||
class Translator: | ||
def __init__(self, filename): | ||
def __init__(self, filename, cache=True): | ||
self._cache_key = filename | ||
self._cache = cache | ||
reader = open(filename, 'r') | ||
self.content = tuple(csv.reader(reader)) | ||
reader.close() | ||
if self._cache and self._cache_key not in _translators.keys(): | ||
_translators[self._cache_key] = self.content | ||
|
||
def get_translations(self): | ||
if self._cache: | ||
return _translators[self._cache_key] | ||
else: | ||
return self.content | ||
|
||
def translate(self, sentence, **kwargs): | ||
for row in self.content: | ||
if row[0] == sentence: | ||
for key, value in kwargs.items(): | ||
row[1] = row[1] % {key: value} | ||
return row[1] | ||
return sentence | ||
translation = dict(self.get_translations()).get(sentence) | ||
if not translation: | ||
return sentence | ||
for key, value in kwargs.items(): | ||
translation = translation % {key: value} | ||
return translation |