comparison index.php @ 64:6900beff8789

Improve multi-language support.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 24 Oct 2012 03:23:04 +0300
parents 6635246b70d9
children 2cfb0a7eac9b
comparison
equal deleted inserted replaced
63:6635246b70d9 64:6900beff8789
1 <?php 1 <?php
2 // 2 //
3 // Yes, this is horrible. :| 3 // Yes, this is horrible. :|
4 // 4 //
5 $pageLang = "fi";
5 $pageCharset = "utf-8"; 6 $pageCharset = "utf-8";
6 $internalCharset = "utf-8"; 7 $internalCharset = "utf-8";
7 $classDefault = "TTE9SNO"; 8 $classDefault = "TTE9SNO";
8 $classFile = "luokat.txt"; 9 $classFile = "luokat.txt";
9 $cacheFile = "coursecache.txt"; 10 $cacheFile = "coursecache.txt";
10 $baseURI = "http://tnsp.org/luk/"; 11 $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=";
12 $infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&koodi1=";
13 12
14 $pageCSSData = array("cookie" => "lukcss", "prefix" => $baseURI."luk"); 13 $pageCSSData = array("cookie" => "lukcss", "prefix" => $baseURI."luk");
15 $pageCSSAlts = array("blue" => "1", "old" => "2", "purple" => "3"); 14 $pageCSSAlts = array("blue" => "1", "old" => "2", "purple" => "3");
16 15
17 $pageLanguages = array("fi", "en"); 16 $pageLanguages = array("fi", "en");
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!", 30 "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!", 31 "fi" => "Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa!",
33 ), 32 ),
34 33
35 "beta" => array( 34 "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.", 35 "en" => " NOTICE! Lukkari v2.0beta is still under development. There may be bugs. Some old features (mini-mode) may not return.",
37 "fi" => " HUOM! Lukkari v2.0beta on vielä keskeneräinen. Vanhassa versiossa olleet puuttuvat ominaisuudet (mini-moodi) palaavat piakkoin.", 36 "fi" => " HUOM! Lukkari v2.0beta on vielä keskeneräinen. Bugeja voi olla. Jotkut vanhat ominaisuudet (mini-moodi) eivät välttämättä tule takaisin.",
38 ), 37 ),
39 38
40 "viikossa" => array( 39 "viikossa" => array(
41 "en" => "Total of <b>%1</b> hours in the week.", 40 "en" => "Total of <b>%1</b> hours in the week.",
42 "fi" => "Viikossa yhteensä <b>%1</b> tuntia.", 41 "fi" => "Viikossa yhteensä <b>%1</b> tuntia.",
60 // Translation function 59 // Translation function
61 function lukMsg($msg) 60 function lukMsg($msg)
62 { 61 {
63 global $lukTranslations, $pageLang; 62 global $lukTranslations, $pageLang;
64 63
65 if (!isset($pageLang))
66 $pageLang = "fi";
67
68 if (isset($lukTranslations[$msg]) && isset($lukTranslations[$msg][$pageLang])) 64 if (isset($lukTranslations[$msg]) && isset($lukTranslations[$msg][$pageLang]))
69 $str = $lukTranslations[$msg][$pageLang]; 65 $str = $lukTranslations[$msg][$pageLang];
70 else 66 else
71 $str = $msg; 67 $str = $msg;
72 68
79 75
80 76
81 function lukGetDayName($day) 77 function lukGetDayName($day)
82 { 78 {
83 global $lukDayNames, $pageLang; 79 global $lukDayNames, $pageLang;
84
85 if (!isset($pageLang))
86 $pageLang = "fi";
87 80
88 if (isset($lukDayNames[$pageLang]) && isset($lukDayNames[$pageLang][$day])) 81 if (isset($lukDayNames[$pageLang]) && isset($lukDayNames[$pageLang][$day]))
89 return $lukDayNames[$pageLang][$day]; 82 return $lukDayNames[$pageLang][$day];
90 else 83 else
91 return $lukDayNames["en"][$day]; 84 return $lukDayNames["en"][$day];
144 } 137 }
145 138
146 139
147 function lukMatchCourse($id) 140 function lukMatchCourse($id)
148 { 141 {
149 global $cache, $infoURI, $cacheDirty, $internalCharset; 142 global $cache, $cacheDirty, $internalCharset, $pageLang;
143
144 $uri = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=".strtoupper($pageLang)."&koodi1=".$id;
145
146 // Create the index
147 if (!isset($cache[$id]))
148 $cache[$id] = array();
150 149
151 // Check if course exists in cache 150 // Check if course exists in cache
152 if (!isset($cache[$id])) 151 if (!isset($cache[$id][$pageLang]))
153 { 152 {
154 // Not cached, try to fetch data 153 // Not cached, try to fetch data
155 $cacheDirty = TRUE; 154 $cacheDirty = TRUE;
156 $data = @file_get_contents($infoURI.$id); 155 $data = @file_get_contents($uri);
157 if ($data !== FALSE) 156 if ($data !== FALSE)
158 { 157 {
159 $data = iconv("iso-8859-1", $internalCharset, $data); 158 $data = iconv("iso-8859-1", $internalCharset, $data);
160 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*op\s*</strong></td>#", $data, $m)) 159 if (preg_match("#<td class=\"smallheadercell\"><strong>(.+?)\s+(\d+)\s*(op|ECTS\s+cr)\s*</strong></td>#", $data, $m))
161 { 160 {
162 // Add data to cache 161 // Add data to cache
163 $cache[$id] = array("desc" => $m[1], "op" => intval($m[2])); 162 $cache[$id][$pageLang] = array("desc" => $m[1], "op" => intval($m[2]));
164 } 163 }
165 } 164 }
166 } 165 }
167 166
168 if (isset($cache[$id])) 167 if (isset($cache[$id]) && isset($cache[$id][$pageLang]))
169 { 168 {
170 return "<a target=\"_blank\" title=\"".lukChEntities($id." - ".$cache[$id]["op"]." op"). 169 return "<a target=\"_blank\" title=\"".lukChEntities($id." - ".$cache[$id][$pageLang]["op"]." op").
171 "\" href=\"".lukChEntities($infoURI.$id)."\">".lukChEntities($cache[$id]["desc"])."</a>"; 170 "\" href=\"".lukChEntities($uri)."\">".lukChEntities($cache[$id][$pageLang]["desc"])."</a>";
172 } 171 }
173 else 172 else
174 return lukChEntities($id); 173 return lukChEntities($id);
175 } 174 }
176 175
453 echo lukNormalTimeTable(); 452 echo lukNormalTimeTable();
454 } 453 }
455 454
456 echo "<div id=\"footer\">". 455 echo "<div id=\"footer\">".
457 lukMsg("contact"). 456 lukMsg("contact").
458 "<span style=\"color: red;\">". 457 "<div style=\"color: red;\">".
459 lukMsg("beta"). 458 lukMsg("beta").
460 "</span> 459 "</div>
461 </div> 460 </div>
462 <div id=\"csssel\"> 461 <div id=\"csssel\">
463 <div id=\"ctitle\">Lukkari v2.0 beta</div> 462 <div id=\"ctitle\">Lukkari v2.0 beta</div>
464 <div>".lukMsg("Style").": "; 463 <div>".lukMsg("Style").": ";
465 464
476 "; 475 ";
477 printPageFooter(); 476 printPageFooter();
478 477
479 478
480 // Dump the course data cache, but only if it has changed 479 // Dump the course data cache, but only if it has changed
481 if ($cacheDirty) { 480 if ($cacheDirty)
482 // Create string containing the data 481 {
483 $str = "<?\n\$cache = array(\n"; 482 $str = "<?\n\$cache = ".var_export($cache, TRUE)."\n?>";
484 foreach ($cache as $id => $data) {
485 $str .= " \"".addslashes($id)."\" => array(\"desc\" => \"".
486 addslashes($data["desc"])."\", \"op\" => ".$data["op"]."),\n";
487 }
488 $str .= ");\n?>";
489 483
490 // Use locking to prevent concurrent access and dump data 484 // Use locking to prevent concurrent access and dump data
491 if (file_put_contents($cacheFile, $str, LOCK_EX) === FALSE) { 485 if (file_put_contents($cacheFile, $str, LOCK_EX) === FALSE) {
492 // Can't do much anything here .. 486 // Can't do much anything here ..
493 } 487 }