backend changes to handle fallback to default language
This commit is contained in:
23
src/middleware/fallbackLocale.js
Normal file
23
src/middleware/fallbackLocale.js
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
* sets request fallbackLocale
|
||||
*
|
||||
* @param localization
|
||||
* @returns {Function}
|
||||
*/
|
||||
|
||||
export default function fallbackLocale(localization) {
|
||||
return function (req, res, next) {
|
||||
// TODO: discuss why this can't be a simple frontend method
|
||||
// config: false => false
|
||||
// config: false, req: true => false
|
||||
// config: true, req: undefined || true => true
|
||||
// config: true, req: false => false
|
||||
// if (localization.fallback && req.query.fallbackLocale === false) {
|
||||
// req.fallbackLocale = false;
|
||||
// } else {
|
||||
// req.model.fallbackLocal = true;
|
||||
// }
|
||||
|
||||
next();
|
||||
}
|
||||
}
|
||||
@@ -60,18 +60,9 @@ export default function internationalization(schema, options) {
|
||||
return localeSubDoc;
|
||||
}
|
||||
|
||||
if (localeSubDoc.hasOwnProperty(locale)) {
|
||||
return localeSubDoc[locale];
|
||||
}
|
||||
|
||||
// are there any other locales defined?
|
||||
for (let prop in localeSubDoc) {
|
||||
if (localeSubDoc.hasOwnProperty(prop)) {
|
||||
// TODO: find a different lang to return
|
||||
return null; // some other locales exist, but the required is not - return null value
|
||||
}
|
||||
}
|
||||
return void 0; // no locales defined - the entire field is undefined
|
||||
return localeSubDoc[locale]
|
||||
|| pluginOptions.fallback ? localeSubDoc[options.defaultLocale] : null
|
||||
|| null;
|
||||
})
|
||||
.set(function (value) {
|
||||
// multiple locales are set as an object
|
||||
@@ -112,9 +103,7 @@ export default function internationalization(schema, options) {
|
||||
if (schemaType.options.requiredAll) {
|
||||
localeOptions.required = schemaType.options.requiredAll;
|
||||
}
|
||||
// the use of this inside of a function caused an issue when changed to an arrow function
|
||||
// this object will not be scoped inside an arrow function but then I don't understand what is set here
|
||||
// if not lost at the end
|
||||
|
||||
this[locale] = localeOptions;
|
||||
}, intlObject[key]);
|
||||
|
||||
@@ -141,6 +130,9 @@ export default function internationalization(schema, options) {
|
||||
},
|
||||
unsetLocale: function () {
|
||||
delete this.docLocale;
|
||||
},
|
||||
setFallback: function(fallback = true) {
|
||||
pluginOptions.fallback = fallback;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user