1 | <?php |
---|
2 | |
---|
3 | function js_string($s, $double = false) { |
---|
4 | $q = $double? '"' : "'"; |
---|
5 | return $q.str_replace($q, "\\$q", $s).$q; |
---|
6 | } |
---|
7 | |
---|
8 | |
---|
9 | function translation2jsitem($key, $value) { |
---|
10 | if (false !== strpos($key, "\0") && false !== strpos($value, "\0")) { |
---|
11 | $value = '['.implode(',', array_map('js_string', explode("\0", $value))).']'; |
---|
12 | } else { |
---|
13 | $value = js_string(js_escape($value)); |
---|
14 | } |
---|
15 | $key = js_string($key); |
---|
16 | return "$key: $value"; |
---|
17 | } |
---|
18 | |
---|
19 | function translations2js(&$translations) { |
---|
20 | $res = array(); |
---|
21 | //grr, there is no map on both key and value |
---|
22 | foreach ($translations as $key => $value) { |
---|
23 | array_push($res, translation2jsitem($key, $value)); |
---|
24 | } |
---|
25 | return "{\n\t".implode(",\n\t", $res)."\n}"; |
---|
26 | } |
---|
27 | ?> |
---|