1
|
|
|
<?php |
2
|
|
|
namespace SilverStripe\Omnipay\UI\Tasks; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Creates JavaScript files ready for consumption by framework/javascript/i18n.js, |
6
|
|
|
* based on source files in JSON format. This is necessary in order to support |
7
|
|
|
* translations in a format which our collaborative translation service (Transifex) |
8
|
|
|
* supports, while retaining the ability to combine JavaScript files in SilverStripe |
9
|
|
|
* without resorting to JSONP or other means of processing raw JSON files. |
10
|
|
|
*/ |
11
|
|
|
class GenerateJavascriptI18nTask extends \BuildTask |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
private $modulePath; |
15
|
|
|
private $sourceDir = 'javascript/lang/src'; |
16
|
|
|
private $targetDir = 'javascript/lang'; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
parent::__construct(); |
21
|
|
|
$this->modulePath = BASE_PATH . '/omnipay-ui'; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function setModulePath($modulePath) |
25
|
|
|
{ |
26
|
|
|
$this->modulePath = $modulePath; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getTemplate() |
30
|
|
|
{ |
31
|
|
|
$tmpl = <<<TMPL |
32
|
|
|
// This file was generated by GenerateJavaScriptI18nTask from %FILE%. |
33
|
|
|
// See https://github.com/silverstripe/silverstripe-buildtools for details |
34
|
|
|
if(typeof(ss) == 'undefined' || typeof(ss.i18n) == 'undefined') { |
35
|
|
|
if(typeof(console) != 'undefined') console.error('Class ss.i18n not defined'); |
36
|
|
|
} else { |
37
|
|
|
ss.i18n.addDictionary('%LOCALE%', %TRANSLATIONS%); |
38
|
|
|
} |
39
|
|
|
TMPL; |
40
|
|
|
return $tmpl; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param \SS_HTTPRequest $request |
45
|
|
|
* @throws \Exception |
46
|
|
|
*/ |
47
|
|
|
public function run($request) |
48
|
|
|
{ |
49
|
|
|
$ds = DIRECTORY_SEPARATOR; |
50
|
|
|
if ($request->getVar('module')) { |
51
|
|
|
$this->setModulePath(BASE_PATH . $ds . $request->getVar('module')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!is_dir($this->modulePath)) { |
55
|
|
|
throw new \Exception("Invalid module path: $this->modulePath"); |
56
|
|
|
} |
57
|
|
|
$iterator = new \GlobIterator( |
58
|
|
|
$this->modulePath . $ds . $this->sourceDir . $ds . '*.json' |
59
|
|
|
); |
60
|
|
|
foreach ($iterator as $item) { |
61
|
|
|
$translations = file_get_contents($item->getPathName()); |
62
|
|
|
$locale = preg_replace('/\.json/', '', $item->getFilename()); |
63
|
|
|
$targetPath = $this->modulePath . $ds . $this->targetDir . $ds . $locale . '.js'; |
64
|
|
|
echo "Generating $targetPath\n"; |
65
|
|
|
file_put_contents( |
66
|
|
|
$targetPath, |
67
|
|
|
str_replace( |
68
|
|
|
array( |
69
|
|
|
'%TRANSLATIONS%', |
70
|
|
|
'%FILE%', |
71
|
|
|
'%LOCALE%' |
72
|
|
|
), |
73
|
|
|
array( |
74
|
|
|
$translations, |
75
|
|
|
$this->sourceDir . $ds . $item->getFilename(), |
76
|
|
|
$locale |
77
|
|
|
), |
78
|
|
|
$this->getTemplate() |
79
|
|
|
) |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|