changeset 1427:466a277c72a3

implement i18n for .js by using jinja2 js templates /+template/<filename> renders the template <filename> and serves the resulting content with the mimetype guessed for filename. removed some hacks used for i18n purposes from common.js common.js needs to get moved to templates/ directory, will be done in next changeset to make review more easy. BTW, this form is needed in the .js templates because i18ned strings are not extracted from inside of '...' : {{ "buttons.attr('title', '%s');" % _("Hide comments") }}
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 15 Jul 2012 23:23:18 +0200
parents cf62198c81f7
children a8d0a52bb45d
files MoinMoin/apps/frontend/views.py MoinMoin/config/default.py MoinMoin/static/js/common.js MoinMoin/templates/base.html setup.py
diffstat 5 files changed, 29 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Sun Jul 15 17:51:57 2012 +0200
+++ b/MoinMoin/apps/frontend/views.py	Sun Jul 15 23:23:18 2012 +0200
@@ -18,6 +18,7 @@
 import re
 import difflib
 import time
+import mimetypes
 from datetime import datetime
 from itertools import chain
 from collections import namedtuple
@@ -28,7 +29,7 @@
 except ImportError:
     import simplejson as json
 
-from flask import request, url_for, flash, Response, redirect, session, abort, jsonify
+from flask import request, url_for, flash, Response, make_response, redirect, session, abort, jsonify
 from flask import current_app as app
 from flask import g as flaskg
 from flaskext.babel import format_date
@@ -1870,6 +1871,18 @@
                            item_name=tag,
                            item_names=item_names)
 
+
+@frontend.route('/+template/<path:filename>')
+def template(filename):
+    """
+    serve a rendered template from <filename>
+    """
+    content = render_template(filename)
+    ct, enc = mimetypes.guess_type(filename)
+    response = make_response((content, 200, {'content-type': ct or 'text/plain;charset=utf-8'}))
+    return response
+
+
 @frontend.errorhandler(404)
 def page_not_found(e):
     return render_template('404.html',
--- a/MoinMoin/config/default.py	Sun Jul 15 17:51:57 2012 +0200
+++ b/MoinMoin/config/default.py	Sun Jul 15 23:23:18 2012 +0200
@@ -365,9 +365,8 @@
         ('frontend.modify_item', L_('Modify'), L_('Edit or Upload'), True, ),
         ('special.supplementation', None, None, False, ),
         ('frontend.index', L_('Index'), L_('List sub-items'), False, ),
-        # The | character in the comments and transclusions lines below separate the off/on title (tooltip) values used by javascript
-        ('special.comments', L_('Comments'), L_('Show comments|Hide comments'), True, ),
-        ('special.transclusions', L_('Transclusions'), L_('Show transclusions|Hide transclusions'), True, ),
+        ('special.comments', L_('Comments'), L_('Hide comments'), True, ),
+        ('special.transclusions', L_('Transclusions'), L_('Show transclusions'), True, ),
         ('frontend.highlight_item', L_('Highlight'), L_('Show with Syntax-Highlighting'), True, ),
         ('frontend.show_item_meta', L_('Meta'), L_('Display Metadata'), True, ),
         ('frontend.quicklink_item', None, L_('Create or remove a navigation link to this item'), False, ),
--- a/MoinMoin/static/js/common.js	Sun Jul 15 17:51:57 2012 +0200
+++ b/MoinMoin/static/js/common.js	Sun Jul 15 23:23:18 2012 +0200
@@ -60,19 +60,17 @@
 
 
 // toggleComments is executed when user clicks a Comments button and conditionally on dom ready.
-var pageComments = null, // will hold list of elements with class "comment"
-    commentsShowTitle = '', // "Show comments"
-    commentsHideTitle = ''; // "Hide comments"
+var pageComments = null; // will hold list of elements with class "comment"
 function toggleComments() {
     "use strict";
     // Toggle visibility of every tag with class "comment"
     var buttons = $('.moin-toggle-comments-button > a');
     if (pageComments.is(':hidden')) {
         pageComments.show();
-        buttons.attr('title', commentsHideTitle);
+        {{ "buttons.attr('title', '%s');" % _("Hide comments") }}
     } else {
         pageComments.hide();
-        buttons.attr('title', commentsShowTitle);
+        {{ "buttons.attr('title', '%s');" % _("Show comments") }}
     }
 }
 
@@ -84,13 +82,6 @@
     if (pageComments.length > 0) {
         // There are comments, so show itemview Comments button
         $('.moin-toggle-comments-button').css('display', '');
-        // read translated show|hide Comments button title, split into show and hide parts, and save
-        titles = $('.moin-toggle-comments-button > a').attr('title').split('|');
-        if (titles.length === 2) {
-            commentsShowTitle = titles[0];
-            commentsHideTitle = titles[1];
-            $('.moin-toggle-comments-button > a').attr('title', commentsHideTitle);
-        }
         // comments are visible; per user option, hide comments if there is not a <br id="moin-show-comments" />
         if (!document.getElementById('moin-show-comments')) {
             toggleComments();
@@ -102,8 +93,6 @@
 
 
 // toggleTransclusionOverlays is executed when user clicks a Transclusions button on the Show item page.
-var transclusionShowTitle = '', // "Show Transclusions"
-    transclusionHideTitle = ''; // "Hide Transclusions"
 function toggleTransclusionOverlays() {
     "use strict";
     var overlays = $('.moin-item-overlay-ul, .moin-item-overlay-lr'),
@@ -112,10 +101,10 @@
         buttons = $('.moin-transclusions-button > a');
         if (overlays.is(':visible')) {
             overlays.hide();
-            buttons.attr('title', transclusionShowTitle);
+            {{ "buttons.attr('title', '%s');" % _("Show transclusions") }}
         } else {
             overlays.show();
-            buttons.attr('title', transclusionHideTitle);
+            {{ "buttons.attr('title', '%s');" % _("Hide transclusions") }}
         }
     }
 }
@@ -156,13 +145,6 @@
     wrappers = $('.moin-item-wrapper');
     if (wrappers.length > 0) {
         $('.moin-transclusions-button').css('display', '');
-        // read translated show|hide Transclusions button title, split into show and hide parts, and save
-        titles = $('.moin-transclusions-button > a').attr('title').split('|');
-        if (titles.length === 2) {
-            transclusionShowTitle = titles[0];
-            transclusionHideTitle = titles[1];
-            $('.moin-transclusions-button > a').attr('title', transclusionShowTitle);
-        }
     }
 }
 $(document).ready(initTransclusionOverlays);
@@ -283,10 +265,14 @@
 function transcludeSubitem(subitem_name, fullname) {
     "use strict";
     function moinwiki(subitem_name, fullname) {
-        return "{{" + fullname.replace("{{", "\\}}") + "}} ";
+        // note: keep the many plusses, to avoid jinja2 templating kicking in
+        // when seeing two curly opening / closing braces
+        return "{"+"{" + fullname.replace("{"+"{", "\\"+"}"+"}") + "}"+"} ";
     }
     function mediawiki(subitem_name, fullname) {
-        return "{{:" + fullname.replace("}}", "\\}}") + "}} ";
+        // note: keep the many plusses, to avoid jinja2 templating kicking in
+        // when seeing two curly opening / closing braces
+        return "{"+"{:" + fullname.replace("}"+"}", "\\"+"}"+"}") + "}"+"} ";
     }
     function rst(subitem_name, fullname) {
         return "\n.. include:: " + subitem_name + "\n";
--- a/MoinMoin/templates/base.html	Sun Jul 15 17:51:57 2012 +0200
+++ b/MoinMoin/templates/base.html	Sun Jul 15 23:23:18 2012 +0200
@@ -48,7 +48,7 @@
     {% block head_scripts %}
     <script src="{{ url_for('serve.files', name='jquery', filename='jquery.min.js') }}"></script>
     <script src="{{ url_for('serve.files', name='svgweb', filename='svg.js') }}"></script>
-    <script src="{{ url_for('static', filename='js/common.js') }}"></script>
+    <script src="{{ url_for('frontend.template', filename='common.js') }}"></script>
     {{ scripts }}
     <!--[if lt IE 9]>
         {# TODO: use a local copy later #}
--- a/setup.py	Sun Jul 15 17:51:57 2012 +0200
+++ b/setup.py	Sun Jul 15 23:23:18 2012 +0200
@@ -128,6 +128,7 @@
             ('MoinMoin/templates/**.html', 'jinja2', None),
             ('MoinMoin/apps/**/templates/**.html', 'jinja2', None),
             ('MoinMoin/**/_tests/**', 'ignore', None),
+            ('MoinMoin/static/**.js', 'javascript', None),
             ('MoinMoin/static/**', 'ignore', None),
             ('MoinMoin/**.py', 'python', None),
         ],