【发布时间】:2011-12-28 04:26:45
【问题描述】:
我发现我忘记在翻译文件中添加一些翻译(我在我的项目中使用 yaml)。不知何故需要找出那些并添加他们的翻译,但是手动这样做很麻烦。我想知道是否有更简单快捷的方法来完成这项任务。
【问题讨论】:
标签: symfony translation yaml
我发现我忘记在翻译文件中添加一些翻译(我在我的项目中使用 yaml)。不知何故需要找出那些并添加他们的翻译,但是手动这样做很麻烦。我想知道是否有更简单快捷的方法来完成这项任务。
【问题讨论】:
标签: symfony translation yaml
你应该安装JMSTranslationBundle:
这个包让 Symfony 翻译组件更加强大。虽然翻译组件经过高度优化以减少代码的运行时开销,但它缺少一些翻译器功能。此捆绑包的目的是使网站的翻译更容易,同时仍保留当前实施的所有性能优化。
主要特点包括:
- 允许开发人员向翻译 ID 添加额外的上下文,以帮助翻译人员找到可能的最佳翻译
- 优化的转储命令(更好的格式,为翻译人员提供更多信息,标记新消息)
- 优化的搜索算法(更快、更可靠地找到消息)
- 可以为捆绑包提取消息,并且可以通过设置您的应用程序(捆绑包)提取配置
- 配置以避免重新键入许多命令行参数/选项
- 基于 Web 的 UI,更容易翻译消息
【讨论】:
您无法使用 JMSTranslationBundle 找到所有未翻译的消息。它使用标准的 Symfony 方法来查找它们。要查找所有内容,您需要将服务附加到记录器。这样您就可以将未翻译的密钥写入数据库。
我认为,这只适用于开发环境。
<?php
namespace AppBundle\Service;
use AppBundle\Entity\Translation;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Monolog\Handler\AbstractProcessingHandler;
class TranslationLogger extends AbstractProcessingHandler
{
protected $container;
public function __construct (ContainerInterface $container)
{
$this->container = $container;
}
protected function write (array $record)
{
if ($record['channel'] === 'translation')
{
$em = $this->container->get('doctrine.orm.default_entity_manager');
$check = $em->getRepository('AppBundle:Translation')->findBy([ 'transKey' => $record['context']['id'] ]);
if (!$check)
{
$new = new Translation;
$new->setTransKey($record['context']['id']);
$em->persist($new);
$em->flush();
}
}
}
}
services.yml:
services:
appbundle.translation_logger:
class: AppBundle\Service\TranslationLogger
arguments: [ "@service_container" ]
【讨论】: