From e03c9f4ecb654a9e1e184a6dd5f5d446e3dbda06 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 7 Jun 2013 16:55:14 +0200 Subject: [PATCH 1/2] Documented translation.extractor --- reference/dic_tags.rst | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index 3a9cf26cd9c..d84c7c62b05 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -65,6 +65,8 @@ may also be tags in other bundles you use that aren't listed here. +-----------------------------------+---------------------------------------------------------------------------+ | `translation.loader`_ | Register a custom service that loads translations | +-----------------------------------+---------------------------------------------------------------------------+ +| `translation.extractor`_ | Register a custom service that extracts translation messages from a file | ++-----------------------------------+---------------------------------------------------------------------------+ | `twig.extension`_ | Register a custom Twig Extension | +-----------------------------------+---------------------------------------------------------------------------+ | `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 about loading those resources from the database. The file is key to trigger the ``load`` method on your custom loader. +translation.extractor +--------------------- + +**Purpose**: To register a custom service that extracts messages from a file + +.. versionadded:: 2.1 + The ability to add message extractors is new in 2.1 + +When executing the ``translation:update`` command, it uses extractors to +extract translation messages from a file. By default, the Symfony2 framework +has a :class:`Symfony\\Bridge\\TwigBridge\\Translation\\TwigExtractor` and a +:class:`Symfony\\Bundle\\FrameworkBundle\\Translation\\PhpExtractor`. + +You can create your own extractor by creating a class which implements +:class:`Symfony\\Component\\Translation\\Extractor\\ExtractorInterface` and +tagging the service with ``translation.extractor``. The tag has one required +option: ``alias``, this defines the name of the extractor. + + // src/Acme/DemoBundle/Translation/FooExtractor.php + namespace Acme\DemoBundle\Translation; + + use Symfony\Component\Translation\Extractor\ExtractorInterface; + use Symfony\Component\Translation\MessageCatalogue; + + class FooExtractor implements ExtractorInterface + { + protected $prefix; + + /** + * Extracts translation messages from a template directory to the catalogue. + */ + public function extract($directory, MessageCatalogue $catalogue) + { + // ... + } + + /** + * Sets the prefix that should be used for new found messages. + */ + public function setPrefix($prefix) + { + $this->prefix = $prefix; + } + } + +.. configuration-block:: + + .. code-block:: yaml + + services: + acme_demo.translation.extractor.foo: + class: Acme\DemoBundle\Translation\FooExtractor + tags: + - { name: translation.extractor, alias: foo } + + .. code-block:: xml + + + + + + .. code-block:: php + + $container->register( + 'acme_demo.translation.extractor.foo', + 'Acme\DemoBundle\Translation\FooExtractor' + ) + ->addTag('translation.extractor', array('alias' => 'foo')); + .. _reference-dic-tags-twig-extension: twig.extension From d46a88e9d68362166327b31279142d47d7bf4f24 Mon Sep 17 00:00:00 2001 From: WouterJ Date: Fri, 7 Jun 2013 17:01:47 +0200 Subject: [PATCH 2/2] Documented translation.dumper tag --- reference/dic_tags.rst | 56 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/reference/dic_tags.rst b/reference/dic_tags.rst index d84c7c62b05..4fb199e3dbb 100644 --- a/reference/dic_tags.rst +++ b/reference/dic_tags.rst @@ -67,6 +67,8 @@ may also be tags in other bundles you use that aren't listed here. +-----------------------------------+---------------------------------------------------------------------------+ | `translation.extractor`_ | Register a custom service that extracts translation messages from a file | +-----------------------------------+---------------------------------------------------------------------------+ +| `translation.dumper`_ | Register a custom service that dumps translation messages | ++-----------------------------------+---------------------------------------------------------------------------+ | `twig.extension`_ | Register a custom Twig Extension | +-----------------------------------+---------------------------------------------------------------------------+ | `validator.constraint_validator`_ | Create your own custom validation constraint | @@ -988,6 +990,60 @@ option: ``alias``, this defines the name of the extractor. ) ->addTag('translation.extractor', array('alias' => 'foo')); +translation.dumper +------------------ + +**Purpose**: To register a custom service that dumps messages to a file + +.. versionadded:: 2.1 + The ability to add message dumpers is new to 2.1 + +After an `Extractor `_ has extracted all messages from +the templates, the dumpers are executed to dump the messages to a translation +file in a specific format. + +Symfony2 comes already with many dumpers: + +* :class:`Symfony\\Component\\Translation\\Dumper\\CsvFileDumper` +* :class:`Symfony\\Component\\Translation\\Dumper\\IcuResFileDumper` +* :class:`Symfony\\Component\\Translation\\Dumper\\IniFileDumper` +* :class:`Symfony\\Component\\Translation\\Dumper\\MoFileDumper` +* :class:`Symfony\\Component\\Translation\\Dumper\\PoFileDumper` +* :class:`Symfony\\Component\\Translation\\Dumper\\QtFileDumper` +* :class:`Symfony\\Component\\Translation\\Dumper\\XliffFileDumper` +* :class:`Symfony\\Component\\Translation\\Dumper\\YamlFileDumper` + +You can create your own dumper by extending +:class:`Symfony\\Component\\Translation\\DumperFileDumper` or implementing +:class:`Symfony\\Component\\Translation\\Dumper\\DumperInterface` and tagging +the service with ``translation.dumper``. The tag has one option: ``alias`` +This is the name that's used to determine which dumper should be used. + +.. configuration-block:: + + .. code-block:: yaml + + services: + acme_demo.translation.dumper.json: + class: Acme\DemoBundle\Translation\JsonFileDumper + tags: + - { name: translation.dumper, alias: json } + + .. code-block:: xml + + + + + + .. code-block:: php + + $container->register( + 'acme_demo.translation.dumper.json', + 'Acme\DemoBundle\Translation\JsonFileDumper' + ) + ->addTag('translation.dumper', array('alias' => 'json')); + .. _reference-dic-tags-twig-extension: twig.extension