Skip to content

Commit

Permalink
better support for localization process (like appier)
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Dec 1, 2015
1 parent a7ab0c1 commit 90d224f
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/quorum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
ensure_user, ensure_session, ensure, ensure_auth
from .base import APP, RUN_CALLED, RUN_F, Quorum, monkey, call_run, run, prepare_app,\
run_base, run_waitress, run_netius, load, unload, load_all, load_app_config,\
load_paths, load_bundles, start_log, extra_logging, get_adapter, get_log, get_level,\
get_handlers, get_handler, get_bundle, is_devel, finalize, before_request,\
load_paths, load_bundles, start_log, extra_logging, get_app, get_adapter, get_log,\
get_level, get_handlers, get_handler, get_bundle, is_devel, finalize, before_request,\
after_request, context_processor, start_execution, stop_execution, setup_models,\
models_c, resolve, templates_path, bundles_path, base_path, onrun
from .config import conf, conf_prefix, confs
Expand Down
17 changes: 17 additions & 0 deletions src/quorum/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ def load(
app.jinja_options = dict(
finalize = finalize
)
app.bundles = dict()
app._locale_d = locales[0]

# sets a series of conditional based attributes in both
# the associated modules and the base app object (as expected)
Expand Down Expand Up @@ -630,6 +632,9 @@ def extra_logging(logger, level, formatter):
handler.setFormatter(formatter)
logger.addHandler(handler)

def get_app(app = None):
return app or APP

def get_adapter(app = None):
return APP and APP.adapter

Expand All @@ -655,6 +660,9 @@ def get_handler(name, app = None):

def get_bundle(name, app = None):
app = app or APP
bundle = app.bundles.get(name, None)
if bundle: return bundle
name = _best_locale(name)
return app.bundles.get(name, None)

def is_devel(app = None):
Expand Down Expand Up @@ -797,6 +805,15 @@ def _level(level):
return logging._checkLevel(level)
return logging.getLevelName(level)

def _best_locale(locale, app = None):
if not locale: return locale
app = app or APP
for _locale in app.locales:
is_valid = _locale.startswith(locale)
if not is_valid: continue
return _locale
return locale

# runs the monkey patching of the flask module so that it
# may be used according to the quorum specification, this
# is used in order to avoid minimal effort in the conversion
Expand Down
7 changes: 3 additions & 4 deletions src/quorum/test/amazon.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

class AmazonTest(quorum.TestCase):

@quorum.secured
def setUp(self):
try:
quorum.load(
Expand All @@ -51,10 +50,10 @@ def setUp(self):
except:
self.skip()

@quorum.secured
def tearDown(self):
quorum.clear_amazon_bucket()
quorum.unload()
try: quorum.clear_amazon_bucket()
except: pass
finally: quorum.unload()

@quorum.secured
def test_connect(self):
Expand Down
89 changes: 89 additions & 0 deletions src/quorum/test/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Flask Quorum
# Copyright (c) 2008-2015 Hive Solutions Lda.
#
# This file is part of Hive Flask Quorum.
#
# Hive Flask Quorum is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Flask Quorum is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Flask Quorum. If not, see <http://www.apache.org/licenses/>.

__author__ = "João Magalhães <joamag@hive.pt>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2015 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import quorum

class BaseTest(quorum.TestCase):

def setUp(self):
try:
quorum.load(name = __name__)
except:
self.skip()

def tearDown(self):
quorum.unload()

@quorum.secured
def test_locale(self):
app = quorum.get_app()
app.locales = ("en_us", "pt_pt", "es_es")
app.bundles["en_us"] = dict(hello = "Hello")
app.bundles["pt_pt"] = dict(hello = "Olá")

result = quorum.to_locale("hello", locale = "en_us")
self.assertEqual(result, "Hello")

result = quorum.to_locale("hello", locale = "en-us")
self.assertEqual(result, "Hello")

result = quorum.to_locale("hello", locale = "pt_pt")
self.assertEqual(result, "Olá")

result = quorum.to_locale("hello", locale = "pt-pt")
self.assertNotEqual(result, "Olá")
self.assertEqual(result, "Hello")

result = quorum.to_locale("hello", locale = "es_es")
self.assertNotEqual(result, "Hola")
self.assertEqual(result, "Hello")

app.bundles["es_es"] = dict(hello = "Hola")

result = quorum.to_locale("hello", locale = "es_es")
self.assertEqual(result, "Hola")

result = quorum.to_locale("hello", locale = "en")
self.assertEqual(result, "Hello")

result = quorum.to_locale("hello", locale = "pt")
self.assertEqual(result, "Olá")

result = quorum.to_locale("hello", locale = "es")
self.assertEqual(result, "Hola")
3 changes: 1 addition & 2 deletions src/quorum/test/mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,16 @@

class MailTest(quorum.TestCase):

@quorum.secured
def setUp(self):
try: quorum.load(
name = __name__
)
except: self.skip()

@quorum.secured
def tearDown(self):
quorum.unload()

@quorum.secured
def test_format(self):
result = quorum.mail._format("João Magalhães <joamag@hive.pt>")
self.assertEqual(type(result), str)
Expand Down
12 changes: 7 additions & 5 deletions src/quorum/test/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

class ModelTest(quorum.TestCase):

@quorum.secured
def setUp(self):
try:
quorum.load(
Expand All @@ -54,11 +53,12 @@ def setUp(self):
except:
self.skip()

@quorum.secured
def tearDown(self):
adapter = quorum.get_adapter()
adapter.drop_db()
quorum.unload()
try:
adapter = quorum.get_adapter()
adapter.drop_db()
except: pass
finally: quorum.unload()

@quorum.secured
def test_basic(self):
Expand Down Expand Up @@ -118,6 +118,7 @@ def test_count(self):
result = mock.Person.count()
self.assertEqual(result, 1)

@quorum.secured
def test_validation(self):
person = mock.Person()

Expand All @@ -132,6 +133,7 @@ def test_validation(self):

self.assertRaises(quorum.ValidationError, person.save)

@quorum.secured
def test_map(self):
person = mock.Person()
person.name = "Name"
Expand Down
27 changes: 22 additions & 5 deletions src/quorum/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,28 +637,45 @@ def generate_identifier(size = 16, chars = string.ascii_uppercase + string.digit

return "".join(random.choice(chars) for _index in range(size))

def to_locale(value):
def to_locale(value, locale = None, fallback = True):
"""
Utility function used to localize the provided value according
to the currently loaded set of bundles, the bundles are loaded
at the application start time from the proper sources.
The (target) locale value for the translation may be provided or
in case it's not the locale associated with the current request
is used as an alternative.
In case the value is not localizable (no valid bundle available)
it is returned as it is without change.
:type value: String
:param value: The value that is going to be localized according
to the current application environment, this may be a normal
english dictionary string or a variable reference.
:type locale: String
:param locale: The (target) locale value to be used in the
translation process for the provided string value.
:type fallback: bool
:param fallback: If a fallback operation should be performed in
case no value was retrieved from the base/request locale.
:rtype: String
:return: The localized value for the current environment or the
proper (original) value in case no localization was possible.
"""

locale = flask.request.locale
bundle = common.base().get_bundle(locale)
if not bundle: return value
return bundle.get(value, value)
locale = locale or flask.request.locale
bundle = common.base().get_bundle(locale) or {}
result = bundle.get(value, None)
if not result == None: return result
app = common.base().APP
if fallback: return to_locale(
value,
locale = app._locale_d,
fallback = False
)
return value

def nl_to_br(value):
"""
Expand Down

0 comments on commit 90d224f

Please sign in to comment.