webpack-contrib / i18n-webpack-plugin

[DEPRECATED] Embed localization into your bundle
MIT License
317 stars 74 forks source link

feat: dynamic keys and keyset implementation #42

Closed mightyaleksey closed 7 years ago

mightyaleksey commented 7 years ago

Dynamic and Parametrized keys

Adds support of dynamic and parametrized keys (issue https://github.com/webpack-contrib/i18n-webpack-plugin/issues/1):

Syntax example:

const count = 3;
const param1 = 'p1';
const param2 = 'p2';

__(`${count} nights`) // dynamic
__(`${param1} ${param2}`) // parametrized

Configuration example:

plugins: [
  ...
  new I18nPlugin({
    '{count} nights': [
      '{count} ночь',
      '{count} ночи',
      '{count} ночей'
    ],
    '{param1} {param2}': '{param1} {param2}',
  }, {
    pluralIdentName: 'count', // the identifier's name, which is used to detect dynamic keys
    pluralRule: 'ru', // the way to choose the particular form by count value
  })
],

Unfortunately, only template literals supported in the current version. Binary expressions (like a + ': ' + b) won't work.

Nested dictionaries

Since, it is not possible to localize properly keys with dots (with nested option). For example,

__('KEYSET.Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it.')

won't be possible to localize with partialy flat object

{
  KEYSET: {
    'Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it.': '<translation>',
  }
}

since the sentence will be splitted by dots and it will be possible to localize the part of it only with

{
  KEYSET: {
    'Light thinks it travels faster than anything but it is wrong': {
      ' No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it': {
        '': '<translation>',
      }
    }
  }
}

I decided to move the path part to the separated argument (option keyset), so the resulting call will look like:

__('KEYSET', 'Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it.')

and the configuration will be:

{
  KEYSET: {
    'Light thinks it travels faster than anything but it is wrong. No matter how fast light travels, it finds the darkness has always got there first, and is waiting for it.': '<translation>',
  }
}
jsf-clabot commented 7 years ago

CLA assistant check
All committers have signed the CLA.

joshwiens commented 7 years ago

Alright, straight out of the gate there are two issues blocking this from landing in it's current state.

As an organization, we are moving to a single standard way of doing things to simplify maintenance. Code style, Allowed Language Features, Test Frameworks ... the whole nine yards.

So before this can be considered conceptually and it will be considered given the age of the related issue, there are two things that need to happen.

1.) webpack-contrib/i18n-webpack-plugin#41 needs to land which is the basis for everything above.

2.) This lib needs validations in place for the major version that is about to land before any major features are considered.

First blocking issue should land this weekend. The second, to completely honest, is going to get done whenever I have time to circle back to it. The webpack-contrib org maintainers have more than a few fires to fight at the moment and upgrading this plugin was upgraded out of order to address a deprecation issue that requires a semver MAJOR hence why the webpack-defaults upgrade was done now.

Any assistance with getting a test suite in place would be greatly appreciated. As I said, the four of us are swamped getting standards in place for every loader & plugin in the organization. Optionally, I can leave this PR open and ping you when I get time to put a test suite in place though I can't honestly tell you when that is going to be at this point.

mightyaleksey commented 7 years ago

@d3viant0ne thank you for the reply.

I'll wait till you finish with the basics. Then, I guess, I can split the current pr and help you with tests, so we can discuss the new features after.

michael-ciniawsky commented 7 years ago

Fixes #6, #16

mightyaleksey commented 7 years ago

A small note. Looks like a babel-loader transforms all the templated literals into the binary expressions (string concatenation) before the plugin. So it's better to support both variants.

joshwiens commented 7 years ago

@sullenor - Basics are in master as of this afternoon & out on a beta dist-tag. Once I get the coverage up to something reasonable, we can start working on some of these long standing features.

Telokis commented 7 years ago

Anything new about this PR?

mightyaleksey commented 7 years ago

@Telokis yeah, finally managed to make a smaller version in https://github.com/webpack-contrib/i18n-webpack-plugin/pull/58