@totvs-agro/core-mobile
Version:
Core Mobile Totvs Agro (Front-End) para utilização dos estilos do T-Faces
177 lines • 7.88 kB
JavaScript
import { Injectable } from "@angular/core";
import { TranslateService } from '@ngx-translate/core';
import { Http } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
var i18nDefault = {};
var TotvsTranslateProvider = /** @class */ (function () {
function TotvsTranslateProvider(_translateNgx, http) {
this._translateNgx = _translateNgx;
this.http = http;
this.i18n = i18nDefault;
this.getFromBetween = {
results: [],
string: "",
getFromBetween: function (sub1, sub2) {
if (this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0)
return false;
var SP = this.string.indexOf(sub1) + sub1.length;
var string1 = this.string.substr(0, SP);
var string2 = this.string.substr(SP);
var TP = string1.length + string2.indexOf(sub2);
return this.string.substring(SP, TP);
},
removeFromBetween: function (sub1, sub2) {
if (this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0)
return false;
var removal = sub1 + this.getFromBetween(sub1, sub2) + sub2;
this.string = this.string.replace(removal, "");
},
getAllResults: function (sub1, sub2) {
// first check to see if we do have both substrings
if (this.string.indexOf(sub1) < 0 || this.string.indexOf(sub2) < 0)
return;
// find one result
var result = this.getFromBetween(sub1, sub2);
// push it to the results array
this.results.push(result);
// remove the most recently found one from the string
this.removeFromBetween(sub1, sub2);
// if there's more substrings
if (this.string.indexOf(sub1) > -1 && this.string.indexOf(sub2) > -1) {
this.getAllResults(sub1, sub2);
}
else
return;
},
get: function (string, sub1, sub2) {
this.results = [];
this.string = string;
this.getAllResults(sub1, sub2);
return this.results;
}
};
}
//PUBLICOS
//PUBLICOS
TotvsTranslateProvider.prototype.initialize =
//PUBLICOS
function (_language, _component) {
var _this = this;
if (null != _language && undefined != _language && "" != _language)
this.language = _language;
else
this.language = this._translateNgx.currentLang.substring(0, 2);
this.getJSONDataAsync("../../assets/i18n/" + _component + "/pt.json").then(function (data) {
_this.i18n[_component]["pt"] = data;
return _this.getJSONDataAsync("../assets/i18n/" + _component + "/es.json");
}).then(function (data) {
_this.i18n[_component]["es"] = data;
return _this.getJSONDataAsync("../assets/i18n/" + _component + "/en.json");
}).then(function (data) {
_this.i18n[_component]["en"] = data;
});
};
;
TotvsTranslateProvider.prototype.instant = function (message, _component) {
return this.findMessage(message, _component);
};
;
TotvsTranslateProvider.prototype.get = function (keys, _component, interpolateParams) {
var retorno = {};
var translatedKey = "";
var paramsFromTranslatedKey = "";
if (!(typeof keys === 'string')) {
for (var key in keys) {
translatedKey = this.instant(keys[key], _component);
paramsFromTranslatedKey = this.getFromBetween.get(translatedKey, "{{", "}}");
translatedKey = this.fillParameters(paramsFromTranslatedKey, translatedKey, _component, interpolateParams);
retorno[keys[key]] = translatedKey;
}
return retorno;
}
else {
translatedKey = this.instant(String(keys), _component);
paramsFromTranslatedKey = this.getFromBetween.get(translatedKey, "{{", "}}");
translatedKey = this.fillParameters(paramsFromTranslatedKey, translatedKey, _component, interpolateParams);
return translatedKey;
}
};
;
//PRIVADOS
//PRIVADOS
TotvsTranslateProvider.prototype.findMessage =
//PRIVADOS
function (_message, _component) {
//let language: string = this._translateNgx.currentLang.substring(0, 2);
try {
if (undefined != this.i18n && null != this.i18n) {
var msg = this.i18n[_component][this.language];
var split = String(_message).split(".");
for (var m in split) {
if (undefined != msg[split[m]] && null != msg[split[m]])
msg = msg[split[m]];
else
msg = _message;
}
return msg;
}
}
catch (e) {
return _message;
}
};
;
TotvsTranslateProvider.prototype.fillParameters = function (paramsFromTranslatedKey, translatedKey, component, interpolateParams) {
for (var _i = 0, paramsFromTranslatedKey_1 = paramsFromTranslatedKey; _i < paramsFromTranslatedKey_1.length; _i++) {
var paramFromTranslatedKey = paramsFromTranslatedKey_1[_i];
var valueFromParamFromTranslatedKey = paramFromTranslatedKey;
if (null != interpolateParams && undefined != interpolateParams && interpolateParams.hasOwnProperty(paramFromTranslatedKey))
valueFromParamFromTranslatedKey = interpolateParams[paramFromTranslatedKey];
translatedKey = translatedKey.replace("{{" + paramFromTranslatedKey + "}}", this.instant(valueFromParamFromTranslatedKey, component));
}
return translatedKey;
};
;
TotvsTranslateProvider.prototype.getJSONDataAsync = function (filePath) {
var _this = this;
return new Promise(function (resolve, reject) {
_this.http.get(filePath)
.subscribe(function (res) {
if (!res.ok) {
reject("Failed with status: " + res.status + "\nTrying to find fil at " + filePath);
}
var jsonRes = res.json();
resolve(jsonRes);
});
}).catch(function (reason) { return _this.handleError(reason); });
};
/* Takes an error, logs it to the console, and throws it */
/* Takes an error, logs it to the console, and throws it */
TotvsTranslateProvider.prototype.handleError = /* Takes an error, logs it to the console, and throws it */
function (error) {
var errMsg;
if (error instanceof Response) {
var body = error.json() || '';
var err = JSON.stringify(body);
errMsg = error.status + " - " + (error.statusText || '') + " " + err;
}
else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
};
TotvsTranslateProvider.decorators = [
{ type: Injectable },
];
/** @nocollapse */
TotvsTranslateProvider.ctorParameters = function () { return [
{ type: TranslateService, },
{ type: Http, },
]; };
return TotvsTranslateProvider;
}());
export { TotvsTranslateProvider };
//# sourceMappingURL=totvs-translate-provider.js.map