toh19
toh19

Reputation: 1229

handlebars i18n helper inside each loop

I am using express handlebars to generate my view but I am having a problem which is that some of the content inside of my view is not translated properly because I am calling the I18n helper inside of an #each helper.

Index.handlebars :

{{#if roomsList}}
    {{#each roomsList}}        
        <section id="roomNumber-{{RoomNumber}}">
            <div class="room-status">              
                <div class="room-number">
                    <p>{{RoomNumber}}</p>
                </div>
                <div class="room-description room-status-{{RoomStatus}}">
                    <p class="clean">{{i18n "clean"}}</p>
                    <p class="dirty">{{i18n "dirty"}}</p>
                    <p class="in-progress">{{i18n "in-progress"}}</p>
                    <p class="to-inspect">{{i18n "to-inspect"}}</p>
                </div>
            </div>
            <span class="clearfix"></span>
        </section>        
    {{/each}}
{{/if}}

JS:

var Handlebars = require('Handlebars');
var i18n = require('i18n');

module.exports = {
    //Setup our default layout
    defaultLayout: 'default',
 
    //Register handlebars helpers
    helpers: {
    //Register your helpers
        //Helper for multiple languages
        i18n: function () {
            return i18n.__.apply(this, arguments);
        },
        __n: function () {
            return i18n.__n.apply(this, arguments);
        },
        section: function (name, options) {
            if (!this._sections) this._sections = {};
            this._sections[name] = options.fn(this);
            return null;
        }, 
        breaklines: function(text) {
            text = Handlebars.Utils.escapeExpression(text);
            text = text.replace(/(\r\n|\n|\r)/gm, '<br>');
            return new Handlebars.SafeString(text);
        }
    }     
}

How am I to resolve the issue?

Upvotes: 4

Views: 1060

Answers (2)

Laode Muhammad Al Fatih
Laode Muhammad Al Fatih

Reputation: 4610

Easier and easier-to-read solutions are:

{{../i18n "parameter"}}

The solution above also works for dynamic variables

Upvotes: 1

Vladyslav Siroshtan
Vladyslav Siroshtan

Reputation: 185

It works for me:

{{#with ..}}{{i18n "to-inspect"}}{{/with}}

This moves the context upward

Upvotes: 2

Related Questions