0

can a wizard out there let me know if it is possible to call a helper inside another helper. i need to call an i18n helper inside a date format helper. i cannot seem to get it to work. was wondering if that is even possible with this plugin.

here is my setup:

const express = require('express');
const i18next = require('i18next');

i18next.init(options);

app.use(i18nextMiddleware.handle(i18next));
app.post(path.join(__dirname,'/locales/add/{{ns}}/{{lng}}'), i18nextMiddleware.missingKeyHandler(i18next));
app.get(path.join(__dirname,'/locales/resources.json'), i18nextMiddleware.getResourcesHandler(i18next));

// view engine setup
app.set('views', path.join(__dirname, 'views'));

app.engine('hbs', exphbs({
    extname: 'hbs',
    defaultLayout: 'main_layout',
    layoutDir: path.join(__dirname, 'views/layouts/'),
    partialsDir: path.join(__dirname,'views/partials/'),
    helpers: {
        i18n: function(key, options){
            var result = this.t(key, options.hash);
            return new Handlebars.SafeString(result);
        },
        formatExpire: function (namespace, date, format, lang) {
        // strText = "expired On";
        // strText = Handlebars.i18n.t('namespace:translationString'); //  Cannot read property 't' of undefined
        // strText = Handlebars.i18n('namespace:translationString'); // Handlebars.i18n is not a function
           strText = Handlebars._default_helpers['i18n'].t('namespace:translationString'); // t is not a function

        htmlDisplay = "<span class='bg-danger-800 text-highlight'>" + strText + "<span class='text-semibold'></span></span>";

        return htmlDisplay;
        }
        ...
        ....
    }
    })
  )

app.set('view engine', 'hbs');

anything else works great, all of the helpers i have no issues with. only when i call the "formatExpire" that i get the errors listed as comments in the helper function.

Any help would be greatly appreciated.

thanks

boulepick
  • 69
  • 2
  • 10

1 Answers1

0

Just Handlebars.helpers.i18n('namespace:translationString') should be fine.

pietrovismara
  • 6,102
  • 5
  • 33
  • 45
  • does not work i get :TypeError: Handlebars.helpers.i18n is not a function when i do a console log(Handelbars.helpers) i get : { blockHelperMissing: [Function], each: [Function], helperMissing: [Function], if: [Function], unless: [Function], log: [Function], lookup: [Function], with: [Function] } it is only displaying the built in handlebars helpers, none of my cutom helpers is listed. – boulepick Oct 19 '17 at 21:30