Translations in sourceLanguage does not work in Yii2 application

I’m using a translations based on keywords in my Yii2 application (I know, that this isn’t best option, but I don’t have other). I’ve prepared @app/messages/pl/app.php and @app/messages/en/app.php files with translation strings using keywords, instead of full-featured sentences or words:


<?php

	return [

    	'name_english'=>'Name in English',

    	'keywords_english'=>'Keywords in English'

	];

?>

I have set my application to use Polish language as default:


'language' => 'pl',

'sourceLanguage' => 'en',

And I’m invoking translation:


Yii::t('app', 'keywords_english');

Everything works fine, when language is actually set to base, Polish (pl):

But, when I change it to English (en; either by setting Yii::$app->language during runtime or by changing application configuration), translation is not performed and I’m getting keywords_english:

I have put die() in the beginning of @app/messages/pl/app.php and @app/messages/en/app.php files and I can clearly see, that when language is set to English, second file is not being included by Yii2 (application run follows), while, when language is Polish, first file is included and application flow breaks on that die().

What am I missing? Why Yii2 is not using translations from @app/messages/en/app.php file, if language is set to English (en)?

EDIT: By default, I was not altering default i18n component configuration in my application’s configuration as I found no need for that. Translation files are stored in default position (@app/messages/<language>/) and are using default class (yii\i18n\PhpMessageSource). And this is working for all languages except sourceLanguage. At some point, I tried to alter configuration:




'i18n' => [

    'translations' => [

        '*' => [

            'class' => 'yii\i18n\PhpMessageSource',

            'sourceLanguage' => 'en',

            'basePath' => '@app/messages'

        ],

    ],

],

But, it brought no change (why should it – it still uses default settings).

You need to force translation by using http://www.yiiframework.com/doc-2.0/yii-i18n-messagesource.html#$forceTranslation-detail

Thanks, that’s the point!

Please, stop spamming with this extension! I’ve seen your “ad” about it in many threads. It has nothing to do with context of this discussion. Without solution given by samdark, this wouldn’t not work, no matter, if I would use your extension or not.

I deleted it, anyway, it’s not spam, just try to share my knowledge.

That is great! I think, that your extension is also fine and I wrote done to test it, when I come to the part of management of translations in my app, because it really looks promising.

But you should not write about it in questions, that has nothing to do with managing of translations, because people can get initiated and ignore your work. Think yourself, how many times you’re getting upset, if you’re talking about one thing and someone else is talking about something completely different, off-topic? :>

Thank you Trejder :)

On second thought… some side questions:

  1. Can I enable this only for particular controller or view? How?

  2. Will this decrease application performance noticeable? Or are translation lightning-fast by their nature? Should care about enabling this only for particular view or controller?

  3. Why I can’t use this with * for all translations? Why do I have to use:


'i18n' => [

	'translations' => [

    	'app' => [

        	'class' => 'yii\i18n\PhpMessageSource',

        	'forceTranslation' => true

    	],

	],

],

and can’t use:


'i18n' => [

	'translations' => [

    	'*' => [

        	'class' => 'yii\i18n\PhpMessageSource',

        	'forceTranslation' => true

    	],

	],

],

(the latter doesn’t work)?

1 Like
  1. I think so. By setting property dynamically i.e. Yii::$app->…

  2. Measure it.

  3. Because * is fallback translation http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html

1 Like

My sourceLanguage is "en-US"

My language can be "en" or "ru"

If i use forceTranslation and my language is “en” then log files are full of messages “The message file for category ‘yii’ does not exist”.

How to fix it?