3 /* Copyright (c) 2009 Arnaud Renevier, Inc, published under the modified BSD
6 // only execute from command line
7 if (!(isset ($argc)) || !(isset ($argv))) {
12 // scripts in rootdir we need to link to
13 $SCRIPTS = array ("admin", "index", "upgrade", "install");
17 return ("Usage: " . $argv[0] . " [lang1] [lang2]\n");
20 function warn($str) { // writes string to stderr
24 function error($str) { // writes string to stderr
25 $stderr = fopen ('php://stderr', 'w');
26 fwrite ($stderr, $str);
30 function main($argv, $argc, $rootdir, $scripts) {
31 $options = getopt("h");
32 if (isset ($options ['h'])) {
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);
44 foreach (array_slice ($argv, 1) as $item) {
45 updatelang ($item, $rootdir, $scripts);
52 function escape_newline ($str, $tab) {
53 return str_replace ("\n", "\\n\" .\n$tab$tab\"", $str);
55 function escape_slash ($str) {
56 $res = str_replace ("\\", "\\\\", $str);
57 $res = str_replace ("\"", "\\\"", $res);
61 function escape_all ($str, $tab) {
62 $str = escape_slash ($str);
63 $str = escape_newline ($str, $tab);
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");
73 warn ("en is reference language. It must be managed manually.\n");
77 if (!is_dir ($lang)) {
79 error ("could not create $lang directory.\n");
84 require ("en/syp.php");
85 if (is_file ("$lang/syp.php")) {
86 require ("$lang/syp.php");
89 $translator_name = $translations[$lang]["_translator_name"];
90 $translator_mail = $translations[$lang]["_translator_mail"];
91 $language_name = $translations[$lang]["_language_name"];
93 $tab = str_repeat (" ", 4);
95 $tmpname = tempnam ("", "");
96 $output = fopen ($tmpname, "w");
98 fwrite ($output, "<?php\n");
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");
105 fwrite ($output, "$tab$tab" . "// your email\n");
106 fwrite ($output, "$tab$tab" . "\"_translator_mail\" => \"" . escape_slash ($translator_mail) . "\",\n");
107 fwrite ($output, "\n");
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");
114 fwrite ($output, "$tab$tab" . "/* starts translation */\n");
115 fwrite ($output, "\n");
116 fwrite ($output, "\n");
118 foreach ($translations['en'] as $key => $value) {
119 if ($key[0] == "_") {
122 $value = $translations[$lang][$key];
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");
131 fwrite ($output, "$tab" . ")\n");
132 fwrite ($output, "?>"); // <?php <- fixes vim syntax
136 if (!rename ($tmpname, "$lang/syp.php")) {
137 error ("could not move $tmpname to $lang/sys.php");
141 if (!chmod ("$lang/syp.php", 0644)) {
142 error ("could not set permissions to $lang/sys.php");
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");
158 exit (main($argv, $argc, $ROOTDIR, $SCRIPTS));