public static localize ( string $file, string $language = null, string $sourceLanguage = null ) : string | ||
$file | string | the original file |
$language | string | the target language that the file should be localized to. If not set, the value of [[\yii\base\Application::language]] will be used. |
$sourceLanguage | string | the language that the original file is in. If not set, the value of [[\yii\base\Application::sourceLanguage]] will be used. |
return | string | the matching localized file, or the original file if the localized version is not found. If the target and the source language codes are the same, the original file will be returned. |
public static function localize($file, $language = null, $sourceLanguage = null)
{
if ($language === null) {
$language = Yii::$app->language;
}
if ($sourceLanguage === null) {
$sourceLanguage = Yii::$app->sourceLanguage;
}
if ($language === $sourceLanguage) {
return $file;
}
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file);
if (is_file($desiredFile)) {
return $desiredFile;
} else {
$language = substr($language, 0, 2);
if ($language === $sourceLanguage) {
return $file;
}
$desiredFile = dirname($file) . DIRECTORY_SEPARATOR . $language . DIRECTORY_SEPARATOR . basename($file);
return is_file($desiredFile) ? $desiredFile : $file;
}
}