@@ -5,28 +5,28 @@ Custom Formats
5
5
==============
6
6
7
7
Sometimes, you need to deal with custom formats for translation files. The
8
- Translation component is flexible enough to support this, just creating a
8
+ Translation component is flexible enough to support this. Just create a
9
9
loader (to load translations) and, optionally, a dumper (to dump translations).
10
10
11
- Let's imagine you have a custom format where translation messages are defined
12
- using one line for each translation and parenthesis to wrap the key and the
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
13
message. A translation file would look like this:
14
14
15
15
.. code-block :: text
16
16
17
17
(welcome)(Bienvenido)
18
- (goodbye)(Adios )
18
+ (goodbye)(Adiós )
19
19
(hello)(Hola)
20
20
21
21
Custom Loader
22
22
-------------
23
23
24
- To define a custom loader able to read this kind of files, you must create a
24
+ To define a custom loader that is able to read this kind of files, you must create a
25
25
new class that implements the
26
26
:class: `Symfony\\ Component\\ Translation\\ Loader\\ LoaderInterface `. The
27
27
:method: `Symfony\\ Component\\ Translation\\ Loader\\ LoaderInterface::load `
28
28
method will get a filename and parse it into an array. Then, it will
29
- create the catalogue that will be returned::
29
+ create the catalog that will be returned::
30
30
31
31
use Symfony\Component\Translation\MessageCatalogue;
32
32
use Symfony\Component\Translation\Loader\LoaderInterface;
@@ -54,6 +54,8 @@ create the catalogue that will be returned::
54
54
55
55
Once created, it can be used as any other loader::
56
56
57
+ use Symfony\Component\Translation\Translator;
58
+
57
59
$translator = new Translator('es_ES');
58
60
$translator->addLoader('my_format', new MyFormatLoader());
59
61
@@ -66,10 +68,11 @@ It will print *"Bienvenido"*.
66
68
Custom Dumper
67
69
-------------
68
70
69
- It is also possible to create a custom dumper for your format, useful when using
70
- the extraction commands. To do so, a new class implementing the
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
71
74
:class: `Symfony\\ Component\\ Translation\\ Dumper\\ DumperInterface `
72
- interface must be created.
75
+ must be created.
73
76
To write the dump contents into a file, extending the
74
77
:class: `Symfony\\ Component\\ Translation\\ Dumper\\ FileDumper ` class
75
78
will save a few lines::
@@ -79,8 +82,7 @@ will save a few lines::
79
82
80
83
class MyFormatDumper extends FileDumper
81
84
{
82
-
83
- public function format(MessageCatalogue $messages, $domain = 'messages')
85
+ protected function format(MessageCatalogue $messages, $domain = 'messages')
84
86
{
85
87
$output = '';
86
88
@@ -100,15 +102,12 @@ will save a few lines::
100
102
The :method: `Symfony\\ Component\\ Translation\\ Dumper\\ FileDumper::format `
101
103
method creates the output string, that will be used by the
102
104
:method: `Symfony\\ Component\\ Translation\\ Dumper\\ FileDumper::dump ` method
103
- of the :class: `Symfony\\ Component\\ Translation\\ Dumper\\ FileDumper ` class to
104
- create the file. The dumper can be used like any other
105
- built-in dumper. In this example, the translation messages defined in the YAML file
106
- are dumped into a text file with the custom format::
105
+ of the FileDumper class to create the file. The dumper can be used like any other
106
+ built-in dumper. In the following example, the translation messages defined in the
107
+ YAML file are dumped into a text file with the custom format::
107
108
108
109
use Symfony\Component\Translation\Loader\YamlFileLoader;
109
110
110
- include_once __DIR__. '/vendor/autoload.php';
111
-
112
111
$loader = new YamlFileLoader();
113
112
$catalogue = $loader->load(__DIR__ . '/translations/messages.es_ES.yml' , 'es_ES');
114
113
0 commit comments