Skip to content

Commit

Permalink
Support for context in locale values
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Dec 4, 2018
1 parent 1c55c21 commit e486d85
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/appier_extras/parts/admin/models/locale.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ class Locale(base.Base):
default = True
)

context = appier.field(
index = "all"
)

data_j = appier.field(
type = dict,
private = True,
Expand All @@ -73,35 +77,36 @@ def validate(cls):
appier.not_null("locale"),
appier.not_empty("locale"),
appier.is_regex("locale", "^[a-z]{2}(?:_[a-z]{2}(?:_[a-z]+)?)?$"),
appier.not_duplicate("locale", cls._name()),

appier.not_null("data_j"),
appier.not_empty("data_j")
]

@classmethod
def list_names(cls):
return ["locale", "description", "count_l"]
return ["locale", "context", "description", "count_l"]

@classmethod
def bundles_d(cls, locale = None):
locales = cls.find_e(rules = False)
return dict([(locale.locale, locale.data_u) for locale in locales])
return dict([((locale.locale, locale.context or None), locale.data_u) for locale in locales])

@classmethod
@appier.operation(
name = "Import Bundle",
parameters = (
("JSON File", "file", "file"),
("Locale", "locale", str)
("Locale", "locale", str),
("Context", "context", str)
),
factory = True
)
def import_bundle_s(cls, file, locale, strict = False):
def import_bundle_s(cls, file, locale, context = None, strict = False):
context = context or None
data_j = cls._json_read(file)
locale_e = cls.get(locale = locale, rules = False, raise_e = False)
locale_e = cls.get(locale = locale, context = context, rules = False, raise_e = False)
if locale_e: locale_e.data_j.update(data_j)
else: locale_e = cls(locale = locale, data_j = data_j)
else: locale_e = cls(locale = locale, context = context, data_j = data_j)
locale_e.save()
return locale_e

Expand Down Expand Up @@ -137,8 +142,8 @@ def _flush(cls):

# iterates over the complete set of locale data pairs
# in the bundles dictionary to set these bundles (locales)
for locale, data_j in appier.legacy.iteritems(bundles_d):
appier.get_app()._register_bundle(data_j, locale)
for (locale, context), data_j in appier.legacy.iteritems(bundles_d):
appier.get_app()._register_bundle(data_j, locale, context = context)

@classmethod
def _escape(cls, data_j, target = ".", sequence = "::"):
Expand Down

0 comments on commit e486d85

Please sign in to comment.