ngx-translate / i18n-polyfill

A speculative polyfill to support i18n code translations in Angular
MIT License
139 stars 30 forks source link

Extraction fails with split strings (concatenation of 2 strings) #45

Open sChupin opened 6 years ago

sChupin commented 6 years ago

When a value is the result of the concatenation of 2 strings, I got the following error after running ng xi18n and ngx-extractor:

node_modules/@ngx-translate/i18n-polyfill/extractor/src/abstract-ast-parser.js:29
                    throw new Error(`An I18nDef requires a value property on '${this.syntaxKindToName(firstArg.kind)}' for ${firstArg}`);
                    ^

Error: An I18nDef requires a value property on 'ObjectLiteralExpression' for [object Object]

Example of i18n-polyfill use that causes this issue:

this.i18n({ value: 'My ' + 'value', id: 'myId', description: 'myDescription' });

The reason why I have some split strings is that my editor wraps line at 120 characters.

adrienverge commented 5 years ago

The problem also happens for invocations with strings (not I18nDef):

this.i18n('My ' + 'value');

I propose a simple fix at #50.

DanielHabenicht commented 5 years ago

I get the same error message with this syntax:

this.i18n({
    meaning: 'ChangeProfilePictureComponent',
    description: 'Error Message if wrong File Type is supplied',
    id: 'ChangeProfilePictureComponentErrorWrongFileType',
    value: `The File Type '${file.type}' is not supported.`
})
SurienDG commented 5 years ago

@DanielHabenicht change `The File Type '${file.type}' is not supported.` to \'The File Type {{file.type}} is not supported.' You need to use the interpolation syntax instead of ${file.type} syntax

SurienDG commented 5 years ago

@sChupin a possible work around for long lines would be to use ` instead of ' for the string you need to split on different lines that way you don't need to use the plus operator:

Example:

this.i18n({ value: `My really really really really really really \
really really really really really really really really really \
really really really really really really really really really really really long string`, 
id: 'myId', description: 'myDescription' });

rather than

this.i18n({ value: 'My really really really really really really' +
'really really really really really really really really really' +
'really really really really really really really really really really really long string', 
id: 'myId', description: 'myDescription' });