| @@ -199,44 +199,6 @@ will tell mustache.js to look for a object in the context's property | |||||
| `winnings`. It will then use that object as the context for the template found | `winnings`. It will then use that object as the context for the template found | ||||
| in `partials` for `winnings`. | in `partials` for `winnings`. | ||||
| ## Internationalization | |||||
| mustache.js supports i18n using the `{{_i}}{{/i}}` tags. When mustache.js encounters | |||||
| an internationalized section, it will call out to the standard global gettext function `_()` with the tag contents for a | |||||
| translation _before_ any rendering is done. For example: | |||||
| var template = "{{_i}}{{name}} is using mustache.js!{{/i}}" | |||||
| var view = { | |||||
| name: "Matt" | |||||
| }; | |||||
| var translationTable = { | |||||
| // Welsh, according to Google Translate | |||||
| "{{name}} is using mustache.js!": "Mae {{name}} yn defnyddio mustache.js!" | |||||
| }; | |||||
| function _(text) { | |||||
| return translationTable[text] || text; | |||||
| } | |||||
| alert(Mustache.to_html(template, view)); | |||||
| // alerts "Mae Matt yn defnyddio mustache.js!" | |||||
| ### The TRANSLATION-HINT Pragma | |||||
| Some single words in English have different translations based on usage context. Mustache.js supports this with the TRANSLATION-HINT pragma. For example, the word "Tweet" can be used as a noun, or a verb. The following template is ambiguous: | |||||
| <div class="tweet-button">{{_i}}Tweet{{/i}}</div> | |||||
| By adding a pragma, we can provide the right context for a given template: | |||||
| {{%TRANSLATION-HINT mode=tweet_button}} | |||||
| <div class="tweet-button">{{_i}}Tweet{{/i}}</div> | |||||
| This will lookup every translation in that template with the mode, e.g. `_('Tweet', {mode: "tweet_button"})`, which your gettext implementation can handle as appropriate. | |||||
| ## Escaping | ## Escaping | ||||
| mustache.js does escape all values when using the standard double mustache | mustache.js does escape all values when using the standard double mustache | ||||
| @@ -293,10 +255,6 @@ own iteration marker: | |||||
| {{bob}} | {{bob}} | ||||
| {{/foo}} | {{/foo}} | ||||
| ### TRANSLATION-HINT | |||||
| See the "Internationalization" section above for info on this pragma. | |||||
| ## F.A.Q. | ## F.A.Q. | ||||
| ### Why doesn’t Mustache allow dot notation like `{{variable.member}}`? | ### Why doesn’t Mustache allow dot notation like `{{variable.member}}`? | ||||
| @@ -1 +0,0 @@ | |||||
| {{_i}}foo {{bar}}{{/i}} | |||||
| @@ -1,4 +0,0 @@ | |||||
| var i18n = { | |||||
| bar: "don't double render me... {{baz}}", | |||||
| baz: "FAIL" | |||||
| }; | |||||
| @@ -1 +0,0 @@ | |||||
| foo don't double render me... {{baz}} | |||||
| @@ -13,8 +13,7 @@ var Mustache = function() { | |||||
| pragmas: {}, | pragmas: {}, | ||||
| buffer: [], | buffer: [], | ||||
| pragmas_implemented: { | pragmas_implemented: { | ||||
| "IMPLICIT-ITERATOR": true, | |||||
| "TRANSLATION-HINT": true | |||||
| "IMPLICIT-ITERATOR": true | |||||
| }, | }, | ||||
| context: {}, | context: {}, | ||||
| @@ -35,17 +34,9 @@ var Mustache = function() { | |||||
| } | } | ||||
| } | } | ||||
| // Branching or moving down the partial stack, save any translation mode info. | |||||
| if (this.pragmas['TRANSLATION-HINT']) { | |||||
| context['_TRANSLATION-HINT_mode'] = this.pragmas['TRANSLATION-HINT'].mode; | |||||
| } | |||||
| // get the pragmas together | // get the pragmas together | ||||
| template = this.render_pragmas(template); | template = this.render_pragmas(template); | ||||
| // handle all translations | |||||
| template = this.render_i18n(template, context, partials); | |||||
| // render the template | // render the template | ||||
| var html = this.render_section(template, context, partials); | var html = this.render_section(template, context, partials); | ||||
| @@ -121,37 +112,6 @@ var Mustache = function() { | |||||
| return this.render(partials[name], context[name], partials, true); | return this.render(partials[name], context[name], partials, true); | ||||
| }, | }, | ||||
| render_i18n: function(html, context, partials) { | |||||
| if (html.indexOf(this.otag + "_i") == -1) { | |||||
| return html; | |||||
| } | |||||
| var that = this; | |||||
| var regex = new RegExp(this.otag + "\\_i" + this.ctag + | |||||
| "\\s*([\\s\\S]+?)" + this.otag + "\\/i" + this.ctag, "mg"); | |||||
| // for each {{_i}}{{/i}} section do... | |||||
| return html.replace(regex, function(match, content) { | |||||
| var translationMode; | |||||
| if (that.pragmas && that.pragmas["TRANSLATION-HINT"] && that.pragmas["TRANSLATION-HINT"].mode) { | |||||
| translationMode = that.pragmas["TRANSLATION-HINT"].mode; | |||||
| } else if (context['_TRANSLATION-HINT_mode']) { | |||||
| translationMode = context['_TRANSLATION-HINT_mode']; | |||||
| } | |||||
| var params = content; | |||||
| if (translationMode) { | |||||
| params = { | |||||
| text: content, | |||||
| mode: translationMode | |||||
| }; | |||||
| } | |||||
| return _(params); | |||||
| }); | |||||
| }, | |||||
| /* | /* | ||||
| Renders inverted (^) and normal (#) sections | Renders inverted (^) and normal (#) sections | ||||
| */ | */ | ||||