Saturday 18 March 2017

How can I change the name of i18n function?

I have a simple express app which uses i18n for localization:

const express = require('express');
const i18n = require('i18n');
const path = require('path');

const app = express();

i18n.configure({
    directory: path.join(__dirname, 'locales'),
    locales: ['en', 'de', 'it'],
    defaultLocale: 'en'
});

app.get('/:language?', (req, res) => {
    if (req.params.language) {
        i18n.setLocale(req.params.language);
    }

    res.send(i18n.__('Thank you for your support'));
});

and now I would like to change the way how I call the translation, so I don't have to type i18n every time. I thought I would do something like this:

const __ = i18n.__

and then call it like this:

__("Thank you for your support")

However, that doesn't work. Could you please explain why and what is the solution for that?

Thank you in advance.



via Gogy

No comments:

Post a Comment