Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including schwifty-i18n with all npm packages installed. Try it out:

require("schwifty/package.json"); // schwifty is a peer dependency. var schwiftyI18n = require("schwifty-i18n")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

schwifty-i18n v0.2.1

A Schwifty plugin to make the task of i18n easier and less repetitive

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');

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free