comparison index.php @ 63:6635246b70d9

Implement translations.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 24 Oct 2012 02:54:10 +0300
parents cdc04572800c
children 6900beff8789
comparison
equal deleted inserted replaced
62:ee2d16f9b204 63:6635246b70d9
2 // 2 //
3 // Yes, this is horrible. :| 3 // Yes, this is horrible. :|
4 // 4 //
5 $pageCharset = "utf-8"; 5 $pageCharset = "utf-8";
6 $internalCharset = "utf-8"; 6 $internalCharset = "utf-8";
7 $luokkaDefault = "TTE9SNO"; 7 $classDefault = "TTE9SNO";
8 $classFile = "luokat.txt"; 8 $classFile = "luokat.txt";
9 $cacheFile = "coursecache.txt"; 9 $cacheFile = "coursecache.txt";
10 $baseURI = "http://tnsp.org/luk2/"; 10 $baseURI = "http://tnsp.org/luk/";
11 //$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1="; 11 //$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1=";
12 $infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&koodi1="; 12 $infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&koodi1=";
13 $dayNames = array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai");
14 13
15 $pageCSSData = array("cookie" => "lukcss", "prefix" => $baseURI."luk"); 14 $pageCSSData = array("cookie" => "lukcss", "prefix" => $baseURI."luk");
16 $pageCSSAlts = array("blue" => "1", "old" => "2", "purple" => "3"); 15 $pageCSSAlts = array("blue" => "1", "old" => "2", "purple" => "3");
16
17 $pageLanguages = array("fi", "en");
18
19 $lukDayNames = array(
20 "fi" => array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai"),
21 "en" => array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
22 );
23
24 $lukTranslations = array(
25 "Original" => array("fi" => "Alkuperäinen"),
26 "Next period" => array("fi" => "Seuraava periodi"),
27 "Current period" => array("fi" => "Nykyinen periodi"),
28 "Switch" => array("fi" => "Vaihda"),
29
30 "contact" => array(
31 "en" => "Contact <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. The author can't be held responsible for any errors in the data!",
32 "fi" => "Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa!",
33 ),
34
35 "beta" => array(
36 "en" => " NOTICE! Lukkari v2.0beta is not yet finished. Features present in the previous version but currently missing (mini-mode) will return soon.",
37 "fi" => " HUOM! Lukkari v2.0beta on vielä keskeneräinen. Vanhassa versiossa olleet puuttuvat ominaisuudet (mini-moodi) palaavat piakkoin.",
38 ),
39
40 "viikossa" => array(
41 "en" => "Total of <b>%1</b> hours in the week.",
42 "fi" => "Viikossa yhteensä <b>%1</b> tuntia.",
43 ),
44
45 "classNotSet" => array(
46 "en" => "Class not set, using default <b>%1</b>.",
47 "fi" => "Luokkaa ei asetettu, käytetään vakioarvoa <b>%1</b>.",
48 ),
49
50 "Style" => array("fi" => "Tyyli"),
51 // "" => array("fi" => ),
52 );
53
17 54
18 // Include framework 55 // Include framework
19 require "mcommon.inc.php"; 56 require "mcommon.inc.php";
20 require "merrors.inc.php"; 57 require "merrors.inc.php";
21 58
22 59
23 // Helper functions 60 // Translation function
61 function lukMsg($msg)
62 {
63 global $lukTranslations, $pageLang;
64
65 if (!isset($pageLang))
66 $pageLang = "fi";
67
68 if (isset($lukTranslations[$msg]) && isset($lukTranslations[$msg][$pageLang]))
69 $str = $lukTranslations[$msg][$pageLang];
70 else
71 $str = $msg;
72
73 foreach (func_get_args() as $argn => $argv)
74 {
75 $str = preg_replace("/\%".$argn."/", $argv, $str);
76 }
77 return $str;
78 }
79
80
81 function lukGetDayName($day)
82 {
83 global $lukDayNames, $pageLang;
84
85 if (!isset($pageLang))
86 $pageLang = "fi";
87
88 if (isset($lukDayNames[$pageLang]) && isset($lukDayNames[$pageLang][$day]))
89 return $lukDayNames[$pageLang][$day];
90 else
91 return $lukDayNames["en"][$day];
92 }
93
94
24 function lukChEntities($str) 95 function lukChEntities($str)
25 { 96 {
26 global $internalCharset; 97 global $internalCharset;
27 // return $str;
28 return htmlentities($str, ENT_NOQUOTES, $internalCharset); 98 return htmlentities($str, ENT_NOQUOTES, $internalCharset);
29 } 99 }
30 100
101
31 function lukCheckClassID(&$id) 102 function lukCheckClassID(&$id)
32 { 103 {
33 global $luokkaDefault; 104 global $classDefault;
34 if (preg_match("#^([A-Z]{3}\d[A-Za-z0-9_]{1,6}|ccr|Ryh_[A-Z]{3}\d[A-Za-z0-9_]{1,6})$#", $id, $m)) { 105 if (preg_match("#^([A-Z]{3}\d[A-Za-z0-9_]{1,6}|ccr|Ryh_[A-Z]{3}\d[A-Za-z0-9_]{1,6})$#", $id, $m)) {
35 $id = $m[1]; 106 $id = $m[1];
36 return TRUE; 107 return TRUE;
37 } else { 108 } else {
38 errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$luokkaDefault."</b>."); 109 errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$classDefault."</b>.");
39 $id = $luokkaDefault; 110 $id = $classDefault;
40 return FALSE; 111 return FALSE;
41 } 112 }
42 } 113 }
43 114
44 115
45 function lukGetDayFromTimestamp($stamp) 116 function lukGetWeekdayFromTimestamp($stamp)
46 { 117 {
47 $info = getdate($stamp); 118 $info = getdate($stamp);
48 $day = $info["wday"]; 119 $day = $info["wday"];
49 return ($day > 0) ? $day - 1 : $day + 6; 120 return ($day > 0) ? $day - 1 : $day + 6;
50 } 121 }
167 } 238 }
168 return 0; 239 return 0;
169 } 240 }
170 241
171 242
172 // Check for mini-info mode
173 $miniMode = isset($_GET["nyt"]);
174
175 // Check given parameters 243 // Check given parameters
176 if (isset($_GET["luokka"])) 244 if (isset($_GET["luokka"]))
177 { 245 {
178 $luokka = $_GET["luokka"]; 246 $luokka = $_GET["luokka"];
179 if (lukCheckClassID($luokka) && !$miniMode) 247 if (lukCheckClassID($luokka) && !$miniMode)
187 $luokka = $_COOKIE["lukluokka"]; 255 $luokka = $_COOKIE["lukluokka"];
188 lukCheckClassID($luokka); 256 lukCheckClassID($luokka);
189 } 257 }
190 else 258 else
191 { 259 {
192 errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>."); 260 errorMsg(lukMsg("classNotSet", $classDefault));
193 $luokka = $luokkaDefault; 261 $luokka = $classDefault;
194 } 262 }
263
264
265 if (isset($_GET["lang"]))
266 {
267 $tmp = strtolower($_GET["lang"]);
268 if (in_array($tmp, $pageLanguages))
269 {
270 $pageLang = $tmp;
271 setcookie("luklang", $tmp, time() + 365*24*60*60); // expire in a year
272 }
273 }
274 else
275 if (isset($_COOKIE["luklang"]))
276 {
277 $tmp = $_COOKIE["luklang"];
278 if (in_array($tmp, $pageLanguages))
279 $pageLang = $tmp;
280 }
281
195 282
196 283
197 if (isset($_GET["next"])) 284 if (isset($_GET["next"]))
198 { 285 {
199 $nextPeriod = TRUE; 286 $nextPeriod = TRUE;
260 } 347 }
261 348
262 349
263 function lukNormalTimeTable() 350 function lukNormalTimeTable()
264 { 351 {
265 global $classInfo, $classHourDefs, $dayNames, $classHourTimes; 352 global $classInfo, $classHourDefs, $classHourTimes;
266 353
267 // $currStamp = time() + 9 * 60 * 60 + 18 * 60; 354 // $currStamp = time() + 9 * 60 * 60 + 18 * 60;
268 $currStamp = time(); 355 $currStamp = time();
269 $currTime = $currStamp - mktime(0, 0, 0); 356 $currTime = $currStamp - mktime(0, 0, 0);
270 $currDay = lukGetDayFromTimestamp($currStamp); 357 $currDay = lukGetWeekdayFromTimestamp($currStamp);
271 358
272 // Create the timetable table 359 // Create the timetable table
273 $out = 360 $out =
274 "<p>".join("; ", $classInfo["general"])."</p>\n". 361 "<p>".join("; ", $classInfo["general"])."</p>\n".
275 "<div>Viikossa yhteensä <b>".$classInfo["totalHours"]."</b> tuntia.</div>\n". 362 "<div>".lukMsg("viikossa", $classInfo["totalHours"])."</div>\n".
276 "<table class=\"timetable\">". 363 "<table class=\"timetable\">".
277 " <tr>\n <th></th>\n"; 364 " <tr>\n <th></th>\n";
278 $cellWidth = 100 / $classInfo["maxDays"]; 365 $cellWidth = 100 / $classInfo["maxDays"];
279 if ($cellWidth > 25) $cellWidth = 25; 366 if ($cellWidth > 25) $cellWidth = 25;
280 for ($day = 0; $day < $classInfo["maxDays"]; $day++) 367 for ($day = 0; $day < $classInfo["maxDays"]; $day++)
281 { 368 {
282 $out .= " <th style=\"width: ".$cellWidth."%;\" class=\"days\">".$dayNames[$day]."</th>\n"; 369 $out .= " <th style=\"width: ".$cellWidth."%;\" class=\"days\">".lukGetDayName($day)."</th>\n";
283 } 370 }
284 $out .= " </tr>\n"; 371 $out .= " </tr>\n";
285 372
286 for ($hour = $classInfo["firstHour"]; $hour < $classInfo["lastHour"]; $hour++) 373 for ($hour = $classInfo["firstHour"]; $hour < $classInfo["lastHour"]; $hour++)
287 { 374 {
302 $isActive = $day == $currDay && 389 $isActive = $day == $currDay &&
303 $currTime >= $classHourTimes[$class["start"]]["start"] && 390 $currTime >= $classHourTimes[$class["start"]]["start"] &&
304 $currTime < $classHourTimes[$class["start"] + $class["hours"] - 1]["end"]; 391 $currTime < $classHourTimes[$class["start"] + $class["hours"] - 1]["end"];
305 392
306 $out .= " <td rowspan=\"".$class["hours"]. 393 $out .= " <td rowspan=\"".$class["hours"].
307 "\" class=\"".($class["grouped"] ? "clgrouped" : ($isActive ? "clactive" : "clnormal"))."\">"; 394 "\" class=\"".($class["grouped"] ? "clgrouped" : ($isActive ? "clactive" : "clnormal"))."\">".
308
309 // if ($isActive)
310 // $out .= sprintf("%d : %d <> %d, %d -- ", $day, $currTime, $classHourTimes[$class["start"]]["start"], $classHourTimes[$class["start"] + $class["hours"] - 1]["end"]);
311
312 $out .=
313 lukGetClassInfo($isSplit, $class["data"]). 395 lukGetClassInfo($isSplit, $class["data"]).
314 "<div class=\"nhours\"><span>".$class["hours"]."h</span></div></td>\n"; 396 "<div class=\"nhours\"><span>".$class["hours"]."h</span></div></td>\n";
315 } 397 }
316 } 398 }
317 else 399 else
343 echo " <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".lukChEntities($class)."</option>\n"; 425 echo " <option ".($luokka == $class ? "selected=\"selected\" " : "")."value=\"".$class."\">".lukChEntities($class)."</option>\n";
344 } 426 }
345 echo 427 echo
346 " </select> 428 " </select>
347 </th> 429 </th>
348 <th><input class=\"submit\" type=\"submit\" value=\" Vaihda \" /></th> 430 <th><input class=\"submit\" type=\"submit\" value=\"".lukMsg("Switch")."\" /></th>
349 <th><a href=\"".$baseURI."?luokka=".$luokka; 431 <th><a href=\"http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_".$luokka.".htm\">".lukMsg("Original")."</a></th>
350 432
351 if ($miniMode) 433 <th><a href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">".
352 echo "\">Normaali"; 434 ($nextPeriod ? lukMsg("Current period") : lukMsg("Next period"))."</a></th>
353 else
354 echo "&amp;nyt\">Mini-info";
355
356 echo "</a></th>
357 <th><a href=\"http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_".$luokka.".htm\">Alkuperäinen</a></th>
358
359 <th><a href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">".($nextPeriod ? "Nykyinen" : "Seuraava")." periodi</a></th>
360 </tr> 435 </tr>
361 </table> 436 </table>
362 </form> 437 </form>
363 "; 438 ";
364 439
376 if ($haveData) 451 if ($haveData)
377 { 452 {
378 echo lukNormalTimeTable(); 453 echo lukNormalTimeTable();
379 } 454 }
380 455
381 echo "<div id=\"footer\"> 456 echo "<div id=\"footer\">".
382 Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa! 457 lukMsg("contact").
458 "<span style=\"color: red;\">".
459 lukMsg("beta").
460 "</span>
383 </div> 461 </div>
384 <div id=\"csssel\"> 462 <div id=\"csssel\">
385 <div id=\"ctitle\">Lukkari v2.0 beta</div> 463 <div id=\"ctitle\">Lukkari v2.0 beta</div>
386 Style: "; 464 <div>".lukMsg("Style").": ";
465
387 foreach ($pageCSSAlts as $name => $id) 466 foreach ($pageCSSAlts as $name => $id)
388 echo "<a href=\"?css=".$id."\">".$name."</a>"; 467 echo "<a href=\"?css=".$id."\">".$name."</a>";
468
469 echo "</div><div id=\"clang\">";
470 foreach ($pageLanguages as $id)
471 echo "<a href=\"?lang=".$id."\">".$id."</a>";
472
389 echo " 473 echo "
474 </div>
390 </div> 475 </div>
391 "; 476 ";
392 printPageFooter(); 477 printPageFooter();
393 478
394 479