npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mongoose-i18n-neutral

v1.2.0

Published

Mongoose schema plugin for i18n (language neutral)

Downloads

22

Readme

mongoose-i18n-neutral

Mongoose schema plugin for multiligual fields (language neutral).

This project is based on Alex Skorichenko project mongoose-intl.

Installation

npm install mongoose-i18n-neutral --save # or yarn add mongoose-i18n-neutral

Overview

I18n modules usually requires to define a default language (if not uses english) as the base of translation process.

This approach may be adequate for some projects, but it is not when we have to manage international data fields containing native language values or we would like to optimize the translation process (not all of default translations must use the same language).

Geonames city name files are a clear example of the use native language.

The approach

Instead of using a default language, this plugin transforms the String schemas with the i18n option into an Object schema with the following properties:

  • _def: Property to store the default translation (language agnostic)
  • lang_code: Additional properties for each language defined in the plugin configuration.

Plugin registration

const mongoose = require('mongoose');
const mongooseI18nNeutral = require('mongoose-i18n-neutral');

To define the plugin as global, register the plugin in mongoose before any schema instantiation.

mongoose.plugin(mongooseI18nNeutral, {
  languages: ['en', 'es', 'de', 'fr']
});

To define the plugin at schema level, register the plugin

GeoCountrySchema.plugin(mongooseI18nNeutral, {
  languages: ['en', 'es', 'de', 'fr']
});

Plugin options

The plugin requires to define only one Array of strings parameter (languages) to define the alternative translations to store.

Set useDefault option to false, if you don't want to implement the default translation field.

Tip: It is strongly recommended to use ISO639 language codes to identify the languages, but it is not mandatory.

I18n schema definition

An schema will be considered as i18n or multilanguage, if any of their String fields has the property i18n set to true.

const GeoCountrySchema = new mongoose.Schema({
  iso3: {
    type: String,
    required: true
  },
  name: {
    type: String,
    i18n: true,
    required: false
  },
  capital: {
    type: String,
    i18n: true,
    default: 'not-available'
  }
});

The plugin with transform the schema using the languages option provided as follows:

const GeoCountrySchema = new mongoose.Schema({
  iso3: {
    type: String,
    required: true
  },
  name: {
    _def: {
      // Only in default usage
      type: String,
      required: true
    },
    en: {
      type: String,
      required: false
    },
    es: {
      type: String,
      required: false
    },
    de: {
      type: String,
      required: false
    },
    fr: {
      type: String,
      required: false
    }
  },
  capital: {
    _def: {
      // Only in default usage
      type: String,
      required: true
    },
    en: {
      type: String,
      required: false
    },
    es: {
      type: String,
      required: false
    },
    de: {
      type: String,
      required: false
    },
    fr: {
      type: String,
      required: false
    }
  }
});

Note: The plugin overrides the required and default options to ensure that the default translation is always provided.

All others options defined (e.g. lowercase, uppercase, trim, minlength, maxlength, match etc.) will be propagated.

Additional methods

Each i18n field has available some additional virtual methods to deal with their values.

const GeoCountry = mongoose.model('countries', GeoCountrySchema);
const doc = new GeoCountry();
// Default translation: Stores the value in name._def
doc.name.default = 'Deutschland'; // Only in default usage
// Set the english translation
doc.name.en = 'Germany';
// Set the spanish translation
doc.name.es = 'Alemania';
// Returns the default translation
const defName = doc.name.default; // Only in default usage
// Returns the spanish translation
const esName = doc.name.es;
// Returns the name object with all translations
const translations = doc.name.all;

The Model and Document getLanguages() method returns the array of languages provided in the plugin configuration.

Managing i18n documents

These documents and its i18n properties may be managed as any other mongo document.

We recommend not to include the virtual fields to the documents.

Providing language-specific information

To provide the information available for a language, you can use a mongo query like this:

db.countries.aggregate({
  $project: {
    _id: 1,
    i18Name: {
      $ifNull: ['name.es', 'name._def']
    }
  }
});