Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 141 lines (119 sloc) 4.592 kb
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
1 #!/usr/bin/env php
2 <?php
3 /**
4 * ------------------------------------------------------------------------- *
5 * This library is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU Lesser General Public *
7 * License as published by the Free Software Foundation; either *
8 * version 2.1 of the License, or (at your option) any later version. *
9 * *
10 * This library is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * Lesser General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Lesser General Public *
16 * License along with this library; if not, write to the Free Software *
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
18 * ------------------------------------------------------------------------- *
19 *
20 * lang_extract.php - extracts gettext strings from ExponentCMS files
21 * adapted from http://smarty-gettext.sf.net/ Sagi Bashari <sagi@boom.org.il>
22 *
23 * Usage:
24 * ./lang_extract.php <filename or directory> <file2> <..>
25 *
26 * If a parameter is a directory, the files within will be parsed.
27 *
28 */
29
a0d11f3 @dleffler Minor language updates to core controllers, models, models-1, conf/data,...
dleffler authored
30 define('DEVELOPMENT','1');
31 define('WRITE_LANG_TEMPLATE', DEVELOPMENT);
32
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
33 // Initialize the exponent environment
34 include_once('../exponent_bootstrap.php');
35 // Initialize the language subsystem
36 expLang::loadLang();
37 global $default_lang, $cur_lang;
38 if (empty($default_lang)) $default_lang = include(BASE."framework/core/lang/English - US.php");
39
40 // regex for the gettext smarty modifier
41 $regex_gettext_mod='/(?<=["\'])((\\\\.|[^\'"])*)(?=["\']\|gettext)/';
42
43 // regex for the gettext smarty function
44 $regex_gettext_func='/(?<=gettext str=[\'"])((\\\\.|[^\'"])*)([^}]*)(?=[\'"]\})/';
45
46 // regex for the gettext gt shortcut function
47 $regex_gt='/(?<=gt\([\'"])((\\\\.|[^\'"])*)(?=[\'"]\))/';
48
49 // extensions of files, used when going through a directory
50 $extensions = array('tpl','php');
51
7a084d8 @dleffler Some language updates [#296]
dleffler authored
52 $recur = true;
53
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
54 $total_new = 0;
55
56 // "fix" string - strip slashes, escape and convert new lines to \n
57 function fs($str) {
58 $str = stripslashes($str);
59 $str = str_replace('"', '\"', $str);
60 $str = str_replace("\n", '\n', $str);
61 return $str;
62 }
63
64 // rips gettext strings from $file and prints them in C format
65 function do_extract($file, $regex) {
6db0b33 @dleffler Minor update to remove deprecated files/comments w/ ckeditor & old versi...
dleffler authored
66 global $total_new;
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
67
68 $content = @file_get_contents($file);
69 if (empty($content)) {
70 return;
71 }
72 preg_match_all(
73 $regex,
74 $content,
75 $matches,
76 PREG_PATTERN_ORDER
77 );
78 print "$file"." - ";
79 $num_added = 0;
80 for ($i=0; $i < count($matches[0]); $i++) {
81 str_replace('"', "\'", $matches[0][$i]); // remove the killer double-quotes
82 expLang::writeTemplate($matches[0][$i]);
83 $num_added++;
84 }
85 $total_new += $num_added;
86 print $num_added."\n";
87 }
88
89 // processes file for assoc strings
90 function do_file($file, $fileext) {
91 global $regex_gt, $regex_gettext_func, $regex_gettext_mod;
92 if ($fileext == 'tpl') {
93 do_extract($file,$regex_gettext_mod);
724764d @dleffler Cleans up language file by removing obsolete phrases (script updated) an...
dleffler authored
94 // do_extract($file,$regex_gettext_func); //FIXME these tend to hold computations and likewise break things?
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
95 } elseif ($fileext == 'php') {
96 do_extract($file,$regex_gt);
97 }
98 }
99
100 // go through a directory
101 function do_dir($dir) {
7a084d8 @dleffler Some language updates [#296]
dleffler authored
102 global $extensions, $recur;
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
103
104 $d = dir($dir);
105
106 while (false !== ($entry = $d->read())) {
107 if ($entry == '.' || $entry == '..') {
108 continue;
109 }
110
111 $entry = $dir.'/'.$entry;
112
113 if (is_dir($entry)) { // if a directory, go through it
7a084d8 @dleffler Some language updates [#296]
dleffler authored
114 if ($recur) do_dir($entry);
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
115 } else { // if file, parse only if extension is matched
116 $pi = pathinfo($entry);
117
118 if (isset($pi['extension']) && in_array($pi['extension'], $extensions)) {
119 do_file($entry,$pi['extension']);
120 }
121 }
122 }
123
124 $d->close();
125 }
126
127 for ($ac=1; $ac < $_SERVER['argc']; $ac++) {
128 print "Extracting Language Phrases\n";
7a084d8 @dleffler Some language updates [#296]
dleffler authored
129 if ($_SERVER['argv'][$ac] == '-r'){
130 $recur = false;
131 } elseif (is_dir($_SERVER['argv'][$ac])) { // go through directory
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
132 do_dir($_SERVER['argv'][$ac]);
133 } else { // do file
134 $pi = pathinfo($_SERVER['argv'][$ac]);
135 do_file($_SERVER['argv'][$ac],$pi['extension']);
136 }
2e3b354 @dleffler Update language (command-line) scripts so now the 'lang_update.php' scri...
dleffler authored
137 print "\nCompleted Extracting from ".$total_new." Phrases!\n\n";
238959b @dleffler Full Spanish translation (file), also the two scripts to extract languag...
dleffler authored
138 }
139
140 ?>
Something went wrong with that request. Please try again.