Skip to content

Commit

Permalink
[Part] Add default key.
Browse files Browse the repository at this point in the history
Add the posibility to give a default key if the key is not found.

Fixes: #18
  • Loading branch information
hobbestigrou committed Mar 22, 2018
1 parent 33f6927 commit dda3111
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 29 deletions.
13 changes: 9 additions & 4 deletions pylocwolowitz/core.py
Expand Up @@ -16,16 +16,19 @@ class Pylocwolowitz(object):
:type path: str
:param format_deserializer: Indicate the serializer to use json or yaml
:type format_deserializer: str
:param default_key: Specify a default key if the key is not found
:type default_key: str
:raises ValueError: Format not supported, only json or yaml.
"""

def __init__(self, path, format_deserializer='json'):
def __init__(self, path, format_deserializer='json', default_key=None):
"""To init the locolization system."""
if format_deserializer not in ('json', 'yaml'):
raise ValueError('FormatNotSupported')

self.path = path
self.format_deserializer = format_deserializer
self.default_key = default_key
self.locales = defaultdict(dict)
self._find_file()

Expand Down Expand Up @@ -73,10 +76,12 @@ def loc(self, key, lang, values=None):
:returns: Translated to the requested language
:rtype: str
"""
if self.locales[key].get(lang) is None:
return key
if key in self.locales:
ret = self.locales[key].get(lang, key)
else:
ret = self.locales[self.default_key].get(lang, key) \
if self.default_key else key

ret = self.locales[key][lang] if key in self.locales else key
if values is None:
return ret
else:
Expand Down
28 changes: 18 additions & 10 deletions tests/basic_json_test.py
Expand Up @@ -4,28 +4,36 @@
from __future__ import unicode_literals


def test_sample(pylocwolowitz):
def test_sample(pylocwolowitz_json):
"""Simple test no placeholders"""
assert pylocwolowitz.loc('hello', 'es') == 'Hola'
assert pylocwolowitz_json.loc('hello', 'es') == 'Hola'


def test_sample_token(pylocwolowitz):
def test_sample_token(pylocwolowitz_json):
"""Tests with placeholders"""
fr = pylocwolowitz.loc('welcome {name}', 'fr', {'name': 'hobbestigrou'})
fr = pylocwolowitz_json.loc(
'welcome {name}', 'fr', {'name': 'hobbestigrou'})
assert fr == 'Bienvenue hobbestigrou'

en = pylocwolowitz.loc('welcome {name}', 'en', {'name': 'hobbestigrou'})
en = pylocwolowitz_json.loc(
'welcome {name}', 'en', {'name': 'hobbestigrou'})
assert en == 'Welcome hobbestigrou'

se = pylocwolowitz.loc('welcome {name}', 'se', {'name': 'hobbestigrou'})
se = pylocwolowitz_json.loc(
'welcome {name}', 'se', {'name': 'hobbestigrou'})
assert se == 'Välkommen hobbestigrou'


def test_no_key(pylocwolowitz):
def test_no_key(pylocwolowitz_json):
"""Test to try with a non existing key"""
assert pylocwolowitz.loc('world', 'fr') == 'world'
assert pylocwolowitz_json.loc('world', 'fr') == 'world'


def test_no_lang(pylocwolowitz):
def test_no_lang(pylocwolowitz_json):
"""Test with a no lang"""
assert pylocwolowitz.loc('hello', 'lu') == 'hello'
assert pylocwolowitz_json.loc('hello', 'lu') == 'hello'


def test_default_key(pylocwolowitz_default_key_json):
assert pylocwolowitz_default_key_json.loc('world', 'fr') == 'Valeur par défaut'
assert pylocwolowitz_default_key_json.loc('world', 'en') == 'world'
28 changes: 18 additions & 10 deletions tests/basic_yaml_test.py
Expand Up @@ -4,28 +4,36 @@
from __future__ import unicode_literals


def test_sample(pylocwolowitz):
def test_sample(pylocwolowitz_yaml):
"""Simple test no placeholders"""
assert pylocwolowitz.loc('hello', 'es') == 'Hola'
assert pylocwolowitz_yaml.loc('hello', 'es') == 'Hola'


def test_sample_token(pylocwolowitz):
def test_sample_token(pylocwolowitz_yaml):
"""Tests with placeholders"""
fr = pylocwolowitz.loc('welcome {name}', 'fr', {'name': 'hobbestigrou'})
fr = pylocwolowitz_yaml.loc(
'welcome {name}', 'fr', {'name': 'hobbestigrou'})
assert fr == 'Bienvenue hobbestigrou'

en = pylocwolowitz.loc('welcome {name}', 'en', {'name': 'hobbestigrou'})
en = pylocwolowitz_yaml.loc(
'welcome {name}', 'en', {'name': 'hobbestigrou'})
assert en == 'Welcome hobbestigrou'

se = pylocwolowitz.loc('welcome {name}', 'se', {'name': 'hobbestigrou'})
se = pylocwolowitz_yaml.loc(
'welcome {name}', 'se', {'name': 'hobbestigrou'})
assert se == 'Välkommen hobbestigrou'


def test_no_key(pylocwolowitz):
def test_no_key(pylocwolowitz_yaml):
"""Test to try with a non existing key"""
assert pylocwolowitz.loc('world', 'fr') == 'world'
assert pylocwolowitz_yaml.loc('world', 'fr') == 'world'


def test_no_lang(pylocwolowitz):
def test_no_lang(pylocwolowitz_yaml):
"""Test with a no lang"""
assert pylocwolowitz.loc('hello', 'lu') == 'hello'
assert pylocwolowitz_yaml.loc('hello', 'lu') == 'hello'


def test_default_key(pylocwolowitz_default_key):
assert pylocwolowitz_default_key.loc('world', 'fr') == 'Valeur par défaut'
assert pylocwolowitz_default_key.loc('world', 'en') == 'world'
28 changes: 23 additions & 5 deletions tests/conftest.py
Expand Up @@ -6,11 +6,29 @@
from pylocwolowitz import Pylocwolowitz


def _get_directory():
return os.getcwd() + '/i18n' if 'tests' in os.getcwd(
) else os.getcwd() + '/tests/i18n'


@pytest.fixture
def pylocwolowitz():
def pylocwolowitz_yaml():
"""To load the pylocwolowitz object"""
directory = os.getcwd() + '/i18n' if 'tests' in os.getcwd(
) else os.getcwd() + '/tests/i18n'
i18n = Pylocwolowitz(directory, 'yaml')
return Pylocwolowitz(_get_directory(), 'yaml')


@pytest.fixture
def pylocwolowitz_default_key():
"""docstring for pylocwolwitz_default_key"""
return Pylocwolowitz(_get_directory(), 'yaml', default_key='default')


@pytest.fixture
def pylocwolowitz_json():
return Pylocwolowitz(_get_directory(), 'json')


return i18n
@pytest.fixture
def pylocwolowitz_default_key_json():
"""docstring for pylocwolwitz_default_key"""
return Pylocwolowitz(_get_directory(), 'json', default_key='default')
3 changes: 3 additions & 0 deletions tests/i18n/fr_and_en.json
Expand Up @@ -2,5 +2,8 @@
"welcome {name}": {
"fr": "Bienvenue {name}",
"en": "Welcome {name}"
},
"default": {
"fr": "Valeur par défaut"
}
}
3 changes: 3 additions & 0 deletions tests/i18n/fr_and_en.yaml
Expand Up @@ -2,3 +2,6 @@
"welcome {name}":
fr: "Bienvenue {name}"
en: "Welcome {name}"

"default":
fr: "Valeur par défaut"

0 comments on commit dda3111

Please sign in to comment.