superlogin icon indicating copy to clipboard operation
superlogin copied to clipboard

Support i18n

Open juliobetta opened this issue 8 years ago • 5 comments

Any plans to support i18n? I could help...

I've been thinking on something like that:

var superloginConfig = {
  ...
  i18n: {
    en: {
      validation_failed: 'Form validation error',
      email_already_in_use: 'This email has already been taken'
      ...
    },

    'pt-BR': {
      validation_failed: 'Erro na validação do formulário',
      email_already_in_use: 'Este email já está sendo utilizado'
      ...
    }
  }  
  ...
};

Then, somehow, a valid lang will be passed as a parameter in the URL. Maybe when using the superlogin client...

Any thoughts? Thanks!

juliobetta avatar Aug 26 '16 17:08 juliobetta

It's a definite possibility. This would be especially important for the email templates.

However, I've also seen that most API messages are translated client-side. I want to standardize each error so there is a code and message. I suppose we could also implement an optional language parameter (possibly as a header) to request the message in a different language.

I didn't anticipate SuperLogin would get so popular and there's a lot to work on but I'm overwhelmed by other paid projects right now.

SuperLogin can come with default languages and there can be an API to add additional languages. This should be done in its own il8n.config. Go ahead and submit a proposal, and once we agree on the spec you could start working on a PR.

colinskow avatar Aug 26 '16 22:08 colinskow

Looking forward to see such a change as well :) And +1 for the error code.

micky2be avatar Aug 29 '16 04:08 micky2be

I've started creating a file with constant values for each message. Then, on each response, I've added a i18n property, like this:

var c = require('./i18nKeys');
...
res.status(200).json({
  ok: true, 
  message: 'Logged out', 
  i18n: { key: c.LOGGED_OUT }
});
...

It solves my problem for now, but it could be even better if the translation happens on server-side, specially the validation errors provided by sofa-model.

@micky2be could be in charge of implementing a feature in superlogin-client to send a language parameter via header to superlogin API. What do you think?

Please, take a look at my commit here and share your thoughts...

juliobetta avatar Aug 29 '16 12:08 juliobetta

You could already have something based on the browser/os language using accept-language

const acceptLanguage = require('accept-language');
acceptLanguage.languages(['en', 'pt-BR']); // list of language you can manage
const clientLanguage = req.headers['accept-language']; // Header added by browsers
console.log(acceptLanguage.get(clientLanguage)); // will display the most suitable language

I already used it on a project and it's working pretty well. Additionally the client could send a preferred language through config.

micky2be avatar Aug 30 '16 09:08 micky2be

I would prefer something very simple like:

res.status(200).json({
  ok: true, 
  code: 'logged_out',
  message: 'Logged out'
});

The message would be in whichever language was requested in the header, with English as a fallback.

colinskow avatar Aug 30 '16 10:08 colinskow