Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespaced Javascript Catalog #41

Merged
merged 1 commit into from
May 31, 2018

Conversation

afzaledx
Copy link
Contributor

@afzaledx afzaledx commented May 11, 2018

This PR adds the namespace parameter to the command line, and can also be read from the STATICI18N_NAMESPACE

The concept behind the change is that the javascript catalog generated by django put the gettext against window.gettext. If, however, we have a modular system in which there are many pluggable modules that require the catalog, it does not make sense to combine the entire catalog into a single catalog. This becomes especially problematic for future modules that will be created.

This PR addresses the issue such that the generated javascript catalog is modified so that the gettext is put against window.NAMESPACE.gettext. The module can use the gettext from its own NAMESPACE rather than use the global namespace of the project.

Reviewers

@afzaledx afzaledx force-pushed the afzaledx/adding-namespace branch 2 times, most recently from f8713b5 to 8276082 Compare May 13, 2018 19:31
@coveralls
Copy link

coveralls commented May 13, 2018

Coverage Status

Coverage increased (+0.3%) to 96.35% when pulling bfc51e8 on open-craft:afzaledx/adding-namespace into fa65ede on zyegfryed:master.

@afzaledx afzaledx force-pushed the afzaledx/adding-namespace branch from fab34a6 to 48de5cd Compare May 14, 2018 13:26
@afzaledx afzaledx changed the title (WIP) added the namespace parameter Namespaced Javascript Catalog May 14, 2018
@pomegranited pomegranited mentioned this pull request May 15, 2018
Copy link

@pomegranited pomegranited left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested this branch and it works as expected, and the added tests cover the feature. Just a few changes requested before I can approve.

Also: please bump the version in setup.py to e.g. 1.7.1 (Though note that the upstream maintainer may want a different version number).

@@ -36,6 +36,14 @@ Some commonly used options are:

Defaults to ``js``.

``-n OUTPUT_FORMAT`` or ``--namespace=SpecialBlock``

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: change OUTPUT_FORMAT to NAMESPACE

and the namespaced contexts. The final gettext will be put under
window.<namespace>.gettext
rather than the
window.gettext

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: fix word wrap


:default: ``None``

The namespace under which the static javascipt will be put.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: capitalise javascript

@@ -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'
# The namespace

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please expand this comment to explain what the namespace is used for. As it stands, the current comment adds nothing that the variable name doesn't already say.

def _get_namespaced_catalog(self, rendered_js, namespace):
template = u'''
(function(global){{
var {namespace}I18n = {{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the I18n suffix added here -- {namespace} should contain the full name of the var. As a developer, I want to be able to specify the full namespace used.

@@ -89,6 +107,7 @@ def _create_output(self, outputdir, outputformat, locale, domain, packages):

if outputformat == 'js':
data = self._create_javascript_catalog(locale, domain, packages)
data = self._get_namespaced_catalog(data, namespace) if namespace else data

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this line clearer please?

       if namespace:
            data = self._get_namespaced_catalog(data, namespace)

init: function() {{
{text}
}}
}};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make using this easier, could you please add this line right below here:

{namespace}.init()


:default: ``None``

The namespace under which the static JAVASCRIPT will be put.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry @afzaledx -- I meant title case Javascript, not all caps 😄

@afzaledx
Copy link
Contributor Author

@pomegranited, let me know if you have more suggestions.

Copy link

@pomegranited pomegranited left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@afzaledx Works well and looks good! 👍

Couple more little nits, but it's ready for upstream review once they're addressed, and your changes are squashed.

This is useful for pluggable modules which need Javascript i18n.



Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove empty lines at end of file

@@ -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'
# The namespace under which the static JAVASCRIPT will be put.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Javascript, not JAVASCRIPT

@@ -49,6 +49,25 @@ 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="value for the namespace. "

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: remove trailing space

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "Javascript identifier to use as namespace" ?

pomegranited added a commit to open-craft/xblock-drag-and-drop-v2 that referenced this pull request May 15, 2018
If a namespace is provided, the gettext is assigned to window.namespace.gettext instead of window.gettext.
@afzaledx afzaledx force-pushed the afzaledx/adding-namespace branch from f9593d2 to bfc51e8 Compare May 16, 2018 11:38
@afzaledx
Copy link
Contributor Author

@zyegfryed, This is PR is ready for your review.

@zyegfryed
Copy link
Owner

@afzaledx @pomegranited Thanks for your work! I'll have a look this week-end.

@pomegranited
Copy link

Hi @zyegfryed -- sorry to bother you, but we were wondering if you needed any changes made here? We've got time allocated this week and next if there's anything we can do?

@zyegfryed
Copy link
Owner

Hi @pomegranited , I'm sorry but I haven't have a look into the PR, yet :( Will try tomorrow evening. Cheers

@pomegranited
Copy link

No worries @zyegfryed ! Thank you for your time.

@zyegfryed zyegfryed merged commit 0574b34 into zyegfryed:master May 31, 2018
@zyegfryed
Copy link
Owner

@afzaledx @pomegranited I released v1.8.0 with your patch.
Thanks 🍰

@afzaledx
Copy link
Contributor Author

@zyegfryed, You are welcome, and thank you for incorporating our contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants