enables retrieving all locales
This commit is contained in:
@@ -8,13 +8,16 @@ import languageParser from 'accept-language-parser';
|
||||
*/
|
||||
export default function localizationMiddleware(localization) {
|
||||
const middleware = (req, res, next) => {
|
||||
if (req.query.locale === '*' || req.query.locale === 'all') {
|
||||
req.locale = 'all';
|
||||
return next();
|
||||
}
|
||||
let setLocale;
|
||||
if (req.query.locale) {
|
||||
setLocale = localization.locales.find(search => search === req.query.locale);
|
||||
if (setLocale) {
|
||||
req.locale = setLocale;
|
||||
}
|
||||
if (req.query.locale === '*' || req.query.locale === 'all') return next();
|
||||
}
|
||||
if (req.body.locale) {
|
||||
setLocale = localization.locales.find(search => search === req.body.locale);
|
||||
@@ -22,9 +25,10 @@ export default function localizationMiddleware(localization) {
|
||||
req.locale = setLocale;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!req.locale && req.headers['accept-language']) req.locale = languageParser.pick(localization.locales, req.headers['accept-language']);
|
||||
if (!req.locale) req.locale = localization.defaultLocale;
|
||||
|
||||
return next();
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,58 @@
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
/* eslint-disable func-names */
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
function formatRefPathLocales(schema, parentSchema, parentPath) {
|
||||
// Loop through all refPaths within schema
|
||||
schema.eachPath((pathname, schemaType) => {
|
||||
// If a dynamic refPath is found
|
||||
if (schemaType.options.refPath && schemaType.options.refPath.includes('{{LOCALE}}') && parentSchema) {
|
||||
// Create a clone of the schema for each locale
|
||||
const newSchema = schema.clone();
|
||||
|
||||
// Remove the old pathname in order to rebuild it after it's formatted
|
||||
newSchema.remove(pathname);
|
||||
|
||||
// Get the locale from the parent path
|
||||
let locale = parentPath;
|
||||
|
||||
// Split the parent path and take only the last segment as locale
|
||||
if (parentPath && parentPath.includes('.')) {
|
||||
locale = parentPath.split('.').pop();
|
||||
}
|
||||
|
||||
// Replace {{LOCALE}} appropriately
|
||||
const refPath = schemaType.options.refPath.replace('{{LOCALE}}', locale);
|
||||
|
||||
// Add new schemaType back to newly cloned schema
|
||||
newSchema.add({
|
||||
[pathname]: {
|
||||
...schemaType.options,
|
||||
refPath,
|
||||
},
|
||||
});
|
||||
|
||||
// Removing and adding a path to a schema does not update tree, so do it manually
|
||||
newSchema.tree[pathname].refPath = refPath;
|
||||
|
||||
const parentSchemaType = parentSchema.path(parentPath).instance;
|
||||
|
||||
// Remove old schema from parent
|
||||
parentSchema.remove(parentPath);
|
||||
|
||||
// Replace newly cloned and updated schema on parent
|
||||
parentSchema.add({
|
||||
[parentPath]: parentSchemaType === 'Array' ? [newSchema] : newSchema,
|
||||
});
|
||||
}
|
||||
|
||||
// If nested schema found, continue recursively
|
||||
if (schemaType.schema) {
|
||||
formatRefPathLocales(schemaType.schema, schema, pathname);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export default function localizationPlugin(schema, options) {
|
||||
if (!options || !options.locales || !Array.isArray(options.locales) || !options.locales.length) {
|
||||
throw new mongoose.Error('Required locales array is missing');
|
||||
@@ -56,6 +109,10 @@ export default function localizationPlugin(schema, options) {
|
||||
|
||||
const value = localeSubDoc[locale];
|
||||
|
||||
if (locale === 'all') {
|
||||
return value;
|
||||
}
|
||||
|
||||
// If there is no value to return, AKA no translation in locale, handle fallbacks
|
||||
if (!value) {
|
||||
// If user specified fallback code as null, send back null
|
||||
@@ -139,11 +196,11 @@ export default function localizationPlugin(schema, options) {
|
||||
this.fallbackLocale = fallbackLocale;
|
||||
this.schema.eachPath((path, schemaType) => {
|
||||
if (schemaType.options.type instanceof Array) {
|
||||
this[path] && this[path].forEach(doc => doc.setLocale && doc.setLocale(locale, fallbackLocale));
|
||||
if (this[path]) this[path].forEach(doc => doc.setLocale && doc.setLocale(locale, fallbackLocale));
|
||||
}
|
||||
|
||||
if (schemaType.options.ref && this[path]) {
|
||||
this[path] && this[path].setLocale && this[path].setLocale(locale, fallbackLocale);
|
||||
if (this[path] && this[path].setLocale) this[path].setLocale(locale, fallbackLocale);
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -175,9 +232,7 @@ export default function localizationPlugin(schema, options) {
|
||||
});
|
||||
};
|
||||
|
||||
if (locale && this.getLocales().indexOf(locale) !== -1) {
|
||||
updateLocale(this.schema, locale);
|
||||
}
|
||||
updateLocale(this.schema, locale);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -199,7 +254,7 @@ export default function localizationPlugin(schema, options) {
|
||||
for (modelName in this.models) {
|
||||
if (this.models.hasOwnProperty(modelName)) {
|
||||
modelToUpdate = this.models[modelName];
|
||||
modelToUpdate.setDefaultLocale && modelToUpdate.setDefaultLocale(locale);
|
||||
if (modelToUpdate.setDefaultLocale) modelToUpdate.setDefaultLocale(locale);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -210,54 +265,3 @@ export default function localizationPlugin(schema, options) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function formatRefPathLocales(schema, parentSchema, parentPath) {
|
||||
// Loop through all refPaths within schema
|
||||
schema.eachPath((pathname, schemaType) => {
|
||||
// If a dynamic refPath is found
|
||||
if (schemaType.options.refPath && schemaType.options.refPath.includes('{{LOCALE}}') && parentSchema) {
|
||||
// Create a clone of the schema for each locale
|
||||
const newSchema = schema.clone();
|
||||
|
||||
// Remove the old pathname in order to rebuild it after it's formatted
|
||||
newSchema.remove(pathname);
|
||||
|
||||
// Get the locale from the parent path
|
||||
let locale = parentPath;
|
||||
|
||||
// Split the parent path and take only the last segment as locale
|
||||
if (parentPath && parentPath.includes('.')) {
|
||||
locale = parentPath.split('.').pop();
|
||||
}
|
||||
|
||||
// Replace {{LOCALE}} appropriately
|
||||
const refPath = schemaType.options.refPath.replace('{{LOCALE}}', locale);
|
||||
|
||||
// Add new schemaType back to newly cloned schema
|
||||
newSchema.add({
|
||||
[pathname]: {
|
||||
...schemaType.options,
|
||||
refPath,
|
||||
},
|
||||
});
|
||||
|
||||
// Removing and adding a path to a schema does not update tree, so do it manually
|
||||
newSchema.tree[pathname].refPath = refPath;
|
||||
|
||||
const parentSchemaType = parentSchema.path(parentPath).instance;
|
||||
|
||||
// Remove old schema from parent
|
||||
parentSchema.remove(parentPath);
|
||||
|
||||
// Replace newly cloned and updated schema on parent
|
||||
parentSchema.add({
|
||||
[parentPath]: parentSchemaType === 'Array' ? [newSchema] : newSchema,
|
||||
});
|
||||
}
|
||||
|
||||
// If nested schema found, continue recursively
|
||||
if (schemaType.schema) {
|
||||
formatRefPathLocales(schemaType.schema, schema, pathname);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
const find = query => {
|
||||
|
||||
const find = (query) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
query.Model.find({}, (err, docs) => {
|
||||
|
||||
if (err || !docs) {
|
||||
return reject({ message: 'not found' })
|
||||
return reject({ message: 'not found' });
|
||||
}
|
||||
|
||||
let result = docs;
|
||||
@@ -16,8 +14,8 @@ const find = query => {
|
||||
}
|
||||
|
||||
resolve(result);
|
||||
})
|
||||
})
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default find;
|
||||
|
||||
Reference in New Issue
Block a user