i18n
authorGuillaume Cottenceau <gcottenc@gmail.com>
Thu, 9 Dec 2010 22:56:55 +0000 (23:56 +0100)
committerGuillaume Cottenceau <gcottenc@gmail.com>
Thu, 9 Dec 2010 22:56:55 +0000 (23:56 +0100)
googlemaps.js.php [moved from googlemaps.js with 90% similarity]
i18n.phpi [new file with mode: 0644]
images/en.png [new file with mode: 0644]
images/fr.png [new file with mode: 0644]
index.php
messages.en [new file with mode: 0644]
messages.fr [new file with mode: 0644]

similarity index 90%
rename from googlemaps.js
rename to googlemaps.js.php
index f3c7d8216c4893e58f3fac368d5a1b5499df1b85..b00879ffd60c66749bb6c4269bea775c9b91c364 100644 (file)
@@ -1,3 +1,9 @@
+<?php
+
+include("i18n.phpi");
+
+?>
+
 // Copyright (c) Guillaume Cottenceau and licensed under the Apache 2.0 License
 
 function debug(txt) {
@@ -41,8 +47,8 @@ function displayDirectionsMoreinfo(route, node) {
     if (total_hours > 0 && total_minutes < 10) {
         total_minutes = '0' + total_minutes;
     }
-    var base_sentence = total_hours == 0 ? '{1} km, about {2} minutes'
-                                         : '{1} km, about {3} hours and {2} minutes';
+    var base_sentence = total_hours == 0 ? '<?php echo t('route.summary.short') ?>'
+                                         : '<?php echo t('route.summary.long') ?>';
     node.innerHTML = base_sentence.replace('{1}', total_distance)
                                   .replace('{2}', total_minutes)
                                   .replace('{3}', total_hours);
diff --git a/i18n.phpi b/i18n.phpi
new file mode 100644 (file)
index 0000000..8909c0e
--- /dev/null
+++ b/i18n.phpi
@@ -0,0 +1,68 @@
+<?php
+
+    session_start();
+
+    $supported_languages = array("fr" => 1, "en" => 1);
+
+    function load($language) {
+        $datafile_fd = fopen("messages.$language", "r");
+        $messages = array();
+        while (!feof($datafile_fd) && ($line = fgets($datafile_fd, 4096))) {
+            $ary = explode('=', $line, 2);
+            if (count($ary) == 2) {
+                $messages[$ary[0]] = trim($ary[1]);
+            }
+        }
+        fclose($datafile_fd);
+        return $messages;
+    }
+
+    function t($msg) {
+        global $language;
+        global $messages_language;
+        global $messages_default;
+        if (!$messages_language) {
+            $messages_language = load($language);
+        }
+        if (!$messages_default) {
+            $messages_default = load('en');
+        }
+        if (isset($messages_language[$msg])) {
+            $msg = $messages_language[$msg];
+        } else {
+            error_log("ERROR " . $msg . " missing in " . $language);
+            if (isset($messages_default[$msg])) {
+                $msg = $messages_default[$msg];
+            } else {
+                error_log("ERROR " . $msg . " missing in en");
+                $msg = "???$msg???";
+            }
+        }
+        for ($i = 0; $i < func_num_args() - 1; $i++){
+            $arg = func_get_arg($i+1);
+            $msg = preg_replace('/\{' . $i . '}/', $arg, $msg);
+        }
+        return $msg;
+    }
+
+    $language = $_SESSION['language'];
+    if (isset($_REQUEST['language']) && $supported_languages[$_REQUEST['language']]) { 
+        $language = $_REQUEST['language'];
+    } 
+    if (!$language && $_SERVER['HTTP_ACCEPT_LANGUAGE']) { 
+        $accepted = explode( ",", $_SERVER['HTTP_ACCEPT_LANGUAGE']); 
+        for ($i = 0; $i < count($accepted); $i++) { 
+            $entry = explode(";", $accepted[$i]);
+            $lang = $entry[0];
+            if ($supported_languages[$lang]) { 
+                $language = $lang;
+                break;
+            } 
+        } 
+    }
+    if (!$language) {
+        $language = 'en';
+    }
+    $_SESSION['language'] = $language;
+
+?>
diff --git a/images/en.png b/images/en.png
new file mode 100644 (file)
index 0000000..0c4f4fe
Binary files /dev/null and b/images/en.png differ
diff --git a/images/fr.png b/images/fr.png
new file mode 100644 (file)
index 0000000..4f52a06
Binary files /dev/null and b/images/fr.png differ
index 2feae12f263a04eb2f3c39fdd53393e787532220..f1d5f5abc6e66aded3ed2e9a3d29d19d169c05f2 100644 (file)
--- a/index.php
+++ b/index.php
@@ -17,6 +17,8 @@
 ini_set("track_errors", 1);
 ini_set('display_errors', 0);
 
+include("i18n.phpi");
+
 // Helper function for fatal errors
 function fatal($msg) {
     echo "<h2>Error</h2>\n";
@@ -139,7 +141,7 @@ function parse_answer($answer) {
     <script type="text/javascript" src="prototype.js"></script>
     <script type="text/javascript" src="effects.js"></script>
     <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
-    <script type="text/javascript" src="googlemaps.js"></script>
+    <script type="text/javascript" src="googlemaps.js.php"></script>
     <script type="text/javascript">
 
       // Copyright (c) Guillaume Cottenceau and licensed under the Apache 2.0 License
@@ -259,7 +261,7 @@ function parse_answer($answer) {
                                    document.getElementById('address').value = '';
                                    document.getElementById('address').focus();
                                } else {
-                                   document.getElementById('submitshortresult').innerHTML = "Address not found...";
+                                   document.getElementById('submitshortresult').innerHTML = "<?php echo t('address.not.found') ?>";
                                    document.getElementById('submitshortresult').style.display = '';
                                    setTimeout('new Effect.Fade(document.getElementById("submitshortresult"), {duration: 0.4})', 1000);
                                }
@@ -321,20 +323,17 @@ function parse_answer($answer) {
                ?>
               mylat = <?php echo $latitude ?>;
               mylng = <?php echo $longitude ?>;
-              var baseinfomessage = 'This is where I located you,<br/>based on your IP address.<br/>';
+              var baseinfomessage = '<?php echo t('geolocated') ?>';
               <?php
                   } else {
                ?>
-              var baseinfomessage = 'It was not possible to locate<br/>you, based on your IP address<br/>'
-                                  + '<a href="http://www.hostip.info/" target="_blank">(submit your location!)</a>.<br/>';
+              var baseinfomessage = '<?php echo t('not.geolocated') ?>';
               <?php
                   }
                ?>
               map.setZoom(<?php echo $zoom ?>);
               me = startUp();
-              info_window = new google.maps.InfoWindow({ content: baseinfomessage
-                                                                  + 'You can drag this icon<br/>to move your position.<br/>'
-                                                                  + '<div class="fakelink" style="float: right" onclick="javascript:info_window.close()">Okay</div>' });
+              info_window = new google.maps.InfoWindow({ content: baseinfomessage + '<?php echo t('home.icon.move.possible') ?>' });
               info_window.open(map, me);
               if (!document.cookie || document.cookie.indexOf("not-a-newbie") == -1) {
                   document.getElementById("newbie").style.display = '';
@@ -357,36 +356,47 @@ function parse_answer($answer) {
 
     <div style="height: 2em">
       <div style="float: right">
-        travel mode:
+        <?php echo t('travelmode') ?>
         <select id="travelmode" onchange="travelmodeChanged()">
-          <option value="google.maps.DirectionsTravelMode.DRIVING">driving</option>
-          <option value="google.maps.DirectionsTravelMode.WALKING">walking</option>
+          <option value="google.maps.DirectionsTravelMode.DRIVING"><?php echo t('travelmode.driving') ?></option>
+          <option value="google.maps.DirectionsTravelMode.WALKING"><?php echo t('travelmode.walking') ?></option>
 <!-- doesn't work  <option value="google.maps.DirectionsTravelMode.BICYCLING">Bicycling</option> -->
         </select>
         - 
-        <input type="checkbox" id="fitmap" checked="true" onchange="fitmap()">fit to elements
+        <input type="checkbox" id="fitmap" checked="true" onchange="fitmap()"><?php echo t('fit.to.elements') ?>
         - 
         <a href="#" onclick="save()">
-          save this page
+          <?php echo t('save.this.page') ?>
           <img src="images/save.png" class="iconwithtext"/>
         </a>
+        -
+        <?php if ($language != 'en') { ?>
+         <a href="?language=en"><img src="images/en.png" class="iconwithtext" style="opacity:0.4;filter:alpha(opacity=40)"/></a>
+        <?php } else { ?>
+         <a href="?language=en"><img src="images/en.png" class="iconwithtext"/></a>
+        <?php } ?>
+        <?php if ($language != 'fr') { ?>
+         <a href="?language=fr"><img src="images/fr.png" class="iconwithtext" style="opacity:0.4;filter:alpha(opacity=40)"/></a>
+        <?php } else { ?>
+         <a href="?language=fr"><img src="images/fr.png" class="iconwithtext"/></a>
+        <?php } ?>
         <div id="savediv" style="display: none; position:absolute; right:1em; top:3em; background:#E0E0FF; border:solid 1px black; padding:0.5em; z-index:1">
-          The following link will directly present the current data:
+          <?php echo t('save.page.expl') ?>
           <br/>
           <input type="text" id="save" size="40"/>
           <br/>
-          <div style="float:right"><a href="#" onclick="javascript:new Effect.Fade(document.getElementById('savediv'), {duration: 0.4})">Okay</a></div>
+          <div style="float:right"><a href="#" onclick="javascript:new Effect.Fade(document.getElementById('savediv'), {duration: 0.4})"><?php echo t('Okay') ?></a></div>
         </div>
       </div>
 
       <form action="javascript:submit()">
-      Address:
+      <?php echo t('address') ?>
       
       <input type="text" size="32" id="address"/>
       <script type="text/javascript">
         document.getElementById('address').focus();
       </script>
-      <input type="submit" value="Show route!"/>
+      <input type="submit" value="<?php echo t('show.route') ?>"/>
       <span id="submitshortresult" style="color: red">
       </span>
       </form>
@@ -400,23 +410,19 @@ function parse_answer($answer) {
          </div>
          <div id="usecases" style="display:none; background-color: #F77; border: solid 1px blue; position:fixed; top:10%; left:30%; right:30%; z-index:1">
            <p style="margin:1em">
-             Use cases:
+             <?php echo t('use.cases.title') ?>
            </p>
            <p style="margin:1em">
-             You live near Gloucester Road in London; you know that Val d'Isère in France and Verbier in Switzerland are two great ski destinations and you would like to <a href="http://routes-compare.zarb.org/#me=51.492758928160555;-0.181784183374063&p=45.4487241;6.9803461;Val%20d'Is%C3%A8re&p=46.09614;7.21909;Verbier" target="_blank">compare routes</a>.
+             <?php echo t('use.case.val') ?>
            </p>
            <p style="margin:1em">
-           You know you want to visit three locations in France: Nice, Monaco and Menton, and you're looking
-        for a small village where to spend a few nights - move the little home icon and see that <a href="http://routes-compare.zarb.org/#me=43.753611471382484;7.382017588330072&p=43.7034273;7.2662656;nice&p=43.7384176;7.4246158;monaco&p=43.7788464;7.505666;menton" target="_blank">Laghet seems nicely located not far from all destinations</a>.
+             <?php echo t('use.case.riviera') ?>
            </p>
-           <div style="float:right; margin:1em"><a href="#" onclick="javascript:new Effect.Fade(document.getElementById('usecases'), {duration: 0.4})">Okay</a></div>
+           <div style="float:right; margin:1em"><a href="#" onclick="javascript:new Effect.Fade(document.getElementById('usecases'), {duration: 0.4})"><?php echo t('Okay') ?></a></div>
          </div>
          <div style="float:right; font-size:70%; margin-top:3px">
            <em>
-             uses goodies from <a href="http://maps.google.com/" target="_blank">Google Maps</a>, <a href="http://script.aculo.us/" target="_blank">Scriptaculous</a> and <a href="http://hostip.info/" target="_blank">hostip.info</a>, and is glued together by
-             <a href="http://zarb.org/~gc/" target="_blank">Guillaume Cottenceau</a>
-             - <a href="#" onclick="javascript:new Effect.Appear(document.getElementById('usecases'), {duration: 0.4})">use cases</a>
-             - <a href="http://git.zarb.org/cgi-bin/gitweb.cgi?p=routes-compare;a=summary" target="_blank">source</a>
+             <?php echo t('bottomstuff') ?>
            </em>
          </div>
       </div>
@@ -425,24 +431,23 @@ function parse_answer($answer) {
           <img src="images/top.png"/>
         </p>
         <p align="right" style="margin-right:1em; margin-top:-1em">
-          1. add an address in the top area
+          <?php echo t('add.addess.top') ?>
         </p>
         <br/>
         <p align="right">
           <img src="images/right.png"/>
         </p>
         <p align="right" style="margin-right:1em; margin-top:-1em">
-          2. the routes from all added addresses to the "home"
-          icon will be shown on the map
+          <?php echo t('routes.shown') ?>
         </p>
         <br/>
         <br/>
         <p align="right" style="margin-right:1em; margin-top:-1em">
-          <a href="#" onclick="javascript:new Effect.Fade(document.getElementById('newbie'), {duration: 0.4})">Okay</a>
+          <a href="#" onclick="javascript:new Effect.Fade(document.getElementById('newbie'), {duration: 0.4})"><?php echo t('Okay') ?></a>
         </p>
         <br/>
         <p style="margin:1em">
-          <a href="#" onclick="javascript:new Effect.Appear(document.getElementById('usecases'), {duration: 0.4})">Show me use cases!</a>
+          <a href="#" onclick="javascript:new Effect.Appear(document.getElementById('usecases'), {duration: 0.4})"><?php echo t('show.use.cases') ?></a>
         </p>
       </div>
       <ul id="resultslist">
diff --git a/messages.en b/messages.en
new file mode 100644 (file)
index 0000000..71e4b87
--- /dev/null
@@ -0,0 +1,30 @@
+address=Address:
+show.route=Show route!
+
+travelmode=travel mode:
+travelmode.driving=driving
+travelmode.walking=walking
+fit.to.elements=fit to elements
+save.this.page=save this page
+
+save.page.expl=The following link will directly present the current data:
+Okay=Okay
+
+add.addess.top=1. add an address in the top area
+routes.shown=2. the routes from all added addresses to the "home" icon will be shown on the map
+show.use.cases=Show me use cases!
+
+use.cases.title=Use cases:
+use.case.val=You live near Gloucester Road in London; you know that Val d'Isère in France and Verbier in Switzerland are two great ski destinations and you would like to <a href="http://routes-compare.zarb.org/#me=51.492758928160555;-0.181784183374063&p=45.4487241;6.9803461;Val%20d'Is%C3%A8re&p=46.09614;7.21909;Verbier" target="_blank">compare routes</a>.
+use.case.riviera=You know you want to visit three locations in France: Nice, Monaco and Menton, and you're looking for a small village where to spend a few nights - move the little home icon and see that <a href="http://routes-compare.zarb.org/#me=43.753611471382484;7.382017588330072&p=43.7034273;7.2662656;nice&p=43.7384176;7.4246158;monaco&p=43.7788464;7.505666;menton" target="_blank">Laghet seems nicely located not far from all destinations</a>.
+
+bottomstuff=uses goodies from <a href="http://maps.google.com/" target="_blank">Google Maps</a>, <a href="http://script.aculo.us/" target="_blank">Scriptaculous</a> and <a href="http://hostip.info/" target="_blank">hostip.info</a>, and is glued together by <a href="http://zarb.org/~gc/" target="_blank">Guillaume Cottenceau</a> - <a href="#" onclick="javascript:new Effect.Appear(document.getElementById('usecases'), {duration: 0.4})">use cases</a> - <a href="http://git.zarb.org/cgi-bin/gitweb.cgi?p=routes-compare;a=summary" target="_blank">source</a>
+
+address.not.found=Address not found...
+
+geolocated=This is where I located you,<br/>based on your IP address.<br/>
+not.geolocated=It was not possible to locate<br/>you, based on your IP address<br/><a href="http://www.hostip.info/" target="_blank">(submit your location!)</a>.<br/>
+home.icon.move.possible=You can drag this icon<br/>to move your position.<br/><div class="fakelink" style="float: right" onclick="javascript:info_window.close()">Okay</div>
+
+route.summary.short={1} km, about {2} minutes
+route.summary.long={1} km, about {3} hours and {2} minutes
diff --git a/messages.fr b/messages.fr
new file mode 100644 (file)
index 0000000..afde43d
--- /dev/null
@@ -0,0 +1,32 @@
+address=Adresse :
+show.route=Montrer la route !
+
+travelmode=transport :
+travelmode.driving=voiture
+travelmode.walking=marche
+fit.to.elements=ajuster aux éléments
+save.this.page=sauver cette page
+
+save.page.expl=Le lien suivant présentera directement les données actuelles :
+Okay=D'accord
+
+add.addess.top=1. ajoutez une adresse dans la zone en haut
+routes.shown=2. the routes from all added addresses to the "home" icon will be shown on the map
+show.use.cases=Show me use cases!
+
+use.cases.title=Use cases:
+use.case.val=You live near Gloucester Road in London; you know that Val d'Isère in France and Verbier in Switzerland are two great ski destinations and you would like to <a href="http://routes-compare.zarb.org/#me=51.492758928160555;-0.181784183374063&p=45.4487241;6.9803461;Val%20d'Is%C3%A8re&p=46.09614;7.21909;Verbier" target="_blank">compare routes</a>.
+use.case.riviera=You know you want to visit three locations in France: Nice, Monaco and Menton, and you're looking for a small village where to spend a few nights - move the little home icon and see that <a href="http://routes-compare.zarb.org/#me=43.753611471382484;7.382017588330072&p=43.7034273;7.2662656;nice&p=43.7384176;7.4246158;monaco&p=43.7788464;7.505666;menton" target="_blank">Laghet seems nicely located not far from all destinations</a>.
+
+bottomstuff=uses goodies from <a href="http://maps.google.com/" target="_blank">Google Maps</a>, <a href="http://script.aculo.us/" target="_blank">Scriptaculous</a> and <a href="http://hostip.info/" target="_blank">hostip.info</a>, and is glued together by <a href="http://zarb.org/~gc/" target="_blank">Guillaume Cottenceau</a> - <a href="#" onclick="javascript:new Effect.Appear(document.getElementById('usecases'), {duration: 0.4})">use cases</a> - <a href="http://git.zarb.org/cgi-bin/gitweb.cgi?p=routes-compare;a=summary" target="_blank">source</a>
+
+address.not.found=Address not found...
+
+geolocated=This is where I located you,<br/>based on your IP address.<br/>
+not.geolocated=It was not possible to locate<br/>you, based on your IP address<br/><a href="http://www.hostip.info/" target="_blank">(submit your location!)</a>.<br/>
+home.icon.move.possible=You can drag this icon<br/>to move your position.<br/><div class="fakelink" style="float: right" onclick="javascript:info_window.close()">Okay</div>
+
+route.summary.short={1} km, environ {2} minutes
+route.summary.long={1} km, environ {3} heures et {2} minutes
+
+