]> dev.renevier.net Git - syp.git/blob - inc/i10n/updatelang.php
023797f8e17cdc16b386788467ee75ff016773ac
[syp.git] / inc / i10n / updatelang.php
1 #!/usr/bin/php
2 <?php
3 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
4    license. */
5
6 // only execute from command line
7 if (!(isset ($argc)) || !(isset ($argv))) {
8     exit (0);
9 }
10
11 $ROOTDIR = "../../";
12 // scripts in rootdir we need to link to
13 $SCRIPTS = array ("admin", "index", "upgrade", "wizard");
14
15 function usage() {
16     global $argv;
17     return ("Usage: " . $argv[0] . " [lang1] [lang2]\n");
18 }
19
20 function warn($str) { // writes string to stderr
21     error ($str);
22 }
23
24 function error($str) { // writes string to stderr
25     $stderr = fopen ('php://stderr', 'w');
26     fwrite ($stderr, $str);
27     fclose ($stderr); 
28 }
29
30 function main($argv, $argc, $rootdir, $scripts) {
31     $options = getopt("h");
32     if (isset ($options ['h'])) {
33         print usage ();
34         return 0;
35     }
36
37     if ($argc <= 1) { // update all existing langs
38         foreach (scandir (".") as $entry) {
39             if (is_dir ($entry) && !($entry[0] == ".") && !($entry == "en")) {
40                 updatelang ($entry, $rootdir, $scripts);
41             }
42         }
43     } else {
44         foreach (array_slice ($argv, 1) as $item) {
45             updatelang ($item, $rootdir, $scripts); 
46         }
47     }
48
49     return 0;
50 }
51
52 function escape_newline ($str, $tab) {
53     return str_replace ("\n", "\\n\" .\n$tab$tab\"", $str);
54 }
55 function escape_slash ($str) {
56     $res = str_replace ("\\", "\\\\", $str);
57     $res = str_replace ("\"", "\\\"", $res);
58     return $res;
59 }
60
61 function escape_all ($str, $tab) {
62     $str = escape_slash ($str);
63     $str = escape_newline ($str, $tab);
64     return $str;
65 }
66
67 function updatelang ($lang, $rootdir, $scripts) {
68     if (!preg_match ('/^[a-zA-Z]{2,3}(-[a-zA-Z]{2,3})?$/', $lang)) {
69         warn ("$lang is not a valid lang format.\n");
70         return false;
71     }
72     if ($lang == "en") {
73         warn ("en is reference language. It must be managed manually.\n");
74         return false;
75     }
76
77     if (!is_dir ($lang)) {
78         if (!mkdir ($lang)) {
79             error ("could not create $lang directory.\n");
80             return false;
81         }
82     }
83
84     require ("en/syp.php");
85     if (is_file ("$lang/syp.php")) {
86         require ("$lang/syp.php");
87     }
88
89     $translator_name = $translations[$lang]["_translator_name"];
90     $translator_mail = $translations[$lang]["_translator_mail"];
91     $language_name   = $translations[$lang]["_language_name"];
92
93     $tab = str_repeat (" ", 4);
94
95     $tmpname = tempnam ("", "");
96     $output = fopen ($tmpname, "w");
97
98     fwrite ($output, "<?php\n");
99
100     fwrite ($output, "$tab" . "\$translations['" . $lang . "'] = array(\n");
101     fwrite ($output, "$tab$tab" . "// your name\n");
102     fwrite ($output, "$tab$tab" . "\"_translator_name\" => \"" . escape_slash ($translator_name) . "\",\n");
103     fwrite ($output, "\n");
104
105     fwrite ($output, "$tab$tab" . "// your email\n");
106     fwrite ($output, "$tab$tab" . "\"_translator_mail\" => \"" . escape_slash ($translator_mail) . "\",\n");
107     fwrite ($output, "\n");
108
109     fwrite ($output, "$tab$tab" . "// your language name in your language. It will be used to link to\n");
110     fwrite ($output, "$tab$tab" . "// pages in your languages from pages in other\n");
111     fwrite ($output, "$tab$tab" . "\"_language_name\" => \"" . escape_slash ($language_name) . "\",\n");
112     fwrite ($output, "\n");
113
114     fwrite ($output, "$tab$tab" . "/* starts translation */\n");
115     fwrite ($output, "\n");
116     fwrite ($output, "\n");
117
118     foreach ($translations['en'] as $key => $value) {
119         if ($key[0] == "_") {
120             continue;
121         }
122         $value = $translations[$lang][$key];
123
124         fwrite ($output, "$tab$tab" . "\"" . escape_all ($key, $tab) . "\"" . "\n");
125         fwrite ($output, "$tab$tab  " . "=>\n");
126         fwrite ($output, "$tab$tab" . "\"" . escape_all ($value, $tab) . "\"" . "\n");
127         fwrite ($output, "$tab$tab  " . ",\n");
128         fwrite ($output, "\n");
129     }
130
131     fwrite ($output, "$tab" . ")\n");
132     fwrite ($output, "?>"); // <?php <- fixes vim syntax
133
134     fclose($output);
135
136     if (!rename ($tmpname, "$lang/syp.php")) {
137         error ("could not move $tmpname to $lang/sys.php");
138         unlink ($tmpname);
139         return false;
140     }
141     if (!chmod ("$lang/syp.php", 0644)) {
142         error ("could not set permissions to $lang/sys.php");
143         return false;
144     }
145
146     foreach ($scripts as $script) {
147         $link = $rootdir . "/"  . $script . "." . $lang . ".php";
148         $target = $script . ".php";
149         if (!file_exists ($link)) {
150             if (!symlink ($target, $link)) {
151                 error ("could not link $target to $link");
152             }
153         }
154     }
155     return true;
156 }
157
158 exit (main($argv, $argc, $ROOTDIR, $SCRIPTS));
159 ?>