4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Intl\ResourceBundle\Writer
;
15 * Writes .txt resource bundles.
17 * The resulting files can be converted to binary .res files using the
18 * {@link \Symfony\Component\Intl\ResourceBundle\Transformer\BundleCompiler}.
20 * @author Bernhard Schussek <bschussek@gmail.com>
22 * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
24 class TextBundleWriter
implements BundleWriterInterface
29 public function write($path, $locale, $data)
31 $file = fopen($path.'/'.$locale.'.txt', 'w');
33 $this->writeResourceBundle($file, $locale, $data);
39 * Writes a "resourceBundle" node.
41 * @param resource $file The file handle to write to.
42 * @param string $bundleName The name of the bundle.
43 * @param mixed $value The value of the node.
45 * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
47 private function writeResourceBundle($file, $bundleName, $value)
49 fwrite($file, $bundleName);
51 $this->writeTable($file, $value, 0);
57 * Writes a "resource" node.
59 * @param resource $file The file handle to write to.
60 * @param mixed $value The value of the node.
61 * @param integer $indentation The number of levels to indent.
62 * @param Boolean $requireBraces Whether to require braces to be printed
65 * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
67 private function writeResource($file, $value, $indentation, $requireBraces = true)
70 $this->writeInteger($file, $value);
75 if (is_array($value)) {
76 if (count($value) === count(array_filter($value, 'is_int'))) {
77 $this->writeIntVector($file, $value, $indentation);
82 $keys = array_keys($value);
84 if (count($keys) === count(array_filter($keys, 'is_int'))) {
85 $this->writeArray($file, $value, $indentation);
90 $this->writeTable($file, $value, $indentation);
95 if (is_bool($value)) {
96 $value = $value ? 'true' : 'false';
99 $this->writeString($file, (string) $value, $requireBraces);
103 * Writes an "integer" node.
105 * @param resource $file The file handle to write to.
106 * @param integer $value The value of the node.
108 * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
110 private function writeInteger($file, $value)
112 fprintf($file, ':int{%d}', $value);
116 * Writes an "intvector" node.
118 * @param resource $file The file handle to write to.
119 * @param array $value The value of the node.
120 * @param integer $indentation The number of levels to indent.
122 * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
124 private function writeIntVector($file, array $value, $indentation)
126 fwrite($file, ":intvector{\n");
128 foreach ($value as $int) {
129 fprintf($file, "%s%d,\n", str_repeat(' ', $indentation +
1), $int);
132 fprintf($file, "%s}", str_repeat(' ', $indentation));
136 * Writes a "string" node.
138 * @param resource $file The file handle to write to.
139 * @param string $value The value of the node.
140 * @param Boolean $requireBraces Whether to require braces to be printed
143 * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
145 private function writeString($file, $value, $requireBraces = true)
147 if ($requireBraces) {
148 fprintf($file, '{"%s"}', $value);
153 fprintf($file, '"%s"', $value);
157 * Writes an "array" node.
159 * @param resource $file The file handle to write to.
160 * @param array $value The value of the node.
161 * @param integer $indentation The number of levels to indent.
163 * @see http://source.icu-project.org/repos/icu/icuhtml/trunk/design/bnf_rb.txt
165 private function writeArray($file, array $value, $indentation)
167 fwrite($file, "{\n");
169 foreach ($value as $entry) {
170 fwrite($file, str_repeat(' ', $indentation +
1));
172 $this->writeResource($file, $entry, $indentation +
1, false);
174 fwrite($file, ",\n");
177 fprintf($file, '%s}', str_repeat(' ', $indentation));
181 * Writes a "table" node.
183 * @param resource $file The file handle to write to.
184 * @param array $value The value of the node.
185 * @param integer $indentation The number of levels to indent.
187 private function writeTable($file, array $value, $indentation)
189 fwrite($file, "{\n");
191 foreach ($value as $key => $entry) {
192 fwrite($file, str_repeat(' ', $indentation +
1));
195 $this->writeResource($file, $entry, $indentation +
1);
200 fprintf($file, '%s}', str_repeat(' ', $indentation));