0

If you have a yii2 module installed by composer how can you add a new language without touching the module itself (since touching anything in vendor/ is quite a bad practice)?

Let's say you have a module vbt-cron which already have an English an a Hungarian language files inside and it's configured like this:

\Yii::$app->i18n->translations['vbt-cron'] = [
    'class' => '\yii\i18n\PhpMessageSource',
    'sourceLanguage' => 'en-US',
    'basePath' => '@sharkom/cron/messages',
];

Your app is in Italian so you want to translate the module externally, yii message extracted the strings from the module, you added it to the config:

'vbt-cron' => [
    'class' => 'yii\i18n\PhpMessageSource',
    'basePath' => '@app/messages',
    'fileMap' => [
        'vbt-cron'     => 'vbt-cron.php'
    ],
]

but it doesn't work because yii only look for translations inside the module messages folder:

The message file for category 'vbt-cron' does not exist: vendor/sharkom/yii2-cron/messages/it-IT/vbt-cron.php Fallback file does not exist as well: vendor/sharkom/yii2-cron/messages/it/vbt-cron.php

So, how can you make yii look inside the app messages folder as well?

P.S. I'm looking for a generic answer, not for a way to translate the specific module used as example here.

  • Texts encapsulated in `Yii::t()` can be extracted. `messages` directory do not have that encapsulation. You don't need to extract messages file of different language. What you need to do is add `vbt-cron` in filemap and make sure vendor directory is not in `except` array when extracting messages. There is no need to place file in vendor directory, if you still want to do that way then extend specific module and configure it as you like. – Insane Skull Dec 25 '22 at 08:46
  • Have you considered extending the class in question and override the method or component of your interest and using your extended class instead? – Clarence Bitegeko Jan 16 '23 at 12:05
  • @ClarenceBitegeko extending or forking the module is an option but it seems rather complicated if you only want to translate some strings – Zhigalin - Reinstate CMs Jan 16 '23 at 15:42
  • I think it might be of help if you share the rest of the code, your implementing model, view and controller for the problematic feature. – Clarence Bitegeko Jan 17 '23 at 06:42

0 Answers0