From f1db5e1c962e458cc5da787cf8c248ebe23294f8 Mon Sep 17 00:00:00 2001 From: Igor Zhukov Date: Fri, 19 Sep 2014 14:49:46 +0400 Subject: [PATCH] Moved i18n locale apply to boot Moved i18n config to config.js App is booted after locale data is fully downloaded The app needs reload after locale change --- app/js/controllers.js | 4 +- app/js/i18n.js | 85 ++++--------------------------------------- app/js/init.js | 66 +++++++++++++++++++++++++++++++-- app/js/lib/config.js | 12 ++++++ 4 files changed, 86 insertions(+), 81 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 70d40b66..5e34305f 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -2185,8 +2185,10 @@ angular.module('myApp.controllers', ['myApp.i18n']) } $scope.$watch('i18n.locale', function (newValue, oldValue) { - _.locale(newValue); Storage.set({i18n_locale: newValue}); + ErrorService.confirm({type: 'APPLY_LANG_WITH_RELOAD'}).then(function () { + location.reload(); + }); }); }); diff --git a/app/js/i18n.js b/app/js/i18n.js index 9e23606b..0fbfb0f6 100644 --- a/app/js/i18n.js +++ b/app/js/i18n.js @@ -1,16 +1,10 @@ 'use strict'; angular.module('myApp.i18n', ['izhukov.utils']) - .factory('_', ['$http', '$route', 'Storage', '$locale', function($http, $route, Storage, $locale) { - var locale = 'en-us'; - var messages = {}; - var fallbackMessages = {}; - var supported = { - 'en-us': 'English' - }; - var aliases = { - 'en': 'en-us' - }; + .factory('_', [function() { + var locale = Config.I18n.locale; + var messages = Config.I18n.messages; + var fallbackMessages = Config.I18n.fallback_messages; var paramRegEx = /\{\s*([a-zA-Z\d\--]+)(?:\s*:\s*(.*?))?\s*\}/g; function insertParams(msgstr, params) { @@ -82,77 +76,14 @@ angular.module('myApp.i18n', ['izhukov.utils']) return msgstr; } - _.supported = function() { - return supported; + _.supported = function () { + return Config.I18n.supported; }; - _.locale = function(newValue) { - if (newValue === undefined) { - return locale; - } - - if (!supported.hasOwnProperty(newValue)) { - newValue = 'en-us'; // fallback - } - - if (locale != newValue) { - var newMessages = false; - var ngLocaleReady = false; - var onReady = function() { - if (newMessages === false || ngLocaleReady === false) { - // only execute when both - ngLocale and the new messages - are loaded - return; - } - function deepUpdate(oldValue, newValue) { - for (var i in newValue) { - if (i in oldValue && typeof oldValue[i] === 'object' && i !== null){ - deepUpdate(oldValue[i], newValue[i]); - } else { - oldValue[i] = newValue[i]; - } - } - } - // first we need to explizitly request the new $locale so it gets initialized - // then we recursively update our current $locale with it so all other modules - // already holding a reference to our $locale will get the new values as well - // this hack is necessary because ngLocale just isn't designed to be changed at runtime - deepUpdate($locale, angular.injector(['ngLocale']).get('$locale')); - messages = newMessages; - locale = newValue; - $route.reload(); - }; - - angular.element('