UNPKG

i18nucleus-mongoose

Version:
166 lines (150 loc) 4.51 kB
var pjson = require('./package.json') , _ = require('underscore') , _s = require('underscore.string') , async = require('breeze-async') , termsModel = require('./models/terms') , defineOptions = require('./options'); function i18nucleus_mongoose () { var version = pjson.version; var type = 'storage'; var name = pjson.name; var options = {}; var utils = null; var parentOptions = null; var MwareError = null; var termModel = null; function setOptions (_options, _utils) { utils = _utils; var optionObj = defineOptions(_options, utils); if (!optionObj.err) { options = optionObj.options; termModel = options.termModel = termsModel(options.mongoose, options.termsModelName); } return optionObj; } function init (_parentOptions, _MwareError, cb) { parentOptions = _parentOptions; MwareError = _MwareError; cb(); } function loadTranslations (cb) { var namespaces = parentOptions.namespaces , translations = {} , locale , ns , md5; termModel.find({} ,function (err, results) { if (err) return cb(new MwareError(err, name)); function iterator (doc, next) { locale = doc.lng; ns = doc.namespace; md5 = doc.md5; if (namespaces.length && namespaces.indexOf(ns) == -1) return next(); if (!translations[locale]) translations[locale] = {}; translations[locale][ns] = {}; translations[locale][ns].resources = doc.resources; if (md5 && md5.length == 32) { translations[locale][ns].md5 = doc.md5; next(); } else { // Set md5 field in case it is not defined yet md5 = utils.createMd5Hash(doc.resources); termModel.findOneAndUpdate({ _id: ns + '_' + locale }, { md5: md5 }, { upsert: true }, function (err, results) { if (err) return next(new MwareError(err, name)); translations[locale][ns].md5 = md5; next(); }); } } function finish (err) { if (err) return cb(err); cb(null, translations); } async.forEach( results , iterator , finish ); }); } function getTermsStorageObject () { return termModel; } function getLocalizedNsTranslations (lng, ns, cb) { var namespaces = parentOptions.namespaces , id = ns + '_' + lng; if (namespaces.length && namespaces.indexOf(ns) == -1) return cb(); termModel.find({ _id: id }, 'resources md5', function (err, results) { if (err) return cb(new MwareError(err, name)); results = results[0]; cb(null, { resources: results.resources , md5: results.md5 }); }); } function getTranslationsSnapshot (cb) { var namespaces = parentOptions.namespaces , snapshot = [] , ns; termModel.find({}, 'lng namespace md5', function (err, results) { if (err) return cb(new MwareError(err, name)); results.forEach(function (doc) { ns = doc.namespace; if (namespaces.length && namespaces.indexOf(ns) != -1) { snapshot.push({ lng: doc.lng , ns: ns , md5: doc.md5 }); } }); cb(null, snapshot); }); } function getDefaultResources (defaultLng, cb) { termModel.find({ lng: defaultLng }, function (err, results) { if (err) return cb(new MwareError(err, name)); cb(null, results); }); } function updateLocaleResources (locale, namespace, translations, cb) { var id = namespace + '_' + locale; termModel.findOneAndUpdate({ _id: id }, { lng: locale , namespace: namespace , resources: translations , md5: utils.createMd5Hash(translations) }, { upsert: true }, function (err, results) { if (err) return cb(new MwareError(err, name)); cb(null, results); }); } return { version: version , type: type , name: name , setOptions: setOptions , init: init , options: options , loadTranslations: loadTranslations , getTermsStorageObject: getTermsStorageObject , getLocalizedNsTranslations: getLocalizedNsTranslations , getTranslationsSnapshot: getTranslationsSnapshot , getDefaultResources: getDefaultResources , updateLocaleResources: updateLocaleResources } } module.exports = i18nucleus_mongoose