Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
edulix committed Feb 8, 2024
1 parent b679f73 commit 9cd7981
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 108 deletions.
90 changes: 7 additions & 83 deletions avUi/i18n-override-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,75 +38,18 @@ angular
'I18nOverride',
function($i18next, $rootScope, $window)
{
function expandObject(obj)
{
var result = {};

// Helper function to handle the recursion
function assignValue(ref, keys, value) {
var key = keys.shift(); // Get the current key part
if (keys.length === 0) {
// If no more keys, assign the value directly
ref[key] = value;
} else {
// Prepare the next level sub-object if necessary
if (!ref[key]) {
ref[key] = {};
}
// Recurse with the next level of the key and the corresponding sub-object
assignValue(ref[key], keys, value);
}
}

// Iterate over each property in the input object
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
var keys = prop.split('.'); // Split the property by dots into parts
assignValue(result, keys, obj[prop]); // Use the helper to assign the value in the result object
}
}

return result;
}
return function (overrides, force, languagesConf)
{
force = angular.isDefined(force) ? force : false;
if (overrides !== null) {
if (languagesConf && languagesConf.available_languages)
{
// We use deep extend to have as an override for all available
// languages the original changed with the override
angular.merge(
overrides,
_.object(_.map(
languagesConf.available_languages,
function(key) { return [key, {}]; }
))
);
}
$i18next.options.useLocalStorage = true;
$window.i18next.options.useLocalStorage = true;
overrides = _.mapObject(overrides, function(obj, langCode) {
var original = {};
if ($window.i18nOriginal && $window.i18nOriginal[langCode]) {
original = $window.i18nOriginal[langCode];
}
var override = expandObject(obj);
var merged = angular.merge({}, original, override);
return merged;
});
} else {
overrides = $window.i18nOverride;
}

// reset $window.i18nOverride
var performOverrides = false;
if (overrides) {
if (overrides !== null) {
performOverrides = (
force ||
JSON.stringify(overrides) !== JSON.stringify($window.i18nOverride)
);
$window.i18nOverride = overrides;
);
if (performOverrides) {
$window.i18nOverride = overrides;
}
}

if (languagesConf)
Expand All @@ -123,30 +66,11 @@ angular
$i18next.options.preload = languagesConf.available_languages;
$i18next.options.fallbackLng = [languagesConf.default_language, 'en'];
}
console.log("calling reloadResources()..");
console.log("calling $window.i18next.reloadResources()..");
$window.i18next
.reloadResources()
.then(function () {
console.log("reloadResources: successful. Now checking overrides");
// load i18n_overrides if any
if (performOverrides)
{
console.log("reloadResources: adding overrides");
_.map(
$window.i18nOverride,
function (i18nOverride, language)
{
$window.i18next.addResourceBundle(
/* lng = */ language,
/* ns = */ "translation",
/* resources = */ i18nOverride,
/* deep */ true,
/* overwrite */ true
);
}
);
}
console.log("reloadResources: broadcast i18nextLanguageChange");
console.log("reloadResources: successful. broadcast i18nextLanguageChange signal");
$rootScope.$broadcast('i18nextLanguageChange', $i18next.options.lng);
});
};
Expand Down
31 changes: 6 additions & 25 deletions dist/appCommon-v10.1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -1121,34 +1121,15 @@ angular.module("avRegistration").config(function() {}), angular.module("avRegist
});
};
} ]), angular.module("avUi").service("I18nOverride", [ "$i18next", "$rootScope", "$window", function($i18next, $rootScope, $window) {
function expandObject(obj) {
var prop, result = {};
for (prop in obj) obj.hasOwnProperty(prop) && !function assignValue(ref, keys, value) {
var key = keys.shift();
0 === keys.length ? ref[key] = value : (ref[key] || (ref[key] = {}), assignValue(ref[key], keys, value));
}(result, prop.split("."), obj[prop]);
return result;
}
return function(overrides, force, languagesConf) {
force = !!angular.isDefined(force) && force, overrides = null !== overrides ? (languagesConf && languagesConf.available_languages && angular.merge(overrides, _.object(_.map(languagesConf.available_languages, function(key) {
return [ key, {} ];
}))), $i18next.options.useLocalStorage = !0, $window.i18next.options.useLocalStorage = !0,
_.mapObject(overrides, function(override, langCode) {
var original = {};
$window.i18nOriginal && $window.i18nOriginal[langCode] && (original = $window.i18nOriginal[langCode]);
override = expandObject(override);
return angular.merge({}, original, override);
})) : $window.i18nOverride;
var performOverrides = !1;
overrides && (performOverrides = force || JSON.stringify(overrides) !== JSON.stringify($window.i18nOverride),
$window.i18nOverride = overrides), languagesConf && ($i18next.options.lng = languagesConf.force_default_language ? languagesConf.default_language : $window.i18next.resolvedLanguage,
force = !!angular.isDefined(force) && force;
null === overrides || !force && JSON.stringify(overrides) === JSON.stringify($window.i18nOverride) || ($window.i18nOverride = overrides),
languagesConf && ($i18next.options.lng = languagesConf.force_default_language ? languagesConf.default_language : $window.i18next.resolvedLanguage,
$i18next.options.lngWhitelist = languagesConf.available_languages, $i18next.options.preload = languagesConf.available_languages,
$i18next.options.fallbackLng = [ languagesConf.default_language, "en" ]), console.log("calling reloadResources().."),
$i18next.options.fallbackLng = [ languagesConf.default_language, "en" ]), console.log("calling $window.i18next.reloadResources().."),
$window.i18next.reloadResources().then(function() {
console.log("reloadResources: successful. Now checking overrides"), performOverrides && (console.log("reloadResources: adding overrides"),
_.map($window.i18nOverride, function(i18nOverride, language) {
$window.i18next.addResourceBundle(language, "translation", i18nOverride, !0, !0);
})), console.log("reloadResources: broadcast i18nextLanguageChange"), $rootScope.$broadcast("i18nextLanguageChange", $i18next.options.lng);
console.log("reloadResources: successful. broadcast i18nextLanguageChange signal"),
$rootScope.$broadcast("i18nextLanguageChange", $i18next.options.lng);
});
};
} ]), angular.module("avUi").directive("avChangeLang", [ "$i18next", "ipCookie", "angularLoad", "amMoment", "$rootScope", "ConfigService", "$window", "I18nOverride", "Authmethod", function($i18next, ipCookie, angularLoad, amMoment, $rootScope, ConfigService, $window, I18nOverride, Authmethod) {
Expand Down
131 changes: 131 additions & 0 deletions dist/libCommon-v10.1.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -17152,6 +17152,137 @@ function(angular) {
}, {} ],
5: [ function(require, module, exports) {}, {} ]
}, {}, [ 2 ])(2);
}), function(global, factory) {
"object" == typeof exports && "undefined" != typeof module ? module.exports = factory() : "function" == typeof define && define.amd ? define(factory) : (global = "undefined" != typeof globalThis ? globalThis : global || self).i18nextChainedBackend = factory();
}(this, function() {
"use strict";
function _typeof(o) {
return (_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(o) {
return typeof o;
} : function(o) {
return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o;
})(o);
}
function _toPropertyKey(key) {
key = function(input, res) {
if ("object" !== _typeof(input) || null === input) return input;
var prim = input[Symbol.toPrimitive];
if (void 0 === prim) return ("string" === res ? String : Number)(input);
if ("object" !== _typeof(res = prim.call(input, res || "default"))) return res;
throw new TypeError("@@toPrimitive must return a primitive value.");
}(key, "string");
return "symbol" === _typeof(key) ? key : String(key);
}
function _defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0,
"value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
}
}
var Backend = [], each = Backend.forEach, slice = Backend.slice;
function handleCorrectReadFunction(backend, language, namespace, resolver) {
var fc = backend.read.bind(backend);
if (2 !== fc.length) fc(language, namespace, resolver); else try {
var r = fc(language, namespace);
r && "function" == typeof r.then ? r.then(function(data) {
return resolver(null, data);
}).catch(resolver) : resolver(null, r);
} catch (err) {
resolver(err);
}
}
Backend = function() {
function Backend(services) {
var options = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {}, i18nextOptions = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : {};
!function(instance) {
if (!(instance instanceof Backend)) throw new TypeError("Cannot call a class as a function");
}(this), this.backends = [], this.type = "backend", this.allOptions = i18nextOptions,
this.init(services, options);
}
var Constructor, protoProps, staticProps;
return Constructor = Backend, (protoProps = [ {
key: "init",
value: function(services) {
var _this = this, options = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {}, i18nextOptions = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : {};
this.services = services, this.options = function(obj) {
return each.call(slice.call(arguments, 1), function(source) {
if (source) for (var prop in source) void 0 === obj[prop] && (obj[prop] = source[prop]);
}), obj;
}(options, this.options || {}, {
handleEmptyResourcesAsFailed: !0,
cacheHitMode: "none"
}), this.allOptions = i18nextOptions, this.options.backends && this.options.backends.forEach(function(ClassOrObject, i) {
_this.backends[i] = _this.backends[i] || ((ClassOrObject = ClassOrObject) ? "function" == typeof ClassOrObject ? new ClassOrObject() : ClassOrObject : null),
_this.backends[i].init(services, _this.options.backendOptions && _this.options.backendOptions[i] || {}, i18nextOptions);
}), this.services && this.options.reloadInterval && setInterval(function() {
return _this.reload();
}, this.options.reloadInterval);
}
}, {
key: "read",
value: function(language, namespace, callback) {
function loadPosition(pos) {
if (bLen <= pos) return callback(new Error("non of the backend loaded data", !0));
var isLastBackend = pos === bLen - 1, lengthCheckAmount = _this2.options.handleEmptyResourcesAsFailed && !isLastBackend ? 0 : -1, backend = _this2.backends[pos];
backend.read ? handleCorrectReadFunction(backend, language, namespace, function(err, data, nextBackend) {
!err && data && Object.keys(data).length > lengthCheckAmount ? (callback(null, data, pos),
savePosition(pos - 1, data), backend.save && _this2.options.cacheHitMode && -1 < [ "refresh", "refreshAndUpdateStore" ].indexOf(_this2.options.cacheHitMode) && (nextBackend && _this2.options.refreshExpirationTime && nextBackend + _this2.options.refreshExpirationTime > Date.now() || (nextBackend = _this2.backends[pos + 1]) && nextBackend.read && handleCorrectReadFunction(nextBackend, language, namespace, function(err, data) {
err || data && (Object.keys(data).length <= lengthCheckAmount || (savePosition(pos, data),
"refreshAndUpdateStore" === _this2.options.cacheHitMode && _this2.services && _this2.services.resourceStore && _this2.services.resourceStore.addResourceBundle(language, namespace, data)));
}))) : loadPosition(pos + 1);
}) : loadPosition(pos + 1);
}
var _this2 = this, bLen = this.backends.length, savePosition = function savePosition(pos, data) {
var backend;
pos < 0 || ((backend = _this2.backends[pos]).save && backend.save(language, namespace, data),
savePosition(pos - 1, data));
};
loadPosition(0);
}
}, {
key: "create",
value: function(languages, namespace, key, fallbackValue) {
var clb = 4 < arguments.length && void 0 !== arguments[4] ? arguments[4] : function() {}, opts = 5 < arguments.length && void 0 !== arguments[5] ? arguments[5] : {};
this.backends.forEach(function(b) {
if (b.create) {
var fc = b.create.bind(b);
if (fc.length < 6) try {
var r = 5 === fc.length ? fc(languages, namespace, key, fallbackValue, opts) : fc(languages, namespace, key, fallbackValue);
r && "function" == typeof r.then ? r.then(function(data) {
return clb(null, data);
}).catch(clb) : clb(null, r);
} catch (err) {
clb(err);
} else fc(languages, namespace, key, fallbackValue, clb, opts);
}
});
}
}, {
key: "reload",
value: function() {
var toLoad, _this3 = this, append = this.services, backendConnector = append.backendConnector, languageUtils = append.languageUtils, logger = append.logger, currentLanguage = backendConnector.language;
currentLanguage && "cimode" === currentLanguage.toLowerCase() || (toLoad = [], (append = function(lng) {
languageUtils.toResolveHierarchy(lng).forEach(function(l) {
toLoad.indexOf(l) < 0 && toLoad.push(l);
});
})(currentLanguage), this.allOptions.preload && this.allOptions.preload.forEach(append),
toLoad.forEach(function(lng) {
_this3.allOptions.ns.forEach(function(ns) {
backendConnector.read(lng, ns, "read", null, null, function(err, data) {
err && logger.warn("loading namespace ".concat(ns, " for language ").concat(lng, " failed"), err),
!err && data && logger.log("loaded namespace ".concat(ns, " for language ").concat(lng), data),
backendConnector.loaded("".concat(lng, "|").concat(ns), err, data);
});
});
}));
}
} ]) && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps),
Object.defineProperty(Constructor, "prototype", {
writable: !1
}), Backend;
}();
return Backend.type = "backend", Backend;
}), function(global, factory) {
"object" == typeof exports && "undefined" != typeof module ? module.exports = factory(require("angular")) : "function" == typeof define && define.amd ? define([ "angular" ], factory) : global.ngI18next = factory(global.angular);
}(this, function(angular) {
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<script src="node_modules/angular-cookie/angular-cookie.js" class="lib"></script>
<script src="node_modules/angular-load/angular-load.js" class="lib"></script>
<script src="node_modules/i18next-http-backend/i18nextHttpBackend.js" class="lib"></script>
<script src="node_modules/i18next-chained-backend/i18nextChainedBackend.js" class="lib"></script>
<script src="node_modules/ng-i18next/dist/ng-i18next.js" class="lib"></script>
<script src="node_modules/angular-drag-and-drop-lists/angular-drag-and-drop-lists.js" class="lib"></script>
<script src="node_modules/@bower_components/ngInfiniteScroll/build/ng-infinite-scroll.js" class="lib"></script>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ng-i18next": "1.0.7",
"i18next": "23.8.2",
"i18next-http-backend": "2.4.3",
"i18next-chained-backend": "4.6.2",
"angular": "~1.8.3",
"angular-animate": "~1.8.0",
"angular-cookie": "~4.0.9",
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,13 @@ human-signals@^1.1.1:
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"
integrity sha512-SEQu7vl8KjNL2eoGBLF3+wAjpsNfA9XMlXAYj/3EdaNfAlxKthD1xjEQfGOUhllCGGJVNY34bRr6lPINhNjyZw==

[email protected]:
version "4.6.2"
resolved "https://registry.yarnpkg.com/i18next-chained-backend/-/i18next-chained-backend-4.6.2.tgz#96bd1fd8c0f719154040665b435cf3f032d7ccec"
integrity sha512-2P092fR+nAPQlGzPUoIIxbwo7PTBqQYgLxwv1XhSTQUAUoelLo5LkX+FqRxxSDg9WEAsrc8+2WL6mJtMGIa6WQ==
dependencies:
"@babel/runtime" "^7.23.2"

[email protected]:
version "2.4.3"
resolved "https://registry.yarnpkg.com/i18next-http-backend/-/i18next-http-backend-2.4.3.tgz#be9a0aaff5f34542f1f9c1f7bd03eb96d8476dbd"
Expand Down

0 comments on commit 9cd7981

Please sign in to comment.