-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsi18n.php
93 lines (84 loc) · 1.91 KB
/
jsi18n.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
89
90
91
92
93
<?php
/**
* Javascript i18n Helper
*
* @author Joshua Chi
**/
class Jsi18nHelper extends AppHelper
{
var $helpers = array('Javascript');
/**
* html tags used by this helper.
*
* @var array
*/
var $tags = array(
'javascriptblock' => '<script type="text/javascript">%s</script>',
'javascriptstart' => '<script type="text/javascript">',
'javascriptlink' => '<script type="text/javascript" src="%s"></script>',
'javascriptend' => '</script>',
);
/**
* The Filename to store Cached JS i18n files to.
*
* @access private
* @var string
**/
var $__inJSCachePath = null;
/**
* strtotime Compatible Time Expression for lifetime of cached JS files
*
* @var string
**/
var $__cacheTime = '+99 days';
/**
* Initialize i18n languages
*
* @var array
**/
var $langs = array('en_us','zh_cn');
/**
* Name the cache file name you like
*
* @var string
**/
var $jsi18nFolder = 'jsi18n';
/**
* construct the helper and include JSMin.
*
* @return void
**/
function __construct(){
parent::__construct();
$this->__inJSCachePath = $this->jsi18nFolder.DS;
}
function __writeJSi18nCache($path, $content) {
if (!is_dir(dirname(CACHE . $path))) {
mkdir(dirname(CACHE . $path));
}
$r = cache($path, $content, $this->__cacheTime);
}
function __makeCleanJSi18n($data) {
$output = $this->Javascript->object($data);
return $output;
}
function link($jsfile){
if(!$jsfile){
return;
}
$cachepath = $this->__inJSCachePath.$jsfile;
if (!($output = cache($cachepath, null, $this->__cacheTime))) {
$output = '';
foreach($this->langs as $lang){
$output .= "var {$lang}_lang = ";
include_once(APP . 'locale' . DS . $lang . DS . 'lang.php');
$output .= $this->__makeCleanJSi18n($lang);
$output .= "; ";
}
$this->__writeJSi18nCache($cachepath, $output);
}
$out = sprintf($this->tags['javascriptblock'], $output);
return $out;
}
}
?>