Skip to content

Commit

Permalink
added possibility to specify custom locales
Browse files Browse the repository at this point in the history
  • Loading branch information
alchrabas committed Dec 5, 2015
1 parent 3080605 commit bba117b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
8 changes: 8 additions & 0 deletions pyslate/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ def load(self, tag_name, language):
if (tag_name, language) in self.cache:
return self.cache[(tag_name, language)]
return None

def remove(self, tag_name):
for tag_language_tuple in self.cache:
if tag_language_tuple[0] == tag_name:
self.cache.pop(tag_language_tuple, None)

def clear(self):
self.cache.clear()
7 changes: 4 additions & 3 deletions pyslate/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ def __init__(self):
When ALLOW_CACHE is True, then the cache needs to be specified as a keyword argument in Pyslate constructor.
You can see the API of the :obj:`SimpleMemoryCache <pyslate.pyslate.cache.SimpleMemoryCache>` to create your own implementation.
.. NOTE::
If you supply a cache this way, then the class must have a default (parameter-less) constructor.
Default: ``True``
"""
Expand Down Expand Up @@ -151,6 +148,8 @@ def __init__(self):
when variable requested in the tag value is missing.
The only argument is name of the missing variable.
Should return string displayed instead of the missing variable.
Default: lambda name: "[MISSING VALUE FOR '{0}']".format(name)
"""

self.ON_MISSING_TAG_KEY = lambda name, params: "[MISSING TAG '{0}']".format(name)
Expand All @@ -161,4 +160,6 @@ def __init__(self):
available for interpolation into this tag's value.
It should return string displayed instead of the missing tag value.
For example you can print keys of params dict to see what parameters are available.
Default: lambda name, params: "[MISSING TAG '{0}']".format(name)
"""
20 changes: 17 additions & 3 deletions pyslate/pyslate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import datetime
import numbers

Expand All @@ -13,7 +14,7 @@ class Pyslate(object):
"""

def __init__(self, language, backend=None, config=DefaultConfig(), context=None,
cache=None, parser=None, on_missing_tag_key_callback=None):
cache=None, locales=None, parser=None, on_missing_tag_key_callback=None):
"""
Constructor
Expand All @@ -22,6 +23,7 @@ def __init__(self, language, backend=None, config=DefaultConfig(), context=None,
:param config: see config field
:param context: see context field
:param cache: see cache field
:param locales: this dict extends default dict of locales available in :obj:`locales.LOCALES <pyslate.pyslate.locales.LOCALES>`
:param parser: see parser field
:param on_missing_tag_key_callback: see on_missing_tag_key_callback field
:return: object of Pyslate class
Expand Down Expand Up @@ -52,6 +54,18 @@ def __init__(self, language, backend=None, config=DefaultConfig(), context=None,
Even if specified, cache may not be used when ``config.ALLOW_CACHE`` is False.
"""

self.locales = copy.deepcopy(LOCALES)
"""
Dict containing information about locales available in the application.
It stores information like native language name, date and time format, decimal separator and
rules for plural forms available in this language.
Keyword argument `locales` does extend, not replace the default set of locales.
Locales specified in keyword argument takes higher precedence over default locales.
For examples of correct locale specification see :obj:`pyslate.locales.LOCALES`
"""
if locales:
self.locales = dict(self.locales, **locales)

self.fallbacks = {}
"""
Dict containing language fallbacks. e.g. dict ``{"pl": "en"}`` means
Expand Down Expand Up @@ -146,7 +160,7 @@ def _translate(self, tag_name, **kwargs):
kwargs = dict(self.context, **kwargs) # add context variables, which have lower priority

if "number" in kwargs:
number_variant = self._first_left_value_from(LOCALES, self._get_languages())["number_rule"](
number_variant = self._first_left_value_from(self.locales, self._get_languages())["number_rule"](
kwargs["number"])

base_variant_parts = tag_name.partition("#")
Expand Down Expand Up @@ -205,7 +219,7 @@ def localize(self, value, short=False):
:param value: value to be localized
:return: string representation of the value, localized if being instance of the supported types
"""
locale_data = self._first_left_value_from(LOCALES, self._get_languages())
locale_data = self._first_left_value_from(self.locales, self._get_languages())
if not self.config.LOCALE_FORMAT_NUMBERS:
return str(value)
if isinstance(value, float):
Expand Down

0 comments on commit bba117b

Please sign in to comment.