|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* BEdita, API-first content management framework |
|
6
|
|
|
* Copyright 2023 Atlas Srl, Chialab Srl |
|
7
|
|
|
* |
|
8
|
|
|
* This file is part of BEdita: you can redistribute it and/or modify |
|
9
|
|
|
* it under the terms of the GNU Lesser General Public License as published |
|
10
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
11
|
|
|
* (at your option) any later version. |
|
12
|
|
|
* |
|
13
|
|
|
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace BEdita\I18n\Filesystem; |
|
17
|
|
|
|
|
18
|
|
|
use Cake\Core\App; |
|
19
|
|
|
use Cake\Core\Plugin; |
|
20
|
|
|
use Cake\Utility\Hash; |
|
21
|
|
|
use Cake\View\View; |
|
22
|
|
|
|
|
23
|
|
|
class Ttag |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Extract ttag strings from javascript files |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $locales The locales |
|
29
|
|
|
* @param string $localePath The locale path |
|
30
|
|
|
* @param string|null $plugin The plugin name, if any |
|
31
|
|
|
* @return array |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function extract(array $locales, string $localePath, ?string $plugin = null): array |
|
34
|
|
|
{ |
|
35
|
|
|
$extracted = false; |
|
36
|
|
|
$info = []; |
|
37
|
|
|
|
|
38
|
|
|
// check ttag command exists |
|
39
|
|
|
$ttag = 'node_modules/ttag-cli/bin/ttag'; |
|
40
|
|
|
if (!file_exists($ttag)) { |
|
41
|
|
|
$info[] = sprintf('Skip javascript parsing - %s command not found', $ttag); |
|
42
|
|
|
|
|
43
|
|
|
return compact('extracted', 'info'); |
|
44
|
|
|
} |
|
45
|
|
|
// check template folder exists |
|
46
|
|
|
$appDir = !empty($plugin) ? Plugin::templatePath($plugin) : Hash::get(App::path(View::NAME_TEMPLATE), 0); |
|
47
|
|
|
if (!file_exists($appDir)) { |
|
|
|
|
|
|
48
|
|
|
$info[] = sprintf('Skip javascript parsing - %s folder not found', $appDir); |
|
49
|
|
|
|
|
50
|
|
|
return compact('extracted', 'info'); |
|
51
|
|
|
} |
|
52
|
|
|
// Path to the resources directory defined in cakephp app config/paths.php |
|
53
|
|
|
// Do not add RESOURCES path when it's a plugin |
|
54
|
|
|
if (empty($plugin) && defined('RESOURCES') && file_exists(RESOURCES)) { |
|
|
|
|
|
|
55
|
|
|
$appDir = sprintf('%s %s', $appDir, RESOURCES); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// do extract translation strings from js files using ttag |
|
59
|
|
|
$info[] = 'Extracting translation string from javascript files using ttag'; |
|
60
|
|
|
|
|
61
|
|
|
$defaultJs = sprintf('%s/default-js.pot', $localePath); |
|
62
|
|
|
foreach ($locales as $locale) { |
|
63
|
|
|
$lang = substr($locale, 0, 2); |
|
64
|
|
|
exec(sprintf('%s extract --extractLocation never --o %s --l %s %s', $ttag, $defaultJs, $lang, $appDir)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// merge default-js.pot and <plugin>.pot|default.pot |
|
68
|
|
|
$potFile = !empty($plugin) && is_string($plugin) ? sprintf('%s.pot', $plugin) : 'default.pot'; |
|
69
|
|
|
$default = sprintf('%s/%s', $localePath, $potFile); |
|
70
|
|
|
exec(sprintf('msgcat --use-first %s %s -o %s', $default, $defaultJs, $default)); |
|
71
|
|
|
|
|
72
|
|
|
// remove default-js.pot |
|
73
|
|
|
unlink($defaultJs); |
|
74
|
|
|
|
|
75
|
|
|
$extracted = true; |
|
76
|
|
|
|
|
77
|
|
|
return compact('extracted', 'info'); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|