php - 是否可以在 Laravel 中自动添加翻译变量?
问题描述
我有一些刀片,其中包括许多这样的上诉:
<div>{{ __('auth.Example') }}</div>
<a href='#'>{{ __('xyz.Another One Example') }}</a>
<span>{{ __('xyz.Example Number Three') }}</span>
...
我将文件添加xyz.php
到目录。现在我想知道,是否可以使用一些命令或方法,将适当的变量添加到这些文件中以重新填充?
除了准备好的翻译,我不会,但类似的东西将不胜感激:auth.php
lang/en
lang/pl
xyz.php
return [
...
'Another One Example' => 'Another One Example',
'Example Number Three' => 'Example Number Three',
...
];
授权文件
return [
...
'Example' => 'Example'
...
];
之后,我可以快速更改正确语言的翻译值,但首先我需要在正确的文件中包含正确的变量。现在我正在手动将每个变量复制到正确的文件中,但感觉就像是永恒的......你有什么想法吗,我怎样才能更容易地做到这一点?
解决方案
我以前写过这样的脚本。我使用下面的函数扫描所有文件并使用正则表达式来检测翻译键。
function findTranslationKeys($grep, $path, $regex)
{
$keys = [];
$filenames = glob(base_path() . $path . '/**/**');
foreach ($filenames as $filename) {
if (preg_match_all($regex, file_get_contents($filename), $matches)) {
foreach ($matches[1] as $index => $match) {
$keys[$match] = $match;
}
}
}
return $keys;
}
$keys = $this->findTranslationKeys( '/resources/views', '/__\(\'([^\']*)\'[^\)]*/s');
您可以加载现有的翻译
$filePath = base_path() . '/resources/lang/en/' . $file . '.php';
if (file_exists($filePath)) {
$existing = require $filePath;
} else {
$existing = [];
}
将任何缺少的翻译添加到文件中,然后将文件写回
$fh = fopen($filePath, 'w');
fwrite($fh, "<?php\n");
fwrite($fh, 'return ' . var_export($existing, true) . ';');
fclose($fh);
推荐阅读
- reactjs - 在反应中使用插件国际电话输入
- gradle - 如何在 Gradle 中设置任务的“之前”和“之后”顺序
- javascript - 将 dir rtl 属性添加到 vuejs 中的 html 标记
- elasticsearch - 必须存在的聚合过滤器返回不存在的字段
- bash - MacintoshHD 内卷内 MacintoshHD 内卷内...?
- leaflet - 添加geojson图层会导致覆盖底图的灰色背景
- spring-el - 如何为多个系统属性执行多个逻辑端?
- excel - 如何根据值的频率为一行中的单元格着色?
- python - 如何在树视图中创建下拉列表菜单?
- python - 在此服务器上找不到请求的 URL /。Apache服务器中的烧瓶