Skip to content

Commit

Permalink
Set current language
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantinos Bairaktaris committed Dec 6, 2020
1 parent d0081b3 commit f5d0af3
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
9 changes: 9 additions & 0 deletions tests/native/core/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,12 @@ def test_returns_entry_if_exists(self):
assert cache.get('chair', 'el') == u'Μια καρέκλα'
assert cache.get('invalid', 'en') is None
assert cache.get('invalid', 'el') is None

def test_contains(self):
cache = MemoryCache()
cache.update({
'lang1': (True, {'source1': {'string': "translation1"},
'source2': {'string': "translation2"}}),
})
assert 'lang1' in cache
assert 'lang2' not in cache
10 changes: 10 additions & 0 deletions tests/native/core/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,13 @@ def test_plural(self, cache_mock):
u"fr_FR",
params={'cnt': 2})
assert translation == u'OTHER'

@patch('transifex.native.core.CDSHandler.fetch_translations')
def test_set_current_language(self, mock_cds):
mock_cds.return_value = {'el': (True, {})}
tx = self._get_tx()
tx.remote_languages = [{'code': "el"}]
tx.set_current_language('el')

assert tx.current_language_code == 'el'
mock_cds.assert_called_once_with('el')
9 changes: 9 additions & 0 deletions transifex/native/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def update(self, data):
"""
pass

def __contains__(self, language_code):
""" Check whether language_code has already been added to the cache.
"""

pass


class MemoryCache(AbstractCache):
"""A cache that stores translations in memory."""
Expand Down Expand Up @@ -81,3 +87,6 @@ def get(self, key, language_code):
except (ValueError, AttributeError):
pass
return retrieved_translation

def __contains__(self, language_code):
return language_code in self._translations_by_lang
31 changes: 26 additions & 5 deletions transifex/native/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ def setup(self,
"""
if source_language is not None:
self.source_language_code = source_language
if current_language is not None:
self.current_language_code = current_language
if languages is not None:
self.hardcoded_language_codes = languages
if missing_policy is not None:
Expand All @@ -62,6 +60,9 @@ def setup(self,
secret=secret,
host=cds_host)

if current_language is not None:
self.set_current_language(current_language)

def fetch_languages(self, force=False):
if self.remote_languages is None or force:
self.remote_languages = self._cds_handler.fetch_languages()
Expand All @@ -73,17 +74,34 @@ def fetch_languages(self, force=False):
else:
return self.remote_languages

def set_current_language(self, language_code, force=False):
if language_code not in (language['code']
for language in self.fetch_languages()):
raise ValueError("Language {} is not supported by the application".
format(language_code))
if language_code not in self._cache or force:
self.fetch_translations(language_code=language_code, force=True)
self.current_language_code = language_code

def fetch_translations(self, language_code=None, force=False):
"""Fetch fresh content from the CDS."""
if language_code is None:
for language in self.fetch_languages():
self.fetch_translations(language['code'], force=force)
else:
translations = self._cds_handler.fetch_translations(language_code)
self._cache.update(translations)
if language_code not in [language['code']
for language in self.fetch_languages()]:
raise ValueError(
"Language {} is not supported by the application".
format(language_code)
)
if language_code not in self._cache or force:
translations = self._cds_handler.\
fetch_translations(language_code)
self._cache.update(translations)

def translate(
self, source_string, language_code, is_source=False,
self, source_string, language_code=None, is_source=False,
_context=None, escape=True, params=None
):
"""Translate the given string to the provided language.
Expand All @@ -106,6 +124,9 @@ def translate(
if params is None:
params = {}

if language_code is None:
language_code = self.current_language_code

translation_template = self.get_translation(source_string,
language_code,
_context,
Expand Down

0 comments on commit f5d0af3

Please sign in to comment.