PHP Laravel,识别未使用的翻译

Maa*_*duk 3 php translation laravel

我在想是否有一种方法可以使用常规下划线翻译来识别 Laravel 中未使用的翻译键?

lin*_*ref 8

如果您使用某些模式循环使用翻译,则这将不起作用,并且您最终可能会删除正在使用的键。

其中一种方法是使用grep

首先,打开修补程序

php artisan tinker
Run Code Online (Sandbox Code Playgroud)

然后需要翻译文件并将其存储在变量中

$translations = require resource_path('lang/en/app.php');
Run Code Online (Sandbox Code Playgroud)

$translations变量将包含翻译文件返回的数组。

循环遍历数组并grep针对每个键使用

$unused_keys = [];

foreach ($translations as $key => $value) {

    // Here app is the name of the translation file
    // that was required in first step.
    // Replace app with the name of the translation file that is been required.

    $out = exec('echo $(grep -rn "app.' . $key . '" . --exclude-dir=vendor)');

    echo strlen($out) . " " . $key . "\n";

    if (strlen($out) <= 0) {
        $unused_keys[] = $key;
    }
}

// The $unused_keys array would contain the translation key that is not being used.
Run Code Online (Sandbox Code Playgroud)