phenric
phenric

Reputation: 317

i18n how to get the cookie information about the language

I use i18n for the translations with Express.

To select the language, I use this

    <select class="selectpicker" data-width="fit" onchange="javascript:langSelect(this)">
                <option value="de" data-content='<span class="flag-icon flag-icon-de"></span> Deutsch'>Deutsch</option>
                <option value="en" data-content='<span class="flag-icon flag-icon-us"></span> English'>English</option>
                <option value="es" data-content='<span class="flag-icon flag-icon-es"></span> Español'>Español</option>
                <option value="fr" data-content='<span class="flag-icon flag-icon-fr"></span> Français'>Français</option>
                <option value="it" data-content='<span class="flag-icon flag-icon-it"></span> Italiano'>Italiano</option>
                <option value="nl" data-content='<span class="flag-icon flag-icon-nl"></span> Nederlands'>Nederlands</option>
     </select>

With this

<script type="text/javascript">
function langSelect(lgu)
{
    window.location = "/setlocale/"+lgu.value;

}
</script>

The setup looks like this for i18n

i18n.configure({
    locales: ['de', 'en', 'es', 'fr', 'it', 'nl'],
    directory: path.join(__dirname, '..', '/locales'),
    defaultLocale: 'en',
    cookie: 'lang',
    objectNotation: true,
});

module.exports = function(req, res, next) {
    i18n.init(req, res);
    var current_local = i18n.getLocale();
    return next();
};

I try to show on the selector the language selected even if I reload the page.

How can I do it?

How can I access to the value in the cookie?

I tried req.query.current_local without any success.

Many thx

Upvotes: 2

Views: 5399

Answers (1)

Ayush Mittal
Ayush Mittal

Reputation: 559

When you are calling the 'setlocale' url in that you can use:

 res.cookie("lang", req.params.lang);

and you can get it as:

req.cookies.lang

Upvotes: 2

Related Questions