Skip to content

Commit

Permalink
removed template system and moved cache into Translator class
Browse files Browse the repository at this point in the history
  • Loading branch information
vinyll committed Jan 26, 2018
1 parent c54488a commit 56dcc94
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__/
*.pyc
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

setup(
name='tcii18n',
version='0.1',
author='Hugo Mochet',
version='0.2',
author='Vincent Agnano',
license='Copyright Anthropedia',
long_description=open('readme.md').read(),
)
14 changes: 0 additions & 14 deletions tcii18n/template.py

This file was deleted.

27 changes: 20 additions & 7 deletions tcii18n/translator.py
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

0 comments on commit 56dcc94

Please sign in to comment.