在Symfony 2中,我使用translation:update命令从我的模板生成翻译YML文件,我已经定义了翻译字符串。
我得到.yml文件,其中包含所有内容。
我正在寻找一个工具,一个可以重构这个的脚本:
menu.home: __en.menu.home
menu.projects: __en.menu.projects
information.address: __en.information.address
information.agent.languages.english: __en.information.agent.languages.english
information.agent.languages.russian: __en.information.agent.languages.russian
information.agent.name: __en.information.agent.name
到:
information:
address: __en.information.address
agent:
languages:
english: __en.information.agent.languages.english
russian: __en.information.agent.languages.russian
name: __en.information.agent.name
menu:
home: __en.menu.home
projects: __en.menu.projects
答案 0 :(得分:0)
以下是执行您要求的代码段:
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Dumper;
// ...
$dottedYaml = Yaml::parse(file_get_contents('dotted-file.yml'));
$nestedYaml = array();
foreach ($dottedYaml as $dottedKey => $value) {
$levels = explode('.', $dottedKey);
$levelYaml =& $nestedYaml;
do {
$level = array_shift($levels);
if (!isset($levelYaml[$level])) {
$levelYaml[$level] = array();
}
$levelYaml =& $levelYaml[$level];
} while (count($levels));
$levelYaml = $value;
}
$dumper = new Dumper();
file_put_contents('nested-file.yml', $dumper->dump($nestedYaml, 5));
请注意,即使我的代码适用于您的示例,如果您有类似的内容:
menu: "here is the problem"
menu.home: __en.menu.home
menu.projects: __en.menu.projects
除非您在某个级别包含标量值和嵌套值数组时决定某种约定,否则您无法实际转换为嵌套表示法:
menu:
home: __en.menu.home
projects: __en.menu.projects
value: "here is the problem" # we made up a "value" attribute to store the top level value