yii\helpers\BaseFileHelper::localize PHP Method

localize() public static method

The searching is based on the specified language code. In particular, a file with the same name will be looked for under the subdirectory whose name is the same as the language code. For example, given the file "path/to/view.php" and language code "zh-CN", the localized file will be looked for as "path/to/zh-CN/view.php". If the file is not found, it will try a fallback with just a language code that is "zh" i.e. "path/to/zh/view.php". If it is not found as well the original file will be returned. If the target and the source language codes are the same, the original file will be returned.
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;
        }
    }