Forcing Yii:t to translate string in given language

What is the best way to force Yii:t to return string translated in a given language, no matter what is the current value of Yii::$app->language? Is there any parameter for this kind of purpose?

The only method, that I found out, is to temporarily change language and revert it back once translation is done:


$currentLanguage = Yii::$app->language;


if (!$translateFieldNames) {

	Yii::$app->language = 'de';

}


foreach($metaData as $name => $value) {

	$newMetaData[Yii::t('models', $name)] = $value;

}


if (!$translateFieldNames) {

	Yii::$app->language = $currentLanguage;

}

But that just sounds creepy to me and I’m looking, if there is a better way of doing this?




Yii::$app->i18n->translate('app', 'Hello!', [], $targetLanguage);