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 🙏

© 2024 – Pkg Stats / Ryan Hefner

sequelize-i18n

v1.1.0

Published

Easily set up database internalization using sequelize

Downloads

48

Readme

sequelize-i18n

Easily set up internalization using sequelize

Installation

Install with npm

npm install sequelize-i18n --save

Usage

###Model definition

Define your models as usal using sequelize, simply set i18n property to true for the internationnalised fields :

var Product = function (sequelize, DataTypes) {
    var Product = sequelize.define('product', {
        id : {
            type 			: DataTypes.BIGINT,
            primaryKey 		: true,
            autoIncrement 	: true
        },
        name : {
            type 			: DataTypes.STRING,
            i18n		 	: true
        },
        reference : {
            type 			: DataTypes.STRING
        }
    },
    
    { /* options */ });
    return Product;
};


module.exports = function(sequelize) {
    return sequelize.import('product', Product)
};

Initialisation

Init Sequelize-i18n module before importing models

var Sequelize = require('sequelize');
var SequelizeI18N = require('sequelize-i18n');

var languages = { 
	list : ["EN" , "FR" , "ES"] , 
	default : "FR" 
};

// Init Sequelize
sequelize = new Sequelize( "db_name" , "user" , "password" );

// Init i18n
i18n = new SequelizeI18N( sequelize, { languages: languages.list, default_language: languages.default } );
i18n.init();

// Import models in sequelize
var ProductModel = sequelize.import('product', Product)

Options

  • languages ( Array ) - List of allowed languages IDs
  • default_language ( Object ) - Default language ID
  • i18n_default_scope ( Boolean = true ) - Add i18n to the model default scope
  • add_i18n_scope ( Boolean = true ) - Add i18n scope to model
  • inject_i18n_scope ( Boolean = true ) - Inject i18n to model scopes
  • default_language_fallback ( Boolean = true ) - Fall back to default language if we can't find a value for the given language in get_i18n method

How it works


Sequelize-i18n will check for i18n property in your models properties. If i18n is set to true, it will create an new table in which internationalized values for the property will be store.

Example : The given the above exemple "Product",

name : {
    type 		: DataTypes.STRING,
    i18n		: true,
}

A Product_i18n Sequelize model will be create, with the following columns:

  • id : the row unique identifier ( INTEGER )
  • language_id : Identifies the language of the current translation ( INTEGER OR STRING )
  • parent_id : id of the targeted product ( Same the Product model primary key or unique key )
  • name : i18n value ( Same as Product.name.type )

The "name" property type will be set to VIRTUAL

Sequelize-i18n will set hooks into models on create, find, update and delete operations.

Creation

ProductModel.create({
     id: 1,
     name: 'test',
     reference: "xxx"
 })
 .then(function (result)  {
     // result.product_i18n == [ {name : "test" , lang : "FR" } ]
 })

Update

product_instance.update( { name : "new name" }  )
.then( function( result ) {
    // result.product_i18n = [ {name : "french name" , language_id : "FR" } ]
}
 
product_instance.update( { name : "english name" } , { language_id : "EN" }  )
.then( function( result ) {
    /*
    result.product_i18n == [ 
        {name : "french name" , language_id : "FR" } ,
        {name : "english name" , language_id : "EN" }
    ]
    */
}

Find

Product.find({ where : { id : 1 } })
.then( function( result ) {
    /*
    result.product_i18n == [ 
        {name : "french name" , language_id : "FR" } ,
        {name : "english name" , language_id : "EN" }
    ]
    */
});

Delete

Deleting a Product instance will also delete i18n values

get_i18n instance method

An Sequelize instance method is added to the Sequelize model in order to set virtual i18n property in the lanuage you want.

product_instance.get_i18n( "EN" );
// product_instance.name == "english name"

product_instance.get_i18n( "FR" );
// product_instance.name == "french name"

product_instance.get_i18n( "ES" );
// product_instance.name == "" if options.default_language_fallback is set to false
// product_instance.name == "french name" if options.default_language_fallback is set to true