4

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?

76484
  • 8,498
  • 3
  • 19
  • 30
toh19
  • 1,083
  • 10
  • 21

2 Answers2

2

It works for me:

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

This moves the context upward

1

Easier and easier-to-read solutions are:

{{../i18n "parameter"}}

The solution above also works for dynamic variables

Laode Muhammad Al Fatih
  • 3,994
  • 1
  • 18
  • 32