web-apps/apps/common/locale.js

166 lines
5.6 KiB
JavaScript
Raw Normal View History

2016-04-01 13:17:09 +00:00
/*
*
* (c) Copyright Ascensio System SIA 2010-2019
2016-04-01 13:17:09 +00:00
*
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation. In accordance with
* Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement
* of any third-party rights.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha
* street, Riga, Latvia, EU, LV-1050.
2016-04-01 13:17:09 +00:00
*
* The interactive user interfaces in modified source and object code versions
* of the Program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product
* logo when distributing the program. Pursuant to Section 7(e) we decline to
* grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as
* well as technical writing content are licensed under the terms of the
* Creative Commons Attribution-ShareAlike 4.0 International. See the License
* terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
*
*/
2016-03-11 00:48:53 +00:00
if (Common === undefined) {
var Common = {};
}
Common.Locale = new(function() {
2019-02-01 16:51:48 +00:00
"use strict";
var l10n = null;
var loadcallback,
2019-12-10 14:22:28 +00:00
apply = false,
defLang = '{{DEFAULT_LANG}}',
currentLang = defLang;
2016-03-11 00:48:53 +00:00
var _applyLocalization = function(callback) {
2016-03-11 00:48:53 +00:00
try {
callback && (loadcallback = callback);
if (l10n) {
for (var prop in l10n) {
var p = prop.split('.');
if (p && p.length > 2) {
2016-03-11 00:48:53 +00:00
var obj = window;
for (var i = 0; i < p.length - 1; ++i) {
if (obj[p[i]] === undefined) {
obj[p[i]] = new Object();
}
obj = obj[p[i]];
2016-03-11 00:48:53 +00:00
}
if (obj) {
obj[p[p.length - 1]] = l10n[prop];
}
2016-03-11 00:48:53 +00:00
}
}
loadcallback && loadcallback();
} else
apply = true;
2016-03-11 00:48:53 +00:00
}
catch (e) {
}
};
var _get = function(prop, scope) {
var res = '';
if (l10n && scope && scope.name) {
2016-03-11 00:48:53 +00:00
res = l10n[scope.name + '.' + prop];
2020-04-29 17:54:48 +00:00
if ( !res && scope.default )
res = scope.default;
2016-03-11 00:48:53 +00:00
}
return res || (scope ? eval(scope.name).prototype[prop] : '');
};
2019-12-10 14:22:28 +00:00
var _getCurrentLanguage = function() {
return currentLang;
};
var _getLoadedLanguage = function() {
return loadedLang;
2019-12-10 14:22:28 +00:00
};
2016-03-11 00:48:53 +00:00
var _getUrlParameterByName = function(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
};
2021-11-13 09:31:50 +00:00
var _requireLang = function (l) {
var lang = (l || _getUrlParameterByName('lang') || defLang).split(/[\-_]/)[0];
2019-12-10 14:22:28 +00:00
currentLang = lang;
2019-02-01 16:51:48 +00:00
fetch('locale/' + lang + '.json')
.then(function(response) {
if (!response.ok) {
currentLang = defLang;
if (lang != defLang)
2019-02-01 16:51:48 +00:00
/* load default lang if fetch failed */
return fetch('locale/' + defLang + '.json');
2019-02-01 16:51:48 +00:00
throw new Error('server error');
}
return response.json();
}).then(function(response) {
if ( response.json ) {
if (!response.ok)
throw new Error('server error');
2019-02-01 16:51:48 +00:00
return response.json();
} else {
2019-02-01 16:51:48 +00:00
l10n = response;
/* to break promises chain */
throw new Error('loaded');
}
}).then(function(json) {
l10n = json || {};
apply && _applyLocalization();
2019-02-01 16:51:48 +00:00
}).catch(function(e) {
2021-11-16 15:30:26 +00:00
if ( !/loaded/.test(e) && currentLang != defLang && defLang && defLang.length < 3 ) {
2021-11-13 09:31:50 +00:00
return setTimeout(function(){
_requireLang(defLang)
}, 0);
}
l10n = l10n || {};
apply && _applyLocalization();
2019-02-04 12:49:41 +00:00
if ( e.message == 'loaded' ) {
} else {
currentLang = null;
2019-02-04 12:49:41 +00:00
console.log('fetch error: ' + e);
}
2019-02-01 16:51:48 +00:00
});
};
if ( !window.fetch ) {
/* use fetch polifill if native method isn't supported */
var polyfills = ['../vendor/fetch/fetch.umd'];
if ( !window.Promise ) {
require(['../vendor/es6-promise/es6-promise.auto.min'],
function () {
require(polyfills, _requireLang);
});
} else require(polyfills, _requireLang);
2019-02-01 16:51:48 +00:00
} else _requireLang();
2016-03-11 00:48:53 +00:00
return {
apply: _applyLocalization,
2019-12-10 14:22:28 +00:00
get: _get,
getCurrentLanguage: _getCurrentLanguage
2016-03-11 00:48:53 +00:00
};
})();