Created
January 25, 2018 22:36
-
-
Save bludnic/ce058d818bd817c9e407c23e6134a3ef to your computer and use it in GitHub Desktop.
Multilingual schema mongoose plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mongoose from 'mongoose' | |
import traverse from 'traverse' | |
const bodyToLocalized = (body, fields, lang = 'en') => { | |
fields.forEach(field => { | |
if (field in body) { | |
body[field + '.' + lang] = body[field] | |
delete body[field] | |
} | |
}) | |
return body | |
} | |
const localizedToBody = (body, fields, lang = 'en') => { | |
fields.forEach(field => { | |
if (field in body) { | |
if (lang in body[field]) { | |
body[field] = body[field][lang] | |
} else { | |
body[field] = '' // ?? if array? | |
} | |
} | |
}) | |
return body | |
} | |
const toLocalized = (obj, lang) => { | |
traverse(obj).forEach(function (item) { | |
if (this.key === lang) { | |
this.parent.update(item) | |
} | |
}) | |
return obj | |
} | |
const toLocalized2 = (obj, fields, lang) => { | |
console.log('paths', traverse(obj).paths()) | |
} | |
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) { | |
if (schema.options.i18n) { | |
delete schema.options.i18n | |
Schema.remove(path) | |
let langObject = {} | |
options.languages.map(lang => { | |
langObject[lang] = schema.options | |
}) | |
Schema.add({ [path]: langObject }) | |
} else { | |
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') | |
//console.log('pathname:options', Schema.paths[path].options) | |
delete schema.options.i18n | |
Schema.remove(path) | |
Schema.add({ | |
[path]: i18nObject(options.languages) | |
}) | |
//console.log('path', path); | |
} | |
} 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') | |
} | |
}) | |
} | |
const setLang = (Obj, key, lang = 'en') => { | |
if (Array.isArray(Obj)) { | |
Obj.map((key) => setLang(Obj[key], key, lang)) | |
} else if (typeof Obj === 'object') { | |
//Object.keys(Obj).map((key) => setLang(Obj[key], key, lang)) | |
console.log(Obj[lang], key) | |
} | |
} | |
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); | |
} | |
export { | |
I18n, | |
toLocalized, | |
toLocalized2, | |
bodyToLocalized, | |
localizedToBody | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment