schwifty-i18n

0.2.1 • Public • Published

Schwifty i18n

A Schwifty i18n plugin

Build Status Coverage Status NSP Status

Lead Maintainer: Daniel Cole

THIS IS A WORK IN PROGRESS

Usage

Create a Schwifty model as per usual:

 
const Model        = require('schwifty').Model;
const Joi          = require('joi');
const SchwiftyI18n = require('schwifty-i18n');
 
class Category extends Model {
 
    static get tableName() {
 
        return 'Category';
    }
 
    static get joiSchema() {
 
        return Joi.object(
            id   : Joi.number(),
            title: Joi.string().meta({translate: true})
        );
    }
}
 
module.exports = SchwiftyI18n(Category);
 

You will need to manually add the migrations, as an example:

 
 knex.schema.createTable('Category', (table) => {
 
            table.increments('id').primary();
            table.string('name');
 });
 
 knex.schema.createTable('CategoryTranslation', (table) => {
 
    table.integer('tableId');
    table.string('locale');
    table.string('column');
    table.string('translation');
 
    table.foreign('tableId').references('Category.id');
    table.primary(['tableId', 'locale', 'column']);
 });
 

To add translations to a model (the model has to have been created previously with the defaultLocale):

 
    const category = { id: 1, name: 'Une catégorie' };
 
    await Category.query().patchTranslation(category, 'FR');
 

To delete a translation (you can't delete the defaultLocale, to do this you must delete the model):

 
    await Category.query().deleteTranslation(1, 'FR');
 

To fetch a specific language:

 
    await Category.query().i18n('FR');
    await Category.query().findById(1).i18n('FR');
 

Package Sidebar

Install

npm i schwifty-i18n

Weekly Downloads

1

Version

0.2.1

License

MIT

Unpacked Size

12.7 kB

Total Files

6

Last publish

Collaborators

  • optii