changeset 63:6635246b70d9

Implement translations.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 24 Oct 2012 02:54:10 +0300
parents ee2d16f9b204
children 6900beff8789
files index.php
diffstat 1 files changed, 123 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/index.php	Wed Oct 24 02:53:44 2012 +0300
+++ b/index.php	Wed Oct 24 02:54:10 2012 +0300
@@ -4,45 +4,116 @@
 //
 $pageCharset = "utf-8";
 $internalCharset = "utf-8";
-$luokkaDefault = "TTE9SNO";
+$classDefault = "TTE9SNO";
 $classFile = "luokat.txt";
 $cacheFile = "coursecache.txt";
-$baseURI = "http://tnsp.org/luk2/";
+$baseURI = "http://tnsp.org/luk/";
 //$infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&opas=2010-2011&vuosi=10S11K&koodi1=";
 $infoURI = "http://www.oamk.fi/opiskelijalle/rakenne/opinto-opas/koulutusohjelmat/?sivu=oj&kieli=FI&koodi1=";
-$dayNames = array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai");
 
 $pageCSSData = array("cookie" => "lukcss", "prefix" => $baseURI."luk");
 $pageCSSAlts = array("blue" => "1", "old" => "2", "purple" => "3");
 
+$pageLanguages = array("fi", "en");
+
+$lukDayNames = array(
+  "fi" => array("Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai"),
+  "en" => array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"),
+);
+
+$lukTranslations = array(
+  "Original" => array("fi" => "Alkuperäinen"),
+  "Next period" => array("fi" => "Seuraava periodi"),
+  "Current period" => array("fi" => "Nykyinen periodi"),
+  "Switch" => array("fi" => "Vaihda"),
+
+  "contact" => array(
+    "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!",
+    "fi" => "Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa!",
+  ),
+  
+  "beta" => array(
+    "en" => " NOTICE! Lukkari v2.0beta is not yet finished. Features present in the previous version but currently missing (mini-mode) will return soon.",
+    "fi" => " HUOM! Lukkari v2.0beta on vielä keskeneräinen. Vanhassa versiossa olleet puuttuvat ominaisuudet (mini-moodi) palaavat piakkoin.",
+  ),
+
+  "viikossa" => array(
+    "en" => "Total of <b>%1</b> hours in the week.",
+    "fi" => "Viikossa yhteensä <b>%1</b> tuntia.",
+  ),
+
+  "classNotSet" => array(
+    "en" => "Class not set, using default <b>%1</b>.",
+    "fi" => "Luokkaa ei asetettu, käytetään vakioarvoa <b>%1</b>.",
+  ),
+  
+  "Style" => array("fi" => "Tyyli"),
+//  "" => array("fi" => ),
+);
+
+
 // Include framework
 require "mcommon.inc.php";
 require "merrors.inc.php";
 
 
-// Helper functions
+// Translation function
+function lukMsg($msg)
+{
+  global $lukTranslations, $pageLang;
+
+  if (!isset($pageLang))
+    $pageLang = "fi";
+
+  if (isset($lukTranslations[$msg]) && isset($lukTranslations[$msg][$pageLang]))
+    $str = $lukTranslations[$msg][$pageLang];
+  else
+    $str = $msg;
+  
+  foreach (func_get_args() as $argn => $argv)
+  {
+    $str = preg_replace("/\%".$argn."/", $argv, $str);
+  }
+  return $str;
+}
+
+
+function lukGetDayName($day)
+{
+  global $lukDayNames, $pageLang;
+
+  if (!isset($pageLang))
+    $pageLang = "fi";
+
+  if (isset($lukDayNames[$pageLang]) && isset($lukDayNames[$pageLang][$day]))
+    return $lukDayNames[$pageLang][$day];
+  else
+    return $lukDayNames["en"][$day];
+}
+
+
 function lukChEntities($str)
 {
   global $internalCharset;
-//  return $str;
   return htmlentities($str, ENT_NOQUOTES, $internalCharset);
 }
 
+
 function lukCheckClassID(&$id)
 {
-  global $luokkaDefault;
+  global $classDefault;
   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)) {
     $id = $m[1];
     return TRUE;
   } else {
-    errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$luokkaDefault."</b>.");
-    $id = $luokkaDefault;
+    errorMsg("Virhe! Luokan täytyy olla muotoa <b>XXXnXXX</b>, käytetään vakioarvoa <b>".$classDefault."</b>.");
+    $id = $classDefault;
     return FALSE;
   }
 }
 
 
-function lukGetDayFromTimestamp($stamp)
+function lukGetWeekdayFromTimestamp($stamp)
 {
   $info = getdate($stamp);
   $day = $info["wday"];
@@ -169,9 +240,6 @@
 }
 
 
-// Check for mini-info mode
-$miniMode = isset($_GET["nyt"]);
-
 // Check given parameters
 if (isset($_GET["luokka"]))
 {
@@ -189,11 +257,30 @@
 }
 else
 {
-  errorMsg("Luokkaa ei asetettu, käytetään vakioarvoa <b>".$luokkaDefault."</b>.");
-  $luokka = $luokkaDefault;
+  errorMsg(lukMsg("classNotSet", $classDefault));
+  $luokka = $classDefault;
 }
 
 
+if (isset($_GET["lang"]))
+{
+  $tmp = strtolower($_GET["lang"]);
+  if (in_array($tmp, $pageLanguages))
+  {
+    $pageLang = $tmp;
+    setcookie("luklang", $tmp, time() + 365*24*60*60); // expire in a year
+  }
+}
+else
+if (isset($_COOKIE["luklang"]))
+{
+  $tmp = $_COOKIE["luklang"];
+  if (in_array($tmp, $pageLanguages))
+    $pageLang = $tmp;
+}
+
+
+
 if (isset($_GET["next"]))
 {
   $nextPeriod = TRUE;
@@ -262,24 +349,24 @@
 
 function lukNormalTimeTable()
 {
-  global $classInfo, $classHourDefs, $dayNames, $classHourTimes;
+  global $classInfo, $classHourDefs, $classHourTimes;
   
 //  $currStamp = time() + 9 * 60 * 60 + 18 * 60;
   $currStamp = time();
   $currTime = $currStamp - mktime(0, 0, 0);
-  $currDay = lukGetDayFromTimestamp($currStamp);
+  $currDay = lukGetWeekdayFromTimestamp($currStamp);
   
   // Create the timetable table
   $out =
   "<p>".join("; ", $classInfo["general"])."</p>\n".
-  "<div>Viikossa yhteensä <b>".$classInfo["totalHours"]."</b> tuntia.</div>\n".
+  "<div>".lukMsg("viikossa", $classInfo["totalHours"])."</div>\n".
   "<table class=\"timetable\">".
   " <tr>\n  <th></th>\n";
   $cellWidth = 100 / $classInfo["maxDays"];
   if ($cellWidth > 25) $cellWidth = 25;
   for ($day = 0; $day < $classInfo["maxDays"]; $day++)
   {
-    $out .=  "  <th style=\"width: ".$cellWidth."%;\" class=\"days\">".$dayNames[$day]."</th>\n";
+    $out .=  "  <th style=\"width: ".$cellWidth."%;\" class=\"days\">".lukGetDayName($day)."</th>\n";
   }
   $out .= " </tr>\n";
 
@@ -304,12 +391,7 @@
                       $currTime <  $classHourTimes[$class["start"] + $class["hours"] - 1]["end"];
 
           $out .= "  <td rowspan=\"".$class["hours"].
-          "\" class=\"".($class["grouped"] ? "clgrouped" : ($isActive ? "clactive" : "clnormal"))."\">";
-
-//          if ($isActive)
-//            $out .= sprintf("%d : %d <> %d, %d -- ", $day, $currTime, $classHourTimes[$class["start"]]["start"], $classHourTimes[$class["start"] + $class["hours"] - 1]["end"]);
-          
-          $out .=
+          "\" class=\"".($class["grouped"] ? "clgrouped" : ($isActive ? "clactive" : "clnormal"))."\">".
           lukGetClassInfo($isSplit, $class["data"]).
           "<div class=\"nhours\"><span>".$class["hours"]."h</span></div></td>\n";
         }
@@ -345,18 +427,11 @@
 echo 
 "    </select>
    </th>
-   <th><input class=\"submit\" type=\"submit\" value=\" Vaihda \" /></th>
-   <th><a href=\"".$baseURI."?luokka=".$luokka;
+   <th><input class=\"submit\" type=\"submit\" value=\"".lukMsg("Switch")."\" /></th>
+   <th><a href=\"http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_".$luokka.".htm\">".lukMsg("Original")."</a></th>
 
-if ($miniMode)
-  echo "\">Normaali";
-else
-  echo "&amp;nyt\">Mini-info";
-
-echo "</a></th>
-   <th><a href=\"http://www.oamk.fi/tyojarjestykset/otek/luokat/OR_".$luokka.".htm\">Alkuperäinen</a></th>
-
-   <th><a href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">".($nextPeriod ? "Nykyinen" : "Seuraava")." periodi</a></th>
+   <th><a href=\"".$baseURI.($nextPeriod ? "" : "?next")."\">".
+     ($nextPeriod ? lukMsg("Current period") : lukMsg("Next period"))."</a></th>
   </tr>
  </table>
 </form>
@@ -378,16 +453,26 @@
   echo lukNormalTimeTable();
 }
 
-echo "<div id=\"footer\">
-Yhteydenotot <b>ccr @ IRCNet</b> tai <b>ccr at tnsp dot org</b>. En vastaa mahdollisista virheistä tiedoissa!
+echo "<div id=\"footer\">".
+lukMsg("contact").
+"<span style=\"color: red;\">".
+lukMsg("beta").
+"</span>
 </div>
 <div id=\"csssel\">
 <div id=\"ctitle\">Lukkari v2.0 beta</div>
-Style: ";
+<div>".lukMsg("Style").": ";
+
 foreach ($pageCSSAlts as $name => $id)
   echo "<a href=\"?css=".$id."\">".$name."</a>";
+
+echo "</div><div id=\"clang\">";
+foreach ($pageLanguages as $id)
+  echo "<a href=\"?lang=".$id."\">".$id."</a>";
+
 echo "
 </div>
+</div>
 ";
 printPageFooter();