Skip to content

Commit

Permalink
Added the namespace parameter. (#41)
Browse files Browse the repository at this point in the history
If a namespace is provided, the gettext is assigned to window.namespace.gettext instead of window.gettext. Thanks @afzaledx  and @pomegranited
  • Loading branch information
afzaledx authored and Sébastien Fievet committed May 31, 2018
1 parent 0b0a485 commit 0574b34
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ Some commonly used options are:

Defaults to ``js``.

``-n NAMESPACE`` or ``--namespace=NAMESPACE``
The final gettext will be put with
window.SpecialBlock.gettext rather than the
window.gettext. This is useful for pluggable
modules which need Javascript i18n.

Defaults to ``None``.

For a full list of options, refer to the ``compilejsi18n`` management command
help by running::

Expand Down
9 changes: 9 additions & 0 deletions docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ Settings
Use the legacy function ``statici18n.utils.legacy_filename`` to
generate a filename with the language code derived from the
``django.utils.translation.trans_real import to_language``.

.. attribute:: STATICI18N_NAMESPACE

:default: ``None``

Javascript identifier to use as namespace. This is useful when we want to
have separate translations for the global and the namespaced contexts.
The final gettext will be put under `window.<namespace>.gettext` rather
than the `window.gettext`. Useful for pluggable modules that need JS i18n.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="django-statici18n",
version="1.7.0",
version="1.7.1",
author="Sebastien Fievet",
author_email="zyegfryed@gmail.com",
url="http://django-statici18n.readthedocs.org/",
Expand Down
2 changes: 2 additions & 0 deletions src/statici18n/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ class StaticFilesConf(AppConf):
OUTPUT_DIR = 'jsi18n'
# The dotted path to the function that creates the filename
FILENAME_FUNCTION = 'statici18n.utils.default_filename'
# Javascript identifier to use as namespace.
NAMESPACE = None
25 changes: 23 additions & 2 deletions src/statici18n/management/commands/compilejsi18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,24 @@ def add_arguments(self, parser):
default='js',
help="Format of the output catalog. "
"Options are: js, json. Defaults to js.")
parser.add_argument('-n', '--namespace', dest='namespace',
metavar='NAMESPACE', default=settings.STATICI18N_NAMESPACE,
help="Javascript identifier to use as namespace.")

def _get_namespaced_catalog(self, rendered_js, namespace):
template = u'''
(function(global){{
var {namespace} = {{
init: function() {{
{text}
}}
}};
{namespace}.init();
global.{namespace} = {namespace};
}}(this));
'''
namespaced_catalog = template.format(text=rendered_js, namespace=namespace)
return namespaced_catalog

def _create_javascript_catalog(self, locale, domain, packages):
activate(locale)
Expand Down Expand Up @@ -80,7 +98,7 @@ def _create_json_catalog(self, locale, domain, packages):
response = catalog.get(self, None, domain=domain, packages=packages)
return force_text(response.content)

def _create_output(self, outputdir, outputformat, locale, domain, packages):
def _create_output(self, outputdir, outputformat, locale, domain, packages, namespace):
outputfile = os.path.join(outputdir, get_filename(locale, domain,
outputformat))
basedir = os.path.dirname(outputfile)
Expand All @@ -89,6 +107,8 @@ def _create_output(self, outputdir, outputformat, locale, domain, packages):

if outputformat == 'js':
data = self._create_javascript_catalog(locale, domain, packages)
if namespace:
data = self._get_namespaced_catalog(data, namespace)
elif outputformat == 'json':
data = self._create_json_catalog(locale, domain, packages)
else:
Expand All @@ -103,6 +123,7 @@ def handle(self, **options):
packages = options['packages'] or settings.STATICI18N_PACKAGES
outputdir = options['outputdir']
outputformat = options['outputformat']
namespace = options['namespace']
verbosity = int(options.get('verbosity'))

if locale is not None:
Expand All @@ -123,4 +144,4 @@ def handle(self, **options):
if verbosity > 0:
self.stdout.write("processing language %s\n" % locale)

self._create_output(outputdir, outputformat, locale, domain, packages)
self._create_output(outputdir, outputformat, locale, domain, packages, namespace)
16 changes: 16 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ def test_compile_with_output_format(settings, locale, output_format):
settings.STATIC_ROOT, "jsi18n", locale, "djangojs.%s" % output_format))


@pytest.mark.parametrize('locale', ['en'])
@pytest.mark.parametrize('namespace', ['MyBlock'])
def test_compile_with_namespace(settings, locale, namespace):
out = six.StringIO()
management.call_command('compilejsi18n', verbosity=1, stdout=out,
locale=locale, outputformat='js', namespace=namespace)
out.seek(0)
lines = [l.strip() for l in out.readlines()]
assert len(lines) == 1
assert lines[0] == "processing language %s" % to_locale(locale)
file_path = os.path.join(settings.STATIC_ROOT, "jsi18n", locale, "djangojs.js")
assert os.path.exists(file_path)
generated_content = open(file_path).read()
assert 'global.MyBlock = MyBlock;' in generated_content


@pytest.mark.usefixtures("cleandir")
def test_compile_locale_not_exists():
out = six.StringIO()
Expand Down

0 comments on commit 0574b34

Please sign in to comment.