-
Notifications
You must be signed in to change notification settings - Fork 1
/
translateFromTpl.php
88 lines (73 loc) · 2.03 KB
/
translateFromTpl.php
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
<?php
require_once '../civicrm.config.php';
function doTranslate($params){
if (genDoc::$_smarty) {
$smarty = genDoc::$_smarty;
}
else {
$smarty = prepareSmarty($params);
}
$result = $smarty->fetch($params['templatesFilePath']);
$params['content'] = $result;
return $params;
}
function prepareSmarty($params) {
chdir(__DIR__);
require_once 'Smarty/Smarty.class.php';
require_once 'CRM/Core/Smarty/resources/String.php';
require_once 'CRM/Core/Smarty/plugins/block.localize.php';
$smarty = new Smarty();
$smarty->template_dir = $params['templatesDir'];
$smarty->plugins_dir = array('../packages/Smarty/plugins', '../CRM/Core/Smarty/plugins');
$smarty->compile_dir = createTempDir();
$smarty->clear_all_cache();
$smarty->register_block('localize', 'smarty_block_localize');
$smarty->assign('locale', 'en');
$smarty->assign('tsLocale', 'zh-TW');
$smarty->register_resource('string', array(
'civicrm_smarty_resource_string_get_template',
'civicrm_smarty_resource_string_get_timestamp',
'civicrm_smarty_resource_string_get_secure',
'civicrm_smarty_resource_string_get_trusted',
)
);
return $smarty;
}
function prepareLocalTranslate() {
$strings = array(
'Request Example' => '傳送範例',
'Response Example' => '回傳範例',
);
return array(
'zh_TW'=> array(
'enabled' => array(
'exactMatch' => $strings,
),
),
);
}
function createTempDir($prefix = '') {
if (isset($_SERVER['TMPDIR'])) {
$tempDir = $_SERVER['TMPDIR'];
}
else {
$tempDir = '/tmp';
}
$newTempDir = $tempDir . '/' . $prefix . rand(1, 10000);
if (file_exists($newTempDir)) {
removeDir($newTempDir);
}
createDir($newTempDir);
return $newTempDir;
}
function removeDir($dir) {
foreach (glob("$dir/*") as $tempFile) {
unlink($tempFile);
}
rmdir($dir);
}
function createDir($dir, $perm = 0755) {
if (!is_dir($dir)) {
mkdir($dir, $perm, TRUE);
}
}