4 // only execute from command line
5 if (!(isset ($argc)) || !(isset ($argv))) {
10 $SCRIPTS=array("admin", "index", "wizard"); // scripts in rootdir we need to link to
14 return ("Usage: " . $argv[0] . " [lang1] [lang2]\n");
17 function warn($str) { // writes string to stderr
21 function error($str) { // writes string to stderr
22 $stderr = fopen ('php://stderr', 'w');
23 fwrite ($stderr, $str);
27 function main($argv, $argc, $rootdir, $scripts) {
28 $options = getopt("h");
29 if (isset ($options ['h'])) {
34 if ($argc <= 1) { // update all existing langs
35 foreach (scandir (".") as $entry) {
36 if (is_dir ($entry) && !($entry[0] == ".") && !($entry == "en")) {
37 updatelang ($entry, $rootdir, $scripts);
41 foreach (array_slice ($argv, 1) as $item) {
42 updatelang ($item, $rootdir, $scripts);
49 function escape_newline ($str, $tab) {
50 return str_replace("\n", "\\n\" .\n$tab$tab\"", $str);
52 function escape_slash ($str) {
53 $res = str_replace("\\", "\\\\", $str);
54 $res = str_replace("\"", "\\\"", $res);
58 function escape_all ($str, $tab) {
59 $str = escape_slash ($str);
60 $str = escape_newline ($str, $tab);
64 function updatelang($lang, $rootdir, $scripts) {
65 if (!preg_match ('/^[a-zA-Z]{2,3}(-[a-zA-Z]{2,3})?$/', $lang)) {
66 warn ("$lang is not a valid lang format.\n");
70 warn ("en is reference language. It must be managed manually.\n");
74 if (!is_dir ($lang)) {
76 error ("could not create $lang directory.\n");
81 require ("en/syp.php");
82 if (is_file ("$lang/syp.php")) {
83 require ("$lang/syp.php");
86 $translator_name = $translations[$lang]["_translator_name"];
87 $translator_mail = $translations[$lang]["_translator_mail"];
88 $language_name = $translations[$lang]["_language_name"];
90 $tab = str_repeat (" ", 4);
92 $tmpname = tempnam ("", "");
93 $output = fopen($tmpname, "w");
95 fwrite ($output, "<?php\n");
97 fwrite ($output, "$tab" . "\$translations['" . $lang . "'] = array(\n");
98 fwrite ($output, "$tab$tab" . "// your name\n");
99 fwrite ($output, "$tab$tab" . "\"_translator_name\" => \"" . escape_slash ($translator_name) . "\",\n");
100 fwrite ($output, "\n");
102 fwrite ($output, "$tab$tab" . "// your email\n");
103 fwrite ($output, "$tab$tab" . "\"_translator_mail\" => \"" . escape_slash ($translator_mail) . "\",\n");
104 fwrite ($output, "\n");
106 fwrite ($output, "$tab$tab" . "// your language name in your language. It will be used to link to\n");
107 fwrite ($output, "$tab$tab" . "// pages in your languages from pages in other\n");
108 fwrite ($output, "$tab$tab" . "\"_language_name\" => \"" . escape_slash ($language_name) . "\",\n");
109 fwrite ($output, "\n");
111 fwrite ($output, "$tab$tab" . "/* starts translation */\n");
112 fwrite ($output, "\n");
113 fwrite ($output, "\n");
115 foreach ($translations['en'] as $key => $value) {
116 if ($key[0] == "_") {
119 $value = $translations[$lang][$key];
121 fwrite ($output, "$tab$tab" . "\"" . escape_all ($key, $tab) . "\"" . "\n");
122 fwrite ($output, "$tab$tab " . "=>\n");
123 fwrite ($output, "$tab$tab" . "\"" . escape_all ($value, $tab) . "\"" . "\n");
124 fwrite ($output, "$tab$tab " . ",\n");
125 fwrite ($output, "\n");
128 fwrite ($output, "$tab" . ")\n");
129 fwrite ($output, "?>"); // <?php <- fixes vim syntax
133 if (!rename ($tmpname, "$lang/syp.php")) {
134 error ("could not move $tmpname to $lang/sys.php");
138 if (!chmod ("$lang/syp.php", 0644)) {
139 error ("could not set permissions to $lang/sys.php");
143 foreach ($scripts as $script) {
144 $link = $rootdir . "/" . $script . "." . $lang . ".php";
145 $target = $script . ".php";
146 if (!file_exists ($link)) {
147 if (!symlink ($target, $link)) {
148 error ("could not link $target to $link");
155 exit (main($argv, $argc, $ROOTDIR, $SCRIPTS));