简体   繁体   English

Symfony2从javascript提取文本到翻译

[英]Symfony2 extract text from javascript to translation

I have installed BazingaJsTranslationBundle in my Symfony2 project. 我在Symfony2项目中安装了BazingaJsTranslationBundle

It works fine but is there a way to extract text from javascript file the way done in translation:extract symfony2 command? 它工作正常,但有没有办法从javascript文件中提取文本的方式在翻译:提取 symfony2命令?

The text in javascript file looks like this: javascript文件中的文本如下所示:

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

I do not want to go through adding keys in twig or php files to translate text in javascript files. 我不想在twig或php文件中添加键来翻译javascript文件中的文本。

Here is the command that I use to extract text from php and twig files: 这是我用来从php和twig文件中提取文本的命令:

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

You can by creating a custom extractor for the JS translations. 您可以通过为JS翻译创建自定义提取器。 That's done by implementing the Symfony\\Component\\Translation\\Extractor\\ExtractorInterface : 这是通过实现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);
}

To make it usefull to others, it'll be even more awesome if you submitted your extractor to the BazingaJsTranslationBundles. 为了使它对其他人有用,如果你将提取器提交给BazingaJsTranslationBundles,它会更加棒极了。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM