webpack-contrib / i18n-webpack-plugin

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

Bug: Concatenate strings in i18n #32

Open Rincewind3d opened 7 years ago

Rincewind3d commented 7 years ago

Hello,

this may be a basic problem/limitation from Webpack, but perhaps someone has an idea.

If i18n is called with a string concatenation Webpack throws an reference error.

let foo = 'foo';
__('bar.'+foo);
ReferenceError: __ is not defined

I checked the i18n-webpack-plugin code and figured out the the problem is this.evaluateExpression(expr.arguments[0]) which is not working if there's string concatenation. My knowledge about webpack plugins is limited, perhaps someone has an idea?

The use for this string concatenation is in combination with the html-webpack-plugin. For each site I define a template variable with the name of the site and use it to get the translation for the header title.

<head>
  <title>${__('view.' + htmlWebpackPlugin.options.environment.viewName + '.title')}</title>
</head>

Yeah, es6 template would be better. But as in https://github.com/webpack-contrib/i18n-webpack-plugin/issues/6 described it's not working.

mightyaleksey commented 7 years ago

Hi, the current version works only with the string literals as an argument as you found earlier and skips other combinations (binary expressions and e.t.c.). Thats why the function call __() remains untouched which results in exception.

I did a small modification in https://github.com/webpack-contrib/i18n-webpack-plugin/pull/58 which provides possibility to use string concatenation (BinaryExpressions) and templated strings (TemplateLiteral) with variables also. But it requires some time for review and to land into the master.