koala-interactive / frenchkiss.js

The blazing fast lightweight internationalization (i18n) module for javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Suggestion] Change the order of parameters

ftonato opened this issue · comments

Hello,

I think would be interesting to change the order of parameters in the set function, in this way we can assume that the language parameter will receive the default _locate, avoiding having to repeat code.

See my suggested code below:
set-function

Let me know, what do you think about it.

Hi, and thanks for your suggestion !

Apart from the breaking change, I see different problems with this implementation.

If the locale isn't defined, it will do the following :

table[""] = {};

I don't feel good with empty string as prop, so it implicates to set a default value to the locale (en ?) to avoid the problem.

It will also break people who wrote the set() before the locale() thinking there is no internal relations:

set({
  test: 'test',
  // ...
});
locale('xx');

And will certainly cause problem with asynchrone JSON loading and locale() updates during this time causing the table to be set in the wrong namespace.

My opinion on this one is too keep it verbose to avoid problems.

Hmmm 🤔,

I don't like "verbose" things, but even if we assume a default value to locale based on browser language, see the below code, we will have problems with async JSON loading =(

let _locale = !languages[navigator.language]
  ? languages[navigator.language.substr(0, 2).toLowerCase()] ||
    'en',
  : languages[navigator.language];

In my opinion, it's not up to this library to check the user language. All websites have different rules to determine it, and the browser language is most of the time the last verification they do, in the order :

  • hl query parameter.
  • fb_locale query parameter.
  • account language (session, cookie)
  • language folder in the url
  • language dns the url
  • browser language

If you really want something that doesn't lead to errors and don't duplicate commands it will be better to configure the table and the locale at the same time, only one function needed.

Something like (not implemented) :

frenchkiss.init({
  locale: 'en',
  fallback: 'fr',
  messages: {
    en: {
       test: 'test'
    },
    fr: {
       test: 'test'
    };
  }
});

Yeah, I agree with you:

it's not up to this library to check the user language 👍

let maybe? = 'keep it verbose is the best approach to avoid problems.';

index