simple-body-validator
Version:
This package is inspired by Laravel validation, and aims to make body validation easier for Javascript developers
100 lines (99 loc) • 3.25 kB
JavaScript
'use strict';
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const object_1 = require("./utils/object");
const index_1 = __importDefault(require("./locales/index"));
const lang = {
/**
* Default lang to be used, when lang is not specified
*/
defaultLang: 'en',
/**
* Determines the locale to be used when tu current one is not available
*/
fallbackLang: 'en',
/**
* The existing langs that are supported by the library
*/
existingLangs: ['en'],
/**
* Store the translations passed by the user
*/
translations: {},
/**
* Stores the messages that are already loaded
*/
messages: {},
/**
* Stores the default messages
*/
defaultMessages: {},
/**
* Stores the fallback messages
*/
fallbackMessages: index_1.default.en,
/**
* Get messages for lang
*/
get(lang = this.defaultLang) {
this.load(lang);
return this.messages[lang];
},
/**
* Set the translation object passed by the user
*/
setTranslationObject(translations) {
this.translations = translations;
this.setDefaultLang(this.defaultLang);
},
/**
* Set the default lang that should be used. And assign the default messages
*/
setDefaultLang(lang) {
this.defaultLang = lang;
this.load(lang);
},
/**
* Set the fallback lang to be used. And assign the fallback messages
*/
setFallbackLang(lang) {
this.fallbackLang = lang;
this.fallbackMessages = index_1.default.en;
// check if the lang translations exist in the library and load them
if (index_1.default.hasOwnProperty(lang)) {
this.fallbackMessages = (0, object_1.mergeDeep)(this.fallbackMessages, index_1.default[lang]);
}
// check if the lang translations exit in the object passed by the user
if (this.translations.hasOwnProperty(lang)) {
this.fallbackMessages = (0, object_1.mergeDeep)(this.fallbackMessages, this.translations[lang]);
}
},
/**
* Get the default language
*/
getDefaultLang() {
return this.defaultLang;
},
/**
* Load the messages based on the specified language
*/
load(lang) {
if (this.messages[lang]) {
return;
}
// check if the lang translations exist in the library and load them
if (index_1.default.hasOwnProperty(lang)) {
this.messages[lang] = (0, object_1.mergeDeep)(this.fallbackMessages, index_1.default[lang]);
}
else {
this.messages[lang] = (0, object_1.mergeDeep)({}, this.fallbackMessages);
}
// check if the lang translations exist in the object passed by the user
if (this.translations.hasOwnProperty(lang)) {
this.messages[lang] = (0, object_1.mergeDeep)(this.messages[lang], this.translations[lang]);
}
}
};
exports.default = lang;