From 2862bb23b43b2a17bea04d513480b4fee40b08c1 Mon Sep 17 00:00:00 2001
From: Benoit Lavenierblavenie <benoit.lavenier@e-is.pro>
Date: Sat, 20 Jul 2019 16:39:37 +0200
Subject: [PATCH] [fix] Fix locale change [enh] Add country flag for each
 locales

---
 www/js/api/app.js                      |  4 +++-
 www/js/services/settings-services.js   | 19 +++++++++++--------
 www/templates/api/home.html            | 12 ++++++++----
 www/templates/api/locales_popover.html |  2 ++
 www/templates/api/transfer.html        | 13 +++++++++----
 5 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/www/js/api/app.js b/www/js/api/app.js
index 86aa5673f..cf809a253 100644
--- a/www/js/api/app.js
+++ b/www/js/api/app.js
@@ -57,9 +57,11 @@ angular.module('cesium-api', ['ionic', 'ionic-material', 'ngMessages', 'pascalpr
     $urlRouterProvider.otherwise('/app/home');
   })
 
-  .controller('ApiCtrl', function ($scope, $state, $translate, $ionicPopover, Modals, csSettings){
+  .controller('ApiCtrl', function ($scope, $state, $translate, $ionicPopover, UIUtils, Modals, csSettings){
     'ngInject';
 
+    // Fill locales
+    $scope.locales = angular.copy(csSettings.locales);
 
     $scope.showAboutModal = function(e) {
       e.preventDefault(); // avoid to open link href
diff --git a/www/js/services/settings-services.js b/www/js/services/settings-services.js
index 006ce5d33..e8e5d64e8 100644
--- a/www/js/services/settings-services.js
+++ b/www/js/services/settings-services.js
@@ -6,13 +6,13 @@ angular.module('cesium.settings.services', ['ngApi', 'cesium.config'])
 
   // Define app locales
   var locales = [
-    {id:'en',    label:'English'},
-    {id:'en-GB', label:'English (UK)'},
+    {id:'en',    label:'English', country: 'us'},
+    {id:'en-GB', label:'English (UK)', country: 'gb'},
     {id:'eo-EO', label:'Esperanto'},
-    {id:'fr-FR', label:'Français'},
-    {id:'nl-NL', label:'Nederlands'},
-    {id:'es-ES', label:'Spanish'},
-    {id:'it-IT', label:'Italiano'}
+    {id:'fr-FR', label:'Français' , country: 'fr'},
+    {id:'nl-NL', label:'Nederlands', country: 'nl'},
+    {id:'es-ES', label:'Spanish', country: 'es'},
+    {id:'it-IT', label:'Italiano', country: 'it'}
   ];
   var fallbackLocale = csConfig.fallbackLanguage ? fixLocale(csConfig.fallbackLanguage) : 'en';
 
@@ -201,7 +201,8 @@ angular.module('cesium.settings.services', ['ngApi', 'cesium.config'])
     var localeChanged = false;
     if (newData.locale && newData.locale.id) {
       // Fix previously stored locale (could use bad format)
-      newData.locale.id = fixLocale(newData.locale.id);
+      var localeId = fixLocale(newData.locale.id);
+      newData.locale = _.findWhere(locales, {id: localeId});
       localeChanged = !data.locale || newData.locale.id !== data.locale.id || newData.locale.id !== $translate.use();
     }
 
@@ -218,7 +219,9 @@ angular.module('cesium.settings.services', ['ngApi', 'cesium.config'])
 
     // Apply the new locale (only if need)
     // will produce an event cached by onLocaleChange();
-    if (localeChanged) $translate.use(data.locale.id);
+    if (localeChanged) {
+      $translate.use(data.locale.id);
+    }
 
   },
 
diff --git a/www/templates/api/home.html b/www/templates/api/home.html
index 6e97f7049..db9604dc5 100644
--- a/www/templates/api/home.html
+++ b/www/templates/api/home.html
@@ -4,12 +4,16 @@
   </ion-nav-title>
 
   <ion-nav-buttons side="right">
-    <!-- locales -->
-    <button class="button button-clear hidden-xs hidden-sm gray icon-left"
+    <!-- locales  -->
+    <img ng-if="$root.settings.locale.country"
+         ng-src="https://www.countryflags.io/{{$root.settings.locale.country}}/shiny/32.png"
+         style="padding: 5px 0"
+         ng-click="showLocalesPopover($event)">
+
+    <button class="button button-clear hidden-xs hidden-sm gray"
             style="max-width: 450px !important"
             ng-click="showLocalesPopover($event)">
-      <i class="icon ion-earth" ></i>
-      {{$root.settings.locale.label}}
+      {{$root.settings.locale.label}}&nbsp;
       <small class="ion-arrow-down-b"></small>
     </button>
   </ion-nav-buttons>
diff --git a/www/templates/api/locales_popover.html b/www/templates/api/locales_popover.html
index f9bf9dffe..e71190c63 100644
--- a/www/templates/api/locales_popover.html
+++ b/www/templates/api/locales_popover.html
@@ -5,6 +5,8 @@
       <a  ng-repeat="l in locales track by l.id"
         class="item item-icon-left ink"
          ng-click="changeLanguage(l.id)">
+        <i class="item-image avatar"
+           style="background-image: url(https://www.countryflags.io/{{l.country}}/flat/32.png)"></i>
         {{l.label | translate}}
       </a>
 
diff --git a/www/templates/api/transfer.html b/www/templates/api/transfer.html
index a2c28b33f..cd774b6cd 100644
--- a/www/templates/api/transfer.html
+++ b/www/templates/api/transfer.html
@@ -10,12 +10,17 @@
   </ion-nav-title>
 
   <ion-nav-buttons side="right">
-    <!-- locales -->
-    <button class="button button-clear hidden-xs hidden-sm gray icon-left"
+
+    <!-- locales  -->
+    <img ng-if="$root.settings.locale.country"
+         ng-src="https://www.countryflags.io/{{$root.settings.locale.country}}/shiny/32.png"
+         style="padding: 5px 0"
+         ng-click="showLocalesPopover($event)">
+
+    <button class="button button-clear hidden-xs hidden-sm gray"
             style="max-width: 450px !important"
             ng-click="showLocalesPopover($event)">
-      <i class="icon ion-earth" ></i>
-      {{$root.settings.locale.label}}
+      {{$root.settings.locale.label}}&nbsp;
       <small class="ion-arrow-down-b"></small>
     </button>
 
-- 
GitLab