ngx-translate-extract icon indicating copy to clipboard operation
ngx-translate-extract copied to clipboard

Translation get erased when fetching multiple keys at once

Open kamilchlebek opened this issue 5 years ago • 8 comments

Hello,

I've defined manually multiple translation keys using marker. However they get erased if I try to fetch translation for whole tree starting from some key.

Example:

_('something.value1')
_('something.value2')
...

I want to fetch translate.instant('something') - it works, but then both something.value1 and something.value2 are removed from the output json file

kamilchlebek avatar Oct 22 '19 06:10 kamilchlebek

Please provide the command you're using to extract the keys and a file that causes the issue you're seeing. I'll take a look when I get some spare time.

biesbjerg avatar Oct 22 '19 07:10 biesbjerg

command: npx ngx-translate-extract --format 'namespaced-json' --marker _ --sort --clean --fi ' ' -i app/src -o app/fr.json

Part of the service from which translations are properly extracted:

 import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker'
 getPlaceholdersConfig() {
    return {
      some_placeholder: {
        icon: ['fal', 'user'],
        title: _('settings.sms.element.some_placeholder'),
      },
 }

Part of the code that makes translation disappear (empty string instead of object in fr.json)

return new SmsPlaceholders(htmlValue, this._translate.instant('settings.sms.element'))

Before adding: this._translate.instant('settings.sms.element') looks like that:

"sms": {
    "element": {
        "some_placeholder": "Some placeholder",
    }
}

After:

"sms": {
    "element": ""
},

I guess the easiest solution would be to add another marker - to ignore some of the TranslateService method calls

kamilchlebek avatar Oct 22 '19 08:10 kamilchlebek

I think it's normal to do that. You can't have a string and an object as a value of a JSON property at once.

I'll try to explain better.

In my understanding, the plugin will search for all translation marks you put on your html and/or ts files. Once it finds settings.sms.element.some_placeholder, it will create it's nested tree and property if it does not exist, as you showed in your first JSON sample. After that, it finds your settings.sms.element and analyses your JSON again to check if the property exists. It actually does, but as an object, which does not provide a translation, so the package will delete the object and replace it with a string, as you showed in your second JSON sample.

My suggestion to you is that you add another property to that object with the settings.sms.element translation. If you want to send an object of translations inside instant(), I would not recommend to do so. ngx-translate docs states that you can only send a string or an array of strings to instant method.

Hope it helps.

GunterJameda avatar Nov 04 '19 18:11 GunterJameda

Unfortunately it does not help. It's fairly obvious that value for given key can't be both string and object at the same time.

The issue is that I can't fetch part of the translation tree at once. ngx-translate may return string or object with multiple translations - that's what I'm trying to achieve.

It's working, but ngx-translate-extract incorrectly erases part of the tree from JSON.

kamilchlebek avatar Nov 04 '19 20:11 kamilchlebek

Right, but in my pov, the plugin is working correctly, as I explained before.

Anyway, I'm not sure, but I guess I read somewhere that there is a way for you to escape the string from being caught by the plugin. If so, try to do so in your settings.sms.element to check if it works. :)

GunterJameda avatar Nov 05 '19 10:11 GunterJameda

there is a way for you to escape the string from being caught by the plugin.

That would do the job. Do you know how to exclude string from being analyzed?

kamilchlebek avatar Nov 05 '19 10:11 kamilchlebek

As I said, I'm not sure there is something out there. I'm almost 100% sure I read something like that some days ago when I started using the plugin. I'll double check and tell you if I find something relevant. :)

GunterJameda avatar Nov 05 '19 10:11 GunterJameda

In case we find nothing, you can always submit a PR to do so. :)

GunterJameda avatar Nov 05 '19 10:11 GunterJameda