Giter VIP home page Giter VIP logo

mongoose-i18n-localize's Introduction

mongoose-i18n-localize

mongoose-i18n-localize is a mongoose plugin to support i18n and localization in your mongoose schemas.

It seems like mongoose-i18n is not longer supported and I didn't get it to work on my machine, so I decided to write my own version.

Usage

npm install mongoose-i18n-localize

Create your schema:

var mongoose = require('mongoose');
var mongooseI18n = require('mongoose-i18n-localize');

var schema = new mongoose.Schema({
	name: {
		type: String,
		i18n: true
	}
});

schema.plugin(mongooseI18n, {
	locales: ['en', 'de']
});

var Model = mongoose.model('Name', schema);

This will create a structure like:

{
	name: {
		en: String,
		de: String
	}
}

All validators of name get also assigned to name.en and name.de.

mongoose-i18n-localize adds the methods toObjectLocalized(resource, locale) and toJSONLocalized(resource, locale) to the i18n schema methods. To set the locale of a resource to en, just do:

Model.find(function(err, resources) {
	var localizedResources = resources.toJSONLocalized('en');
});

//or

Model.find(function(err, resources) {
	var localizedResources = Model.schema.methods.toJSONLocalized(resources, 'en');
});

localizedResources has now the following structure:

[
	{
		name: {
			en: 'hello',
			de: 'hallo',
			localized: 'hello'
		}
	}
]

Use toObjectLocalized or toJSONLocalized according to toObject or toJSON.

If you only want to show only one locale message use the methods toObjectLocalizedOnly(resource, locale, localeDefault) or toJSONLocalizedOnly(resource, locale, localeDefault).

Tests

To run the tests you need a local MongoDB instance available. Run with:

npm test

Issues

Please use the GitHub issue tracker to raise any problems or feature requests.

If you would like to submit a pull request with any changes you make, please feel free!

mongoose-i18n-localize's People

Contributors

acardinale avatar netbull avatar rusty1s avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongoose-i18n-localize's Issues

schema.remove is not a function

I'm getting this error when I try and apply this to an existing collection. I'm wondering if this even possible to do or if I need to migrate it somehow.

/node_modules/mongoose-i18n-localize/index.js:18
			schema.remove(pathname);
			       ^
TypeError: schema.remove is not a function

Support for arrays of strings

Hi,
would it be hard to implement support for arrays of strings? Like:
name: { type: String, }, localizedName: { type: String, i18n: true, }, nicknames: { type: [String], i18n: true, }

So that as a result you would have
name
localizedName.fi
and also
nicknames.fi[0]?

Problem with nodejs v.6.6.0

Hi,

I have a problem when I try to update a field with i18n: true with the version of node 6.6.0

I have 2 servers. The development with the version of nodejs 6.4.0 and I have no problems, but in the test server, which has the version of nodejs 6.6.0 throws the following error:

Cannot set property 'es-ES' of undefined
    at model.Document.$__set (.../node_modules/mongoose/lib/document.js:889:21)
    at model.Document.set (.../node_modules/mongoose/lib/document.js:772:10)
    at model._handleIndex (.../node_modules/mongoose/lib/document.js:609:14)
    at model.Document.set (.../node_modules/mongoose/lib/document.js:569:24)
    at model._handleIndex (.../node_modules/mongoose/lib/document.js:593:12)
    at model.Document.set (.../node_modules/mongoose/lib/document.js:569:24)
    at model._handleIndex (.../node_modules/mongoose/lib/document.js:593:12)
    at model.Document.set (.../node_modules/mongoose/lib/document.js:569:24)
    at model._handleIndex (.../node_modules/mongoose/lib/document.js:593:12)
    at model.Document.set (.../node_modules/mongoose/lib/document.js:569:24)
    at model.Document.set (.../node_modules/mongoose/lib/document.js:642:14)
    at model.set [as emailConfig] (.../node_modules/mongoose/lib/document.js:1790:41)
    ...

The versions of mongo and mongoose are the same

You can help?

Indexing and search

What about indexing? If I have a model

var schema = new mongoose.Schema({
    name: {
        type: String,
        i18n: true
    }
});

How can I do cross-language search? Or will it always have to be schema.find({'name.en': /search/})?

Getting error with mongoose 5.7.5

I'm on 5.7.3 and it works OK, but not on the latest:


Emitted 'error' event at:
 {
  message: 'Invalid ref at path "F3FCYBUABRY.en". Got null',
  name: 'MongooseError'
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.