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.

i18n localization

See original GitHub issue

Hi! First of all I have to say that I discovered AH a few days ago and I am impressed. Thanks for this piece of code!

I am testing it and found an issue that maybe it is a bug, maybe it is something I am doing wrong…

I am using an initializer to create the static method (lookup) to localize client requests as explained in documentation, but even when I check that the returned value is the one I need (I use locale module to parse accept-language header, and, as it sometimes returns the system default locale, I use underscore to discard that if it is not in the “provided languages”), AH always use the default locale set.

At the end I opt to “force” api.i18n to the desired locale using the setLocale function provided. That way, it works fine. A bug?

Thanks in advance,

PD: I pasted below my initializer code… maybe it is useful for somebody… Miguel

'use strict';

var locale = require("locale");
var _ = require("underscore");

module.exports = {
  initialize: function(api, next){
    api.customLocalization = {
      lookup: function(connection){
        var clocale = 'en';
        if(connection.type === 'web'){
            var supported = new locale.Locales(api.config.i18n.locales);
            var locales = new locale.Locales(connection.rawConnection.req.headers["accept-language"]);
            var ulocale = locales.best(supported).toString();
            if(_.indexOf(api.config.i18n.locales, ulocale) !== -1){
                clocale = ulocale;
            }
        }
        /* IF NOT, IT DOES NOT LOCALIZE */
       /* UPDATE: DO NOT DO THIS */
        api.i18n.setLocale(clocale);
        /* --- */
        return clocale;
      }
    };
    next();
  },
  start: function(api, next){
    next();
  },
  stop: function(api, next){
    next();
  }
};

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
evantahlercommented, Jun 19, 2016

I don’t think I follow what you are trying to do here. The line api.i18n.setLocale(clocale) seems to set the system locale, but if you are trying to respond to a client in the proper language, you actually need to set the connection’s locale. ’

To set the connection’s locale, you should tell the config (https://github.com/evantahler/actionhero/blob/master/config/i18n.js#L18-L21) which method to use. This is the method which probably uses locale to check the accept-headers. This method just returns a string locale, ie: en-US. Then, in your actions you can use data.connection.loacalize() to generate strings in the proper languge

0reactions
miguelavaqrodcommented, Jun 21, 2016

Ok then! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Localization vs. Internationalization - W3C
Internationalization is often written in English as i18n, where 18 is the number of letters between i and n in the English word....
Read more >
Internationalization and localization - Wikipedia
Internationalization is the process of designing a software application so that it can be adapted to various languages and regions without engineering changes....
Read more >
i18next documentation: Introduction
i18next is an internationalization-framework written in and for JavaScript. ... provides its own translation management tool: localization as a service.
Read more >
What Is i18n? | Introduction to Internationalization - Lingoport
Internationalization (i18n) is the process of preparing software so that it can support local languages and cultural settings. An internationalized product ...
Read more >
Internationalization vs. localization (i18n vs l10n) - Lokalise
Localization, on the other hand, is the process of adapting your internationalized software to meet the language, cultural, and other ...
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