Skip to content

Commit de0e355

Browse files
committed
Merge branch '2.4' into 2.5
* 2.4: (30 commits) [#4166] Fixing small typo caution on `null` values in Expression constraint Fixed all the errors found by Ryan Removed two highlight formats which are "experimental" and not used by end users Reworded the explanation about the limitation of enablin PHP templates Improved the explanation thanks to @stof comments More and more fixes and improvements Added another bunch of fixes suggested by reviewers Added lots of fixes suggested by reviewers Added a note about not using relative internal links in the doc Switched another relative link into an absolute reference lways use absolute links instead of relative for internal doc links Added missing link Revamped the documentation about "Contributing Docs" do not reference services in parameters Fix reference label Add label book-security-roles Add formatting, links, and clarity Added a note about the side effects of enabling both PHP and Twig Caution that roles should start with ROLE_ ...
2 parents f732747 + c570ee3 commit de0e355

File tree

15 files changed

+525
-289
lines changed

15 files changed

+525
-289
lines changed

book/security.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1782,6 +1782,8 @@ the default for the firewall as a whole).
17821782
For more information about user provider and firewall configuration, see
17831783
the :doc:`/reference/configuration/security`.
17841784

1785+
.. _book-security-roles:
1786+
17851787
Roles
17861788
-----
17871789

components/dependency_injection/parameters.rst

-73
Original file line numberDiff line numberDiff line change
@@ -336,76 +336,3 @@ To disable this behavior, use the ``string`` type:
336336

337337
This is not available for YAML and PHP, because they already have built-in
338338
support for the PHP keywords.
339-
340-
Syntax for Referencing Services
341-
-------------------------------
342-
343-
You can of course also reference services, which looks a bit different in
344-
each format. You can configure the behavior if the referenced service does
345-
not exist. By default, an exception is thrown when a non-existent service
346-
is referenced.
347-
348-
YAML
349-
~~~~
350-
351-
Start the string with ``@`` or ``@?`` to reference a service in YAML.
352-
353-
* ``@mailer`` references the ``mailer`` service. If the service does not
354-
exist, an exception will be thrown;
355-
* ``@?mailer`` references the ``mailer`` service. If the service does not
356-
exist, it will be ignored;
357-
358-
.. code-block:: yaml
359-
360-
parameters:
361-
# if 'my_mailer' service isn't defined, an exception will be raised
362-
foo: @my_mailer
363-
364-
# if 'my_logger' service isn't defined, 'bar' will be null
365-
bar: @?my_logger
366-
367-
.. tip::
368-
369-
Use ``@@`` to escape the ``@`` symbol in YAML. ``@@mailer`` will be
370-
converted into the string ``"@mailer"`` instead of referencing the
371-
``mailer`` service.
372-
373-
XML
374-
~~~
375-
376-
In XML, use the ``service`` type. The behavior if the service does not exist
377-
can be specified using the ``on-invalid`` argument. By default, an exception
378-
is thrown. Valid values for ``on-invalid`` are ``null`` (uses ``null`` in place
379-
of the missing service) or ``ignored`` (very similar, except if used on a
380-
method call, the method call is removed).
381-
382-
.. code-block:: xml
383-
384-
<parameters>
385-
<!-- if 'my_mailer' service isn't defined, an exception will be raised -->
386-
<parameter key="foo" type="service" id="my_mailer" />
387-
388-
<!-- if 'my_logger' service isn't defined, 'bar' will be null -->
389-
<parameter key="bar" type="service" id="my_logger" on-invalid="null" />
390-
</parameters>
391-
392-
PHP
393-
~~~
394-
395-
In PHP, you can use the
396-
:class:`Symfony\\Component\\DependencyInjection\\Reference` class to reference
397-
a service. The invalid behavior is configured using the second constructor
398-
argument and constants from
399-
:class:`Symfony\\Component\\DependencyInjection\\ContainerInterface`.
400-
401-
.. code-block:: php
402-
403-
use Symfony\Component\DependencyInjection\Reference;
404-
405-
// if 'my_mailer' service isn't defined, an exception will be raised
406-
$container->setParameter('foo', new Reference('my_mailer'));
407-
408-
// if 'my_logger' service isn't defined, 'bar' will be null
409-
$container->setParameter('bar', new Reference('my_logger',
410-
ContainerInterface::NULL_ON_INVALID_REFERENCE
411-
));

components/map.rst.inc

+1
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141

142142
* :doc:`/components/translation/introduction`
143143
* :doc:`/components/translation/usage`
144+
* :doc:`/components/translation/custom_formats`
144145

145146
* :doc:`/components/yaml/index`
146147

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
.. index::
2+
single: Translation; Adding Custom Format Support
3+
4+
Adding Custom Format Support
5+
============================
6+
7+
Sometimes, you need to deal with custom formats for translation files. The
8+
Translation component is flexible enough to support this. Just create a
9+
loader (to load translations) and, optionally, a dumper (to dump translations).
10+
11+
Imagine that you have a custom format where translation messages are defined
12+
using one line for each translation and parentheses to wrap the key and the
13+
message. A translation file would look like this:
14+
15+
.. code-block:: text
16+
17+
(welcome)(accueil)
18+
(goodbye)(au revoir)
19+
(hello)(bonjour)
20+
21+
Creating a Custom Loader
22+
------------------------
23+
24+
To define a custom loader that is able to read these kinds of files, you must create a
25+
new class that implements the
26+
:class:`Symfony\\Component\\Translation\\Loader\\LoaderInterface`. The
27+
:method:`Symfony\\Component\\Translation\\Loader\\LoaderInterface::load`
28+
method will get a filename and parse it into an array. Then, it will
29+
create the catalog that will be returned::
30+
31+
use Symfony\Component\Translation\MessageCatalogue;
32+
use Symfony\Component\Translation\Loader\LoaderInterface;
33+
34+
class MyFormatLoader implements LoaderInterface
35+
{
36+
public function load($resource, $locale, $domain = 'messages')
37+
{
38+
$messages = array();
39+
$lines = file($resource);
40+
41+
foreach ($lines as $line) {
42+
if (preg_match('/\(([^\)]+)\)\(([^\)]+)\)/', $line, $matches)) {
43+
$messages[$matches[1]] = $matches[2];
44+
}
45+
}
46+
47+
$catalogue = new MessageCatalogue($locale);
48+
$catalogue->add($messages, $domain);
49+
50+
return $catalogue;
51+
}
52+
53+
}
54+
55+
Once created, it can be used as any other loader::
56+
57+
use Symfony\Component\Translation\Translator;
58+
59+
$translator = new Translator('fr_FR');
60+
$translator->addLoader('my_format', new MyFormatLoader());
61+
62+
$translator->addResource('my_format', __DIR__.'/translations/messages.txt', 'fr_FR');
63+
64+
echo $translator->trans('welcome');
65+
66+
It will print *"accueil"*.
67+
68+
Creating a Custom Dumper
69+
------------------------
70+
71+
It is also possible to create a custom dumper for your format, which is
72+
useful when using the extraction commands. To do so, a new class
73+
implementing the
74+
:class:`Symfony\\Component\\Translation\\Dumper\\DumperInterface`
75+
must be created. To write the dump contents into a file, extending the
76+
:class:`Symfony\\Component\\Translation\\Dumper\\FileDumper` class
77+
will save a few lines::
78+
79+
use Symfony\Component\Translation\MessageCatalogue;
80+
use Symfony\Component\Translation\Dumper\FileDumper;
81+
82+
class MyFormatDumper extends FileDumper
83+
{
84+
protected function format(MessageCatalogue $messages, $domain = 'messages')
85+
{
86+
$output = '';
87+
88+
foreach ($messages->all($domain) as $source => $target) {
89+
$output .= sprintf("(%s)(%s)\n", $source, $target);
90+
}
91+
92+
return $output;
93+
}
94+
95+
protected function getExtension()
96+
{
97+
return 'txt';
98+
}
99+
}
100+
101+
The :method:`Symfony\\Component\\Translation\\Dumper\\FileDumper::format`
102+
method creates the output string, that will be used by the
103+
:method:`Symfony\\Component\\Translation\\Dumper\\FileDumper::dump` method
104+
of the FileDumper class to create the file. The dumper can be used like any other
105+
built-in dumper. In the following example, the translation messages defined in the
106+
YAML file are dumped into a text file with the custom format::
107+
108+
use Symfony\Component\Translation\Loader\YamlFileLoader;
109+
110+
$loader = new YamlFileLoader();
111+
$catalogue = $loader->load(__DIR__ . '/translations/messages.fr_FR.yml' , 'fr_FR');
112+
113+
$dumper = new MyFormatDumper();
114+
$dumper->dump($catalogue, array('path' => __DIR__.'/dumps'));

components/translation/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Translation
66

77
introduction
88
usage
9+
custom_formats

components/translation/introduction.rst

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ Loader too. The default loaders are:
9595

9696
All file loaders require the :doc:`Config component </components/config/index>`.
9797

98+
You can also :doc:`create your own Loader </components/translation/custom_formats>`,
99+
in case the format is not already supported by one of the default loaders.
100+
98101
At first, you should add one or more loaders to the ``Translator``::
99102

100103
// ...

0 commit comments

Comments
 (0)