lexik / LexikTranslationBundle

This Symfony bundle allow to import translation files content into the database and provide a GUI to edit translations.
MIT License
419 stars 255 forks source link

TransUnitRepository::getAllDomainByLocale problem #423

Open web-fu opened 1 year ago

web-fu commented 1 year ago

I was trying to refresh the cache via console and I received this error:

Argument 3 passed to Symfony\Bundle\FrameworkBundle\Translation\Translator::addResource() must be of the type string, null given, called in vendor/lexik/translation-bundle/Translation/Translator.php on line 44

After many attempts and some debug, I discovered the problem is caused by this query:

public function getAllDomainsByLocale()
{
    return $this->createQueryBuilder('tu')
        ->select('te.locale, tu.domain')
        ->leftJoin('tu.translations', 'te')
        ->addGroupBy('te.locale')
        ->addGroupBy('tu.domain')
        ->getQuery()
        ->getArrayResult();
}

If the locale column contains any null value the error is triggered.

I don't know if the problem is the leftJoin (maybe should be a normal join) or addResource signature (maybe should accept also null values).

For now I removed the invalid translation_unit rows that caused the problem.

gansky-alexander commented 1 year ago

Same error. I am implementing project on SF 6.2, previously it was iplemented on SF3.4. Database for translations was just copied from one project to another.

martijnboers commented 1 year ago

Have a look at https://github.com/lexik/LexikTranslationBundle/issues/388#issuecomment-1260756831, it solved our issue

web-fu commented 1 year ago

As I said, I solved the issue deleting orphan entry from lexik_trans_unit, but I'm not sure it is a "real" solution or just a workaround. I'll explain how the bug can occur, so maybe it's more clear:

  1. Prepare your view, with your trans term ready ex: {{ 'I am a text to transalte'|trans }}
  2. Go to the /translations endpoint provided by the library
  3. Check for missing translations, but don't translate them
  4. Refresh the cache -> BUG

The missing translation is saved and became an orphan lexik_trans_unit without locale and because
Symfony\Bundle\FrameworkBundle\Translation\Translator::addResource() expect a string (and not a null), it crashes.

The question is: the code shoud exclude orphans or not?

I think "missing translation" should not be an "halting error", just think to a deploy CI that crasches because some translation is missing.

If I would to avoid that an untraslated text to arrive in production: 1 . There should be an explicit option "CI_FAIL_IF_TRANSLATION_MISSING"

  1. There should be an Exception, not a type error.
bartmcleod commented 1 year ago

@web-fu thanks for the clear reproduction steps.