Skip to content

Commit 8b6c584

Browse files
committed
Added custom format page to translation component
1 parent 2c93aa5 commit 8b6c584

File tree

2 files changed

+113
-0
lines changed

2 files changed

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

components/translation/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ Translation
66

77
introduction
88
usage
9+
custom_formats

0 commit comments

Comments
 (0)