/
l10n_scanner.php
153 lines (140 loc) · 5.23 KB
/
l10n_scanner.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
/**
* Scans all source code for messages that need to be localized.
*/
class l10n_scanner_Core {
// Based on Drupal's potx module, originally written by:
// Gbor Hojtsy http://drupal.org/user/4166
public static $cache;
static function process_message($message, &$cache) {
if (empty($cache)) {
foreach (db::build()
->select("key")
->from("incoming_translations")
->where("locale", "=", "root")
->execute() as $row) {
$cache[$row->key] = true;
}
}
$key = Gallery_I18n::get_message_key($message);
if (array_key_exists($key, $cache)) {
return $cache[$key];
}
$entry = ORM::factory("incoming_translation", array("key" => $key));
if (!$entry->loaded()) {
$entry->key = $key;
$entry->message = serialize($message);
$entry->locale = "root";
$entry->save();
}
}
static function scan_php_file($file, &$cache) {
$code = file_get_contents($file);
$raw_tokens = token_get_all($code);
unset($code);
$tokens = array();
$func_token_list = array("t" => array(), "t2" => array());
$token_number = 0;
// Filter out HTML / whitespace, and build a lookup for global function calls.
foreach ($raw_tokens as $token) {
if ((!is_array($token)) || (($token[0] != T_WHITESPACE) && ($token[0] != T_INLINE_HTML))) {
if (is_array($token)) {
if ($token[0] == T_STRING && in_array($token[1], array("t", "t2"))) {
$func_token_list[$token[1]][] = $token_number;
}
}
$tokens[] = $token;
$token_number++;
}
}
unset($raw_tokens);
if (!empty($func_token_list["t"])) {
l10n_scanner::_parse_t_calls($tokens, $func_token_list["t"], $cache);
}
if (!empty($func_token_list["t2"])) {
l10n_scanner::_parse_plural_calls($tokens, $func_token_list["t2"], $cache);
}
}
static function scan_info_file($file, &$cache) {
$info = new ArrayObject(parse_ini_file($file), ArrayObject::ARRAY_AS_PROPS);
foreach (array('name', 'description') as $property) {
if (isset($info->$property)) {
l10n_scanner::process_message($info->$property, $cache);
}
}
}
private static function _parse_t_calls(&$tokens, &$call_list, &$cache) {
foreach ($call_list as $index) {
$function_name = $tokens[$index++];
$parens = $tokens[$index++];
$first_param = $tokens[$index++];
$next_token = $tokens[$index];
if ($parens == "(") {
if (in_array($next_token, array(")", ","))
&& (is_array($first_param) && ($first_param[0] == T_CONSTANT_ENCAPSED_STRING))) {
$message = self::_escape_quoted_string($first_param[1]);
l10n_scanner::process_message($message, $cache);
} else {
// t() found, but inside is something which is not a string literal.
// @todo Call status callback with error filename/line.
}
}
}
}
private static function _parse_plural_calls(&$tokens, &$call_list, &$cache) {
foreach ($call_list as $index) {
$function_name = $tokens[$index++];
$parens = $tokens[$index++];
$first_param = $tokens[$index++];
$first_separator = $tokens[$index++];
$second_param = $tokens[$index++];
$next_token = $tokens[$index];
if ($parens == "(") {
if ($first_separator == "," && $next_token == ","
&& is_array($first_param) && $first_param[0] == T_CONSTANT_ENCAPSED_STRING
&& is_array($second_param) && $second_param[0] == T_CONSTANT_ENCAPSED_STRING) {
$singular = self::_escape_quoted_string($first_param[1]);
$plural = self::_escape_quoted_string($second_param[1]);
l10n_scanner::process_message(array("one" => $singular, "other" => $plural), $cache);
} else {
// t2() found, but inside is something which is not a string literal.
// @todo Call status callback with error filename/line.
}
}
}
}
/**
* Escape quotes in a strings depending on the surrounding
* quote type used.
*
* @param $str The strings to escape
*/
private static function _escape_quoted_string($str) {
$quo = substr($str, 0, 1);
$str = substr($str, 1, -1);
if ($quo == '"') {
$str = stripcslashes($str);
} else {
$str = strtr($str, array("\\'" => "'", "\\\\" => "\\"));
}
return $str;
}
}