BoltTranslate / labels

Bolt Labels extension - Translatable labels for Bolt
https://bolt.cm/
18 stars 12 forks source link

Check MutableBag with has() instead of hasItem() #62

Closed nestordedios closed 4 years ago

nestordedios commented 6 years ago

Fixes #61

It seems hasItem($label) always returns false and the check on line 117 https://github.com/BoltTranslate/labels/blob/master/src/Labels.php#L117 never evaluates to true.

Then translated labels are being reset on line 121 https://github.com/BoltTranslate/labels/blob/master/src/Labels.php#L121

SvanteRichter commented 6 years ago

My guess is that this has to do with the strict check that hasItem does, while has is more naive IIRC.

The specific parts for hasItem are: https://github.com/bolt/collection/blob/9ef5a76e4b417d16adb5850cfe5cc93ed1903e2c/src/Bag.php#L180-L192

And for has: https://github.com/bolt/collection/blob/9ef5a76e4b417d16adb5850cfe5cc93ed1903e2c/src/Bag.php#L149-L159

xiaohutai commented 4 years ago

Yes, hasItem needs to be has. The code wants to check whether a key for a label has been defined.

As pointed out above. Given:

$label1 : {
    $language1: $translation1,
    $language2: $translation2
},

This function is called in the twigL function and is supposed to add labels if they don't exist yet. However, every time a label+language combination's value is empty, all the translations for that label will be wiped.

There's another hasItem check that needs to be changed. It's basically the same check. I made a new PR with this addition: #65


Initially, I thought the whole labels.json would be wiped, but it's only for labels with non-existent translations.

NOTE: I still don't know why it happens with one of my projects that has both translations filled in and still get wiped.