summaryrefslogtreecommitdiffstats
path: root/modules/translation/library/Translation/Util/GettextTranslationHelper.php
blob: 043b2b73c827577f780425f9367b3319ed38a7bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Translation\Util;

use Exception;
use Icinga\Application\ApplicationBootstrap;
use Icinga\Application\Config;
use Icinga\Application\Modules\Manager;
use Icinga\Exception\IcingaException;
use Icinga\Util\File;

/**
 * This class provides some useful utility functions to handle gettext translations
 */
class GettextTranslationHelper
{
    /**
     * All project files are supposed to have the same/this encoding
     */
    const FILE_ENCODING = 'UTF-8';

    /**
     * Config
     *
     * @var Config
     */
    protected $config;

    /**
     * The source files to parse
     *
     * @var array
     */
    private $sourceExtensions = array(
        'php',
        'phtml'
    );

    /**
     * The module manager of the application's bootstrap
     *
     * @var Manager
     */
    private $moduleMgr;

    /**
     * The current version of Icingaweb 2 or of the module the catalog is being created for
     *
     * @var string
     */
    private $version;

    /**
     * The name of the module if any
     *
     * @var string
     */
    private $moduleName;

    /**
     * The locale used by this helper
     *
     * @var string
     */
    private $locale;

    /**
     * The path to the module, if any
     *
     * @var string
     */
    private $moduleDir;

    /**
     * The path to the file catalog
     *
     * @var string
     */
    private $catalogPath;

    /**
     * The path to the *.pot file
     *
     * @var string
     */
    private $templatePath;

    /**
     * The path to the *.po file
     *
     * @var string
     */
    private $tablePath;

    /**
     * Create a new TranslationHelper object
     *
     * @param   ApplicationBootstrap    $bootstrap  The application's bootstrap object
     * @param   string                  $locale     The locale to be used by this helper
     */
    public function __construct(ApplicationBootstrap $bootstrap, $locale)
    {
        $this->moduleMgr = $bootstrap->getModuleManager();
        $this->locale = $locale;
    }

    /**
     * Cleanup temporary files
     */
    public function __destruct()
    {
        if ($this->catalogPath !== null && file_exists($this->catalogPath)) {
            unlink($this->catalogPath);
        }

        if ($this->templatePath !== null && file_exists($this->templatePath)) {
            unlink($this->templatePath);
        }
    }

    /**
     * Get the config
     *
     * @return Config
     */
    public function getConfig()
    {
        return $this->config;
    }

    /**
     * Set the config
     *
     * @param   Config $config
     *
     * @return  $this
     */
    public function setConfig(Config $config)
    {
        $this->config = $config;
        return $this;
    }

    /**
     * Update the translation table for a particular module
     *
     * @param   string      $module     The name of the module for which to update the translation table
     */
    public function updateModuleTranslations($module)
    {
        $this->catalogPath = tempnam(sys_get_temp_dir(), 'IcingaTranslation_');
        $this->templatePath = tempnam(sys_get_temp_dir(), 'IcingaPot_');
        $this->version = $this->moduleMgr->getModule($module)->getVersion();
        $this->moduleName = $this->moduleMgr->getModule($module)->getName();

        $this->moduleDir = $this->moduleMgr->getModuleDir($module);
        $this->tablePath = implode(
            DIRECTORY_SEPARATOR,
            array(
                $this->moduleDir,
                'application',
                'locale',
                $this->locale,
                'LC_MESSAGES',
                $module . '.po'
            )
        );

        $this->createFileCatalog();
        $this->createTemplateFile();
        $this->updateTranslationTable();
    }

    /**
     * Compile the translation table for a particular module
     *
     * @param   string      $module     The name of the module for which to compile the translation table
     */
    public function compileModuleTranslation($module)
    {
        $this->moduleDir = $this->moduleMgr->getModuleDir($module);
        $this->tablePath = implode(
            DIRECTORY_SEPARATOR,
            array(
                $this->moduleDir,
                'application',
                'locale',
                $this->locale,
                'LC_MESSAGES',
                $module . '.po'
            )
        );

        $this->compileTranslationTable();
    }

    /**
     * Update any existing or create a new translation table using the gettext tools
     *
     * @throws  Exception   In case the translation table does not yet exist and cannot be created
     */
    private function updateTranslationTable()
    {
        if (is_file($this->tablePath)) {
            shell_exec(sprintf(
                '%s --update --backup=none %s %s 2>&1',
                $this->getConfig()->get('translation', 'msgmerge', '/usr/bin/env msgmerge'),
                $this->tablePath,
                $this->templatePath
            ));
        } else {
            if ((!is_dir(dirname($this->tablePath)) && !@mkdir(dirname($this->tablePath), 0755, true)) ||
                !rename($this->templatePath, $this->tablePath)) {
                throw new IcingaException(
                    'Unable to create %s',
                    $this->tablePath
                );
            }
        }
        $this->updateHeader($this->tablePath);
        $this->fixSourceLocations($this->tablePath);
    }

    /**
     * Create the template file using the gettext tools
     */
    private function createTemplateFile()
    {
        shell_exec(
            implode(
                ' ',
                array(
                    $this->getConfig()->get('translation', 'xgettext', '/usr/bin/env xgettext'),
                    '--language=PHP',
                    '--keyword=translate',
                    '--keyword=translate:1,2c',
                    '--keyword=translateInDomain:2',
                    '--keyword=translateInDomain:2,3c',
                    '--keyword=translatePlural:1,2',
                    '--keyword=translatePlural:1,2,4c',
                    '--keyword=translatePluralInDomain:2,3',
                    '--keyword=translatePluralInDomain:2,3,5c',
                    '--keyword=mt:2',
                    '--keyword=mt:2,3c',
                    '--keyword=mtp:2,3',
                    '--keyword=mtp:2,3,5c',
                    '--keyword=t',
                    '--keyword=t:1,2c',
                    '--keyword=tp:1,2',
                    '--keyword=tp:1,2,4c',
                    '--keyword=N_',
                    '--sort-output',
                    '--force-po',
                    '--omit-header',
                    '--from-code=' . self::FILE_ENCODING,
                    '--files-from="' . $this->catalogPath . '"',
                    '--output="' . $this->templatePath . '"'
                )
            )
        );
    }

    /**
     * Create or update a gettext conformant header in the given file
     *
     * @param   string  $path   The path to the file
     */
    private function updateHeader($path)
    {
        $headerInfo = array(
            'title' => $this->moduleMgr->getModule($this->moduleName)->getTitle(),
            'copyright_holder' => 'TEAM NAME',
            'copyright_year' => date('Y'),
            'author_name' => 'FIRST AUTHOR',
            'author_mail' => 'EMAIL@ADDRESS',
            'author_year' => 'YEAR',
            'project_name' => ucfirst($this->moduleName) . ' Module',
            'project_version' => $this->version,
            'project_bug_mail' => 'ISSUE TRACKER',
            'pot_creation_date' => date('Y-m-d H:iO'),
            'po_revision_date' => 'YEAR-MO-DA HO:MI+ZONE',
            'translator_name' => 'FULL NAME',
            'translator_mail' => 'EMAIL@ADDRESS',
            'language'  => $this->locale,
            'language_team_name' => 'LANGUAGE',
            'language_team_url' => 'LL@li.org',
            'charset' => self::FILE_ENCODING
        );

        $content = file_get_contents($path);
        if (strpos($content, '# ') === 0) {
            $authorInfo = array();
            if (preg_match('@# (.+) <(.+)>, (\d+|YEAR)\.@', $content, $authorInfo)) {
                $headerInfo['author_name'] = $authorInfo[1];
                $headerInfo['author_mail'] = $authorInfo[2];
                $headerInfo['author_year'] = $authorInfo[3];
            }
            $revisionInfo = array();
            if (preg_match('@Revision-Date: (\d{4}-\d{2}-\d{2} \d{2}:\d{2}\+\d{4})@', $content, $revisionInfo)) {
                $headerInfo['po_revision_date'] = $revisionInfo[1];
            }
            $translatorInfo = array();
            if (preg_match('@Last-Translator: (.+) <(.+)>@', $content, $translatorInfo)) {
                $headerInfo['translator_name'] = $translatorInfo[1];
                $headerInfo['translator_mail'] = $translatorInfo[2];
            }
            $languageTeamInfo = array();
            if (preg_match('@Language-Team: (.+) <(.+)>@', $content, $languageTeamInfo)) {
                $headerInfo['language_team_name'] = $languageTeamInfo[1];
                $headerInfo['language_team_url'] = $languageTeamInfo[2];
            }
            $languageInfo = array();
            if (preg_match('@Language: ([a-z]{2}_[A-Z]{2})@', $content, $languageInfo)) {
                $headerInfo['language'] = $languageInfo[1];
            }
        }

        file_put_contents(
            $path,
            implode(
                PHP_EOL,
                array(
                    '# ' . $headerInfo['title'] . '.',
                    '# Copyright (C) ' . $headerInfo['copyright_year'] . ' ' . $headerInfo['copyright_holder'],
                    '# This file is distributed under the same license as ' . $headerInfo['project_name'] . '.',
                    '# ' . $headerInfo['author_name'] . ' <' . $headerInfo['author_mail']
                    . '>, ' . $headerInfo['author_year'] . '.',
                    '# ',
                    '#, fuzzy',
                    'msgid ""',
                    'msgstr ""',
                    '"Project-Id-Version: ' . $headerInfo['project_name'] . ' ('
                    . $headerInfo['project_version'] . ')\n"',
                    '"Report-Msgid-Bugs-To: ' . $headerInfo['project_bug_mail'] . '\n"',
                    '"POT-Creation-Date: ' . $headerInfo['pot_creation_date'] . '\n"',
                    '"PO-Revision-Date: ' . $headerInfo['po_revision_date'] . '\n"',
                    '"Last-Translator: ' . $headerInfo['translator_name'] . ' <'
                    . $headerInfo['translator_mail'] . '>\n"',
                    '"Language: ' . $headerInfo['language'] . '\n"',
                    '"Language-Team: ' . $headerInfo['language_team_name'] . ' <'
                    . $headerInfo['language_team_url'] . '>\n"',
                    '"MIME-Version: 1.0\n"',
                    '"Content-Type: text/plain; charset=' . $headerInfo['charset'] . '\n"',
                    '"Content-Transfer-Encoding: 8bit\n"',
                    '"Plural-Forms: nplurals=2; plural=(n != 1);\n"',
                    '"X-Poedit-Basepath: .\n"',
                    '"X-Poedit-SearchPath-0: .\n"',
                    ''
                )
            ) . PHP_EOL . substr($content, strpos($content, '#: '))
        );
    }

    /**
     * Adjust all absolute source file paths so that they're all relative to the catalog's location
     *
     * @param   string  $path
     */
    protected function fixSourceLocations($path)
    {
        shell_exec(sprintf(
            "sed -i 's;%s;../../../..;g' %s",
            $this->moduleDir,
            $path
        ));
    }

    /**
     * Create the file catalog
     *
     * @throws  Exception   In case the catalog-file cannot be created
     */
    private function createFileCatalog()
    {
        $catalog = new File($this->catalogPath, 'w');

        try {
            $this->getSourceFileNames($this->moduleDir, $catalog);
        } catch (Exception $error) {
            throw $error;
        }

        $catalog->fflush();
    }

    /**
     * Recursively scan the given directory for translatable source files
     *
     * @param   string      $directory      The directory where to search for sources
     * @param   File        $file           The file where to write the results
     *
     * @throws  Exception                   In case the given directory is not readable
     */
    private function getSourceFileNames($directory, File $file)
    {
        $directoryHandle = opendir($directory);
        if (!$directoryHandle) {
            throw new IcingaException(
                'Unable to read files from %s',
                $directory
            );
        }

        $subdirs = array();
        while (($filename = readdir($directoryHandle)) !== false) {
            if ($filename[0] === '.' || $filename === 'vendor') {
                continue;
            }
            $filepath = $directory . DIRECTORY_SEPARATOR . $filename;
            if (preg_match('@^[^\.].+\.(' . implode('|', $this->sourceExtensions) . ')$@', $filename)) {
                $file->fwrite($filepath . PHP_EOL);
            } elseif (! is_link($filepath) && is_dir($filepath)) {
                $subdirs[] = $filepath;
            }
        }
        closedir($directoryHandle);

        foreach ($subdirs as $subdir) {
            $this->getSourceFileNames($subdir, $file);
        }
    }

    /**
     * Compile the translation table
     */
    private function compileTranslationTable()
    {
        $targetPath = substr($this->tablePath, 0, strrpos($this->tablePath, '.')) . '.mo';
        shell_exec(
            implode(
                ' ',
                array(
                    $this->getConfig()->get('translation', 'msgfmt', '/usr/bin/env msgfmt'),
                    '-o ' . $targetPath,
                    $this->tablePath
                )
            )
        );
    }
}