Browse Source

Fix gettext and prepare French translation

Julien VAUBOURG 9 years ago
parent
commit
4729390463
5 changed files with 81 additions and 71 deletions
  1. 59 42
      sources/config.php
  2. 8 12
      sources/controller.php
  3. 0 3
      sources/i18n/README
  4. 4 4
      sources/views/layout.html.php
  5. 10 10
      sources/views/settings.html.php

+ 59 - 42
sources/config.php

@@ -1,56 +1,73 @@
 <?php
 
-// Limonade configuration
+/* Wifi Hotspot app for YunoHost 
+ * Copyright (C) 2015 Julien Vaubourg <julien@vaubourg.com>
+ * Contribute at https://github.com/jvaubourg/hotspot_ynh
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Framework configuration
 function configure() {
-    option('env', ENV_PRODUCTION);
-    option('debug', false);
-    option('base_uri', '<TPL:NGINX_LOCATION>/');
-    layout("layout.html.php");
-    define('PUBLIC_DIR', '<TPL:NGINX_LOCATION>/public');
-}
+  option('env', ENV_PRODUCTION);
+  option('debug', false);
+  option('base_uri', '<TPL:NGINX_LOCATION>/');
 
-// Not found page
-function not_found($errno, $errstr, $errfile=null, $errline=null) {
-    $msg = h(rawurldecode($errstr));
-    return render($msg, 'error_layout.html.php');
-}
+  layout('layout.html.php');
 
-function T_($string) {
-    return gettext($string);
+  define('PUBLIC_DIR', '<TPL:NGINX_LOCATION>/public');
 }
 
 // Before routing
 function before($route) {
-     /**
-     * * Locale
-     * */
-    if (!isset($_SESSION['locale'])) {
-        $locale = explode(',',$_SERVER['HTTP_ACCEPT_LANGUAGE']);
-        $_SESSION['locale'] = strtolower(substr(chop($locale[0]),0,2));
-    }
-    $textdomain="localization";
-    putenv('LANGUAGE='.$_SESSION['locale']);
-    putenv('LANG='.$_SESSION['locale']);
-    putenv('LC_ALL='.$_SESSION['locale']);
-    putenv('LC_MESSAGES='.$_SESSION['locale']);
-    setlocale(LC_ALL,$_SESSION['locale']);
-    setlocale(LC_CTYPE,$_SESSION['locale']);
-    $locales_dir = dirname(__FILE__).'/../i18n';
-    bindtextdomain($textdomain,$locales_dir);
-    bind_textdomain_codeset($textdomain, 'UTF-8');
-    textdomain($textdomain);
-    // Set the $locale variable in template
-    set('locale', $_SESSION['locale']);
+  $lang_mapping = array(
+    'fr' => 'fr_FR'
+  );
+
+  if(!isset($_SESSION['locale'])) {
+    $locale = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
+    $_SESSION['locale'] = strtolower(substr(chop($locale[0]), 0, 2));
+  }
+
+  $lang = $_SESSION['locale'];
+
+  // Convert simple language code into full language code
+  if(array_key_exists($lang, $lang_mapping)) {
+    $lang = $lang_mapping[$lang];
+  }
+
+  $lang = "$lang.utf8";
+  $textdomain = "localization";
+
+  putenv("LANGUAGE=$lang");
+  putenv("LANG=$lang");
+  putenv("LC_ALL=$lang");
+  putenv("LC_MESSAGES=$lang");
+
+  setlocale(LC_ALL, $lang);
+  setlocale(LC_CTYPE, $lang);
+
+  $locales_dir = dirname(__FILE__).'/i18n';
+
+  bindtextdomain($textdomain, $locales_dir);
+  bind_textdomain_codeset($textdomain, 'UTF-8');
+  textdomain($textdomain);
+
+  set('locale', $lang);
 }
 
 // After routing
 function after($output, $route) {
-    /*
-    $time = number_format( (float)substr(microtime(), 0, 10) - LIM_START_MICROTIME, 6);
-    $output .= "\n<!-- page rendered in $time sec., on ".date(DATE_RFC822)." -->\n";
-    $output .= "<!-- for route\n";
-    $output .= print_r($route, true);
-    $output .= "-->";
-    */
-    return $output;
+  return $output;
 }

+ 8 - 12
sources/controller.php

@@ -66,11 +66,11 @@ dispatch_put('/settings', function() {
   if($service_enabled == 1) {
     try {
       if($_POST['wifi_device_id'] == -1) {
-        throw new Exception(T_('You need to select an associated hotspot'));
+        throw new Exception(_('You need to select an associated hotspot'));
       }
 
     } catch(Exception $e) {
-      flash('error', $e->getMessage().' ('.T_('configuration not updated').').');
+      flash('error', $e->getMessage().' ('._('configuration not updated').').');
       goto redirect;
     }
   }
@@ -85,13 +85,13 @@ dispatch_put('/settings', function() {
      $retcode = start_service();
 
     if($retcode == 0) {
-      flash('success', T_('Configuration updated and service successfully reloaded'));
+      flash('success', _('Configuration updated and service successfully reloaded'));
     } else {
-      flash('error', T_('Configuration updated but service reload failed'));
+      flash('error', _('Configuration updated but service reload failed'));
     }
 
   } else {
-      flash('success', T_('Service successfully disabled'));
+      flash('success', _('Service successfully disabled'));
   }
 
   redirect:
@@ -121,18 +121,14 @@ dispatch('/status', function() {
 });
 
 dispatch('/lang/:locale', function($locale = 'en') {
-  switch ($locale) {
+  switch($locale) {
     case 'fr':
       $_SESSION['locale'] = 'fr';
-      break;
+    break;
 
     default:
       $_SESSION['locale'] = 'en';
   }
 
-  if(!empty($_GET['redirect_to'])) {
-    redirect_to($_GET['redirect_to']);
-  } else {
-    redirect_to('/');
-  }
+  redirect_to('/');
 });

+ 0 - 3
sources/i18n/README

@@ -1,3 +0,0 @@
-Add a "localization.pot" to this directory, and one folder per translated language, containing the po a LC_MESSAGE directory (with the .mo)
-
-See: https://github.com/YunoHost/admin_v1/tree/master/i18n

+ 4 - 4
sources/views/layout.html.php

@@ -6,7 +6,7 @@
 <head>
   <meta charset="utf-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
-  <title><?= T_("Tor Client") ?><?= isset($title) ? " | $title" : '' ?></title>
+  <title><?= _("Tor Client") ?><?= isset($title) ? " | $title" : '' ?></title>
   <meta name="viewport" content="width=device-width">
   <link media="all" type="text/css" href="<?= PUBLIC_DIR ?>/bootstrap/css/bootstrap.min.css" rel="stylesheet">
   <link media="all" type="text/css" href="<?= PUBLIC_DIR ?>/bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
@@ -22,12 +22,12 @@
     <?php if(isset($flash['error'])): ?>
       <div class="alert alert-dismissible alert-danger fade in" style="margin-top: 20px" role="alert">
         <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
-        <strong><?= T_('Error') ?>:</strong> <?= $flash['error'] ?>
+        <strong><?= _('Error') ?>:</strong> <?= $flash['error'] ?>
       </div>
     <?php elseif(isset($flash['notice'])): ?>
       <div class="alert alert-dismissible alert-info fade in" style="margin-top: 20px" role="alert">
         <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
-        <strong><?= T_('Notice') ?>:</strong> <?= $flash['notice'] ?>
+        <strong><?= _('Notice') ?>:</strong> <?= $flash['notice'] ?>
       </div>
     <?php elseif(isset($flash['success'])): ?>
       <div class="alert alert-dismissible alert-success fade in" style="margin-top: 20px" role="alert">
@@ -39,7 +39,7 @@
     <?= $content?>
 
     <hr />
-    <div id="github"><a href="https://github.com/labriqueinternet/torclient_ynh"><?= T_('Any problem? Contribute!') ?></a> - AGPL 3.0</div>
+    <div id="github"><a href="https://github.com/labriqueinternet/torclient_ynh"><?= _('Any problem? Contribute!') ?></a> - AGPL 3.0</div>
   </div>
 </body>
 </html>

+ 10 - 10
sources/views/settings.html.php

@@ -1,11 +1,11 @@
-<h2><?= T_("Tor client configuration") ?></h2>
+<h2><?= _("Tor client configuration") ?></h2>
 <?php if($faststatus): ?>
-  <span class="label label-success" data-toggle="tooltip" data-title="<?= T_('This is a fast status. Click on More details to show the complete status.') ?>"><?= T_('Running') ?></span>
+  <span class="label label-success" data-toggle="tooltip" data-title="<?= _('This is a fast status. Click on More details to show the complete status.') ?>"><?= _('Running') ?></span>
 <?php else: ?>
-  <span class="label label-danger" data-toggle="tooltip" data-title="<?= T_('This is a fast status. Click on More details to show the complete status.') ?>"><?= T_('Not Running') ?></span>
+  <span class="label label-danger" data-toggle="tooltip" data-title="<?= _('This is a fast status. Click on More details to show the complete status.') ?>"><?= _('Not Running') ?></span>
 <?php endif; ?>
 
- &nbsp; <img src="public/img/loading.gif" id="status-loading" alt="Loading..." /><a href="#" id="statusbtn" data-toggle="tooltip" data-title="<?= T_('Loading complete status may take a few minutes. Be patient.') ?>"><?= T_('More details') ?></a>
+ &nbsp; <img src="public/img/loading.gif" id="status-loading" alt="Loading..." /><a href="#" id="statusbtn" data-toggle="tooltip" data-title="<?= _('Loading complete status may take a few minutes. Be patient.') ?>"><?= _('More details') ?></a>
 
 <div id="status" class="alert alert-dismissible alert-info fade in" style="margin-top: 10px" role="alert">
   <button type="button" class="close"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
@@ -21,13 +21,13 @@
 
       <div class="panel panel-default">
         <div class="panel-heading">
-          <h3 class="panel-title"><?= T_("Service") ?></h3>
+          <h3 class="panel-title"><?= _("Service") ?></h3>
         </div>
 
         <div style="padding: 14px 14px 0 10px">
   
           <div class="form-group">
-            <label for="service_enabled" class="col-sm-3 control-label"><?= T_('Tor Enabled') ?></label>
+            <label for="service_enabled" class="col-sm-3 control-label"><?= _('Tor Enabled') ?></label>
             <div class="col-sm-9 input-group-btn">
               <div class="input-group">
                 <input type="checkbox" class="form-control switch" name="service_enabled" id="service_enabled" value="1" <?= $service_enabled == 1 ? 'checked="checked"' : '' ?> />
@@ -39,15 +39,15 @@
             <?php if($wifi_device_id == -1): ?>
               <div class="alert alert-dismissible alert-warning fade in" style="margin: 2px 16px 17px" role="alert">
                 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
-                <strong><?= T_('Notice') ?>:</strong> <?= T_("You need to select an associated hotspot.") ?>
+                <strong><?= _('Notice') ?>:</strong> <?= _("You need to select an associated hotspot.") ?>
               </div>
             <?php endif; ?>
 
-            <label for="wifi_device_id" class="col-sm-3 control-label"><?= T_('Associated Hotspot') ?></label>
+            <label for="wifi_device_id" class="col-sm-3 control-label"><?= _('Associated Hotspot') ?></label>
             <div class="col-sm-9 input-group-btn">
               <div class="input-group">
                   <input type="text" name="wifi_device_id" id="wifi_device_id" value="<?= $wifi_device_id ?>" style="display: none" />
-                  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><?= empty($wifi_ssid) ? '<em>'.T_("None").'</em>' : $wifi_ssid ?> <span class="caret"></span></button>
+                  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"><?= empty($wifi_ssid) ? '<em>'._("None").'</em>' : $wifi_ssid ?> <span class="caret"></span></button>
                   <ul class="dropdown-menu dropdown-menu-left" id="deviceidlist" role="menu">
                     <?= $wifi_ssid_list ?>
                   </ul>
@@ -59,7 +59,7 @@
       </div>
       <div class="form-group">
         <div style="text-align: center">
-          <button type="submit" class="btn btn-default" data-toggle="tooltip" id="save" data-title="<?= T_('Reloading may take a few minutes. Be patient.') ?>"><?= T_('Save and reload') ?></button> <img src="public/img/loading.gif" id="save-loading" alt="Loading..." />
+          <button type="submit" class="btn btn-default" data-toggle="tooltip" id="save" data-title="<?= _('Reloading may take a few minutes. Be patient.') ?>"><?= _('Save and reload') ?></button> <img src="public/img/loading.gif" id="save-loading" alt="Loading..." />
         </div>
       </div>
     </form>