webpack-contrib / i18n-webpack-plugin

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

i18n-webpack-plugin don't work properly with ProvidePlugin #27

Open kimown opened 7 years ago

kimown commented 7 years ago

i18n-webpack-plugin only support __("Hello World") , but I want this plugin support format like this

___("Hello World, language is ${language}",{
    language: language
})

so I make a little change about the source code

// same code
case 2:
    param = this.evaluateExpression(expr.arguments[0]);
    defaultValue = param = param.string;
    break;
// same code ...
var dep = new ConstDependency(JSON.stringify(result), expr.arguments.length==2?expr.arguments[0].range:expr.range);
dep.loc = (expr.arguments.length==2?expr.arguments[0].loc: expr.loc);
// same code

, but when I apply webpack.ProvidePlugin, the error occurs.

image

Here is my code, yarn install, webpack -w, then open index.html, you can see the error in Chrome DevTools console. i18n.zip

I spent some time trying to fix this problem, but failed, I don't know why this happens. any suggestion will be appreciated, thanks.

oreqizer commented 7 years ago

you can always make another function that suits your needs, then shove the translated strings together with placeholders into your custom function that will do the replacing

StephanBijzitter commented 7 years ago

For example:

const language = 'Dutch';
const message = template(__('Hello World, language is %s'), language);

function template(target = '', ...replacements) {
    for (let i = 0; i < replacements.length; i++) {
        target = target.replace('%s', replacements[i]);
    }

    return target;
}

Your translation file would look like:

{
   "Hello World, language is %s": "Hallo wereld, taal is %s"
}