Skip to content

Instantly share code, notes, and snippets.

@bludnic
Last active January 24, 2018 13:46
Show Gist options
  • Save bludnic/4a3aaf0e88fbb4b253e33059ced500a5 to your computer and use it in GitHub Desktop.
Save bludnic/4a3aaf0e88fbb4b253e33059ced500a5 to your computer and use it in GitHub Desktop.
Mongoose i18n plugin
const mongoose = require('mongoose');
const mapObject = (Obj) => {
if (Array.isArray(Obj)) {
} else if(typeof Obj === 'object') {
} else {
}
}
const toLocalize = (doc, lang) => {
if(typeof doc.toJSON === 'function') doc = doc.toJSON()
if(Array.isArray(doc)) {
let doc2 = []
doc.map((item, index) => {
doc2[index] = toLocalize(item, lang)
})
return doc2
} else if (typeof doc === 'object') {
let doc2 = {}
Object.keys(doc).map((index) => {
if (index === lang) {
doc2 = doc[lang]
} else {
if (doc[index] !== null) {
doc2[index] = toLocalize(doc[index], lang)
} else {
doc2 = ''
}
}
})
return doc2
} else {
return doc
}
}
const bodyLocalized = (body, fields = [], lang = 'en') => {
let bodyLang = {}
// const local = (obj) => {
// if (Array.isArray(obj)) {
// } else if (typeof obj === 'object') {
// } else {
// }
// }
fields.map(field => {
const [arrayName, arrayKey] = field.split('.$.')
if (arrayKey !== undefined) {
console.log(body[arrayName], arrayName);
if (body[arrayName] === undefined) {
return
} else if (Array.isArray(body[arrayName])) {
console.log('array', body[arrayName]);
body[arrayName].map((item, index) => {
bodyLang[arrayName + '.' + index + '.' + arrayKey + '.' + lang] = item[arrayKey]
})
} else if (typeof body[arrayName] === 'object') {
Object.keys(body[arrayName]).map((item, index) => {
bodyLang[arrayName + '.' + index + '.' + arrayKey + '.' + lang] = item[arrayKey]
})
}
} else if (body[field] !== undefined) {
bodyLang[field + '.' + lang] = body[field]
} else {
bodyLang[field] = body[field]
}
})
return bodyLang
}
const i18n = (Schema, options) => {
const i18nObject = (languages) => {
let obj = {}
languages.map(lang => obj[lang] = String)
return obj
}
const megaMap = (Schema, startPath = '') => {
Schema.eachPath((path, schema) => {
if (schema instanceof mongoose.Schema.Types.DocumentArray) {
//console.log(path, 'DocumentArray')
megaMap(schema.schema, path)
} else if (schema instanceof mongoose.Schema.Types.Array) {
//console.log(path, 'Array')
} else if (schema instanceof mongoose.Schema.Types.String) {
//console.log(path, 'String')
if (schema.options.i18n) {
// let fullPath = (startPath) ? startPath + '.' + path : path
// console.log(fullPath, 'String', 'i18n')
delete schema.options.i18n
Schema.remove(path)
Schema.add({
[path]: i18nObject(options.languages)
})
}
} else if (schema instanceof mongoose.Schema.Types.Number) {
//console.log(path, 'Number')
} else if (schema instanceof mongoose.Schema.Types.Date) {
//console.log(path, 'Date')
} else {
//console.log(path, 'Other')
}
})
}
megaMap(Schema)
// setLang(Schema, 'ru')
// console.log(Schema.paths);
// Schema.methods.toJSONLocalized = function() {
// setLang(this, null, 'en')
// console.log(this);
// }
//console.log('obj', Schema.paths.included.schema.paths);
}
module.exports = {
i18n,
toLocalize,
bodyLocalized
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment