Skip to content

Commit

Permalink
Merge pull request #23 from yelworc/fallback
Browse files Browse the repository at this point in the history
Add fallback option
  • Loading branch information
alexsk authored Mar 21, 2021
2 parents ed7fcf3 + f058a0f commit 054f7c5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ build/Release
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

# IDE directory
# IDE directories
.idea
.vscode
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ mongoose.plugin(mongooseIntl, { languages: ['en', 'de', 'fr'], defaultLanguage:

### Plugin options

* languages - required, array with languages, suggested to use 2- or 3-letters language codes using [ISO 639 standard](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
* defaultLanguage - optional, if omitted the first value from `languages` array will be used as a default language
* `languages` - required, array with languages, suggested to use 2- or 3-letters language codes using [ISO 639 standard](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes)
* `defaultLanguage` - optional, if omitted the first value from `languages` array will be used as a default language
* `fallback` - when `true`, another translation is returned for fields that are not available in the currently selected language, chosen according to the order of the `languages` option (default: `false`, i.e. fields with missing translation are returned as `null`)

### Database representation

Expand Down
7 changes: 4 additions & 3 deletions lib/mongoose-intl.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = exports = function mongooseIntl(schema, options) {
} else {
pluginOptions.defaultLanguage = options.defaultLanguage.slice(0);
}
pluginOptions.fallback = 'fallback' in options ? options.fallback : false;

schema.eachPath(function (path, schemaType) {
if (schemaType.schema) { // propagate plugin initialization for sub-documents schemas
Expand Down Expand Up @@ -68,9 +69,9 @@ module.exports = exports = function mongooseIntl(schema, options) {
}

// are there any other languages defined?
for (var prop in langSubDoc) {
if (langSubDoc.hasOwnProperty(prop)) {
return null; // some other languages exist, but the required is not - return null value
for (lang of pluginOptions.languages) {
if (langSubDoc.hasOwnProperty(lang)) {
return pluginOptions.fallback ? langSubDoc[lang] : null;
}
}
return void 0; // no languages defined - the entire field is undefined
Expand Down

0 comments on commit 054f7c5

Please sign in to comment.