Symfony2从javascript提取文本到翻译

时间:2014-04-20 10:55:36

标签: javascript symfony translation

我在Symfony2项目中安装了BazingaJsTranslationBundle

它运行正常,但是有没有办法从javascript文件中提取文字,方法是翻译:提取 symfony2命令?

javascript文件中的文字如下所示:

Translator.trans("This is the text that I want to extract")

我不想在twig或php文件中添加密钥来翻译javascript文件中的文本。

这是我用来从php和twig文件中提取文本的命令:

php app/console translation:extract en
--enable-extractor=jms_i18n_routing --dir=./src/ --output-dir=./app/Resources/translations

2 个答案:

答案 0 :(得分:1)

您可以为JS翻译创建自定义提取器。通过实施Symfony\Component\Translation\Extractor\ExtractorInterface

完成了这项工作
/**
 * Extracts translation messages from a template directory to the catalogue.
 * New found messages are injected to the catalogue using the prefix.
 *
 * @author Michel Salib <michelsalib@hotmail.com>
 */
interface ExtractorInterface
{
    /**
     * Extracts translation messages from a template directory to the catalogue.
     *
     * @param string           $directory The path to look into
     * @param MessageCatalogue $catalogue The catalogue
     */
    public function extract($directory, MessageCatalogue $catalogue);

    /**
     * Sets the prefix that should be used for new found messages.
     *
     * @param string $prefix The prefix
     */
    public function setPrefix($prefix);
}
  

为了使其对其他人有用,如果您将提取器提交给BazingaJsTranslationBundles,它会更加棒极了。

答案 1 :(得分:0)

我在主存储库中为此打开了一个PR:https://github.com/willdurand/BazingaJsTranslationBundle/pull/164