question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to change locale through the language picker

See original GitHub issue

This is my configuration but locale doesn’t change, if I change defalutLocale in initial configuration webpage is translated. But if I pick different language from the dropdown nothing changes

i18n.configure({
  locales: ['eng', 'rus'],
  defaultLocale: 'rus',
  directory: __dirname + '/static/locales',
  cookiename: 'locale'
});

app.use(function (req, res, next) {
    res.locals.__ = res.__ = function() {
        return i18n.__.apply(req, arguments);
    };
    next();
});

app.get('/:locale', function (req, res) {
    res.cookie('locale', req.params.locale);
    i18n.setLocale(i18n, req.params.locale);
    res.redirect(req.headers.referer);
});

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mashpiecommented, May 13, 2016

haha I didn’t spot that either… cookie vs. cookiename - the later is more explicit => thinking of an alias…

0reactions
mashpiecommented, May 21, 2016

hbs helpers are always tricky to implement as they maintain their own scope[s] in nested levels. I recently setup some helpers by use of this.__() in helper methods. Beware that this will only work on @root level in hbs:

setup express app

// app.js

var exphbs = require('express-handlebars');
var helper = require('./helper');

/**
 * setup templating / rendering
 */
app.engine('hbs', exphbs({
  defaultLayout: 'default',
  extname: 'hbs',
  helpers: helpers(app)
}));

and a helper module combining all of my helpers:

// helpers.js

/**
 * hbs helpers get invoked by rendering
 * a template. They get the res.locals as
 * context in `this`
 */
module.exports = function(app) {
  return {

    /**
     * a basic form helper to render input fields
     */
    input: function(field, opt) {
      opt = opt.data ? {} : JSON5.parse(opt);
      var value = this.formdata ? this.formdata[field] || '' : '';
      var hasError = this.formerror && this.formerror[field];
      var ErrorMsg = hasError ? '<span class="help-block">' +
        this.__(
          this.formerror[field].msg,
          this.__('field.' + field),
          this.formerror[field].value
        ) +
        '</span>' : '';

      return `
          <div class="form-group ${hasError? 'has-error':''}">
            <label for="${field}">${this.__(opt.label || 'field.' + field)}</label>
            <input type="${opt.type || 'text'}"
              class="form-control"
              id="${field}"
              name="${field}"
              value="${value}">
            ${ErrorMsg}
          </div>`;
    }
  }
};

so a view could read like so:

    <form>

        {{{ input 'username' }}}
        {{{ input 'password' "{type: 'password'}" }}}

        {{> alerts/formerrors}}

        <div class="text-center">
            <button type="submit" class="btn btn-primary btn-block">{{__ 'Sign In' }}</button>
        </div>

    </form>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to change the Language Locale of an individual User ...
Answer: · 1. Click Management · 2. Click My User · 3. Click Preferences · 4. Click the Locale drop-down · 5. Select...
Read more >
JavaFX: How to Change the Locale in JFXDatePicker
Short Answer: Use Locale.setDefault(Locale newLocale); (if that's possible in your application). I am not sure if the Uzbek language has a ...
Read more >
How to change Language, Time, Region, Locale settings in ...
Time and Language Settings in Windows 10 consist of all the settings that allow you to set your date and time, time zone,...
Read more >
How to Change System Locale on Windows 10 - YouTube
Steps-1. Go into settings2. Type in control panel3. Select "Clock, Language and Region".4. Select "region"5. Go to the administrative tab6.
Read more >
Visual Studio Code Display Language (Locale)
You can also override the default UI language by explicitly setting the VS Code display language using the Configure Display Language command. Press...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found