8000 Documented translation.extractor · symfony/symfony-docs@e03c9f4 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e03c9f4

Browse files
committed
Documented translation.extractor
1 parent 3081634 commit e03c9f4

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

reference/dic_tags.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ may also be tags in other bundles you use that aren't listed here.
6565
+-----------------------------------+---------------------------------------------------------------------------+
6666
| `translation.loader`_ | Register a custom service that loads translations |
6767
+-----------------------------------+---------------------------------------------------------------------------+
68+
| `translation.extractor`_ | Register a custom service that extracts translation messages from a file |
69+
+-----------------------------------+---------------------------------------------------------------------------+
6870
| `twig.extension`_ | Register a custom Twig Extension |
6971
+-----------------------------------+---------------------------------------------------------------------------+
7072
| `validator.constraint_validator`_ | Create your own custom validation constraint |
@@ -916,6 +918,76 @@ file, but it might either be blank or contain a little bit of information
916918
about loading those resources from the database. The file is key to trigger
917919
the ``load`` method on your custom loader.
918920
921+
translation.extractor
922+
---------------------
923+
924+
**Purpose**: To register a custom service that extracts messages from a file
925+
926+
.. versionadded:: 2.1
927+
The ability to add message extractors is new in 2.1
928+
929+
When executing the ``translation:update`` command, it uses extractors to
930+
extract translation messages from a file. By default, the Symfony2 framework
931+
has a :class:`Symfony\\Bridge\\TwigBridge\\Translation\\TwigExtractor` and a
932+
:class:`Symfony\\Bundle\\FrameworkBundle\\Translation\\PhpExtractor`.
933+
934+
You can create your own extractor by creating a class which implements
935+
:class:`Symfony\\Component\\Translation\\Extractor\\ExtractorInterface` and
936+
tagging the service with ``translation.extractor``. The tag has one required
937+
option: ``alias``, this defines the name of the extractor.
938+
939+
// src/Acme/DemoBundle/Translation/FooExtractor.php
940+
namespace Acme\DemoBundle\Translation;
941+
942+
use Symfony\Component\Translation\Extractor\ExtractorInterface;
943+
use Symfony\Component\Translation\MessageCatalogue;
944+
945+
class FooExtractor implements ExtractorInterface
946+
{
947+
protected $prefix;
948+
949+
/**
950+
* Extracts translation messages from a template directory to the catalogue.
951+
*/
952+
public function extract($directory, MessageCatalogue $catalogue)
953+
{
954+
// ...
955+
}
956+
957+
/**
958+
* Sets the prefix that should be used for new found messages.
959+
*/
960+
public function setPrefix($prefix)
961+
{
962+
$this->prefix = $prefix;
963+
}
964+
}
965+
966+
.. configuration-block::
967+
968+
.. code-block:: yaml
969+
970+
services:
971+
acme_demo.translation.extractor.foo:
972+
class: Acme\DemoBundle\Translation\FooExtractor
973+
tags:
974+
- { name: translation.extractor, alias: foo }
975+
976+
.. code-block:: xml
977+
978+
<service id="acme_demo.translation.extractor.foo"
979+
class="Acme\DemoBundle\Translation\FooExtractor">
980+
<tag name="translation.extractor" alias="foo" />
981+
</service>
982+
983+
.. code-block:: php
984+
985+
$container->register(
986+
'acme_demo.translation.extractor.foo',
987+
'Acme\DemoBundle\Translation\FooExtractor'
988+
)
989+
->addTag('translation.extractor', array('alias' => 'foo'));
990+
919991
.. _reference-dic-tags-twig-extension:
920992
921993
twig.extension

0 commit comments

Comments
 (0)
0