0.2.1 • Published 6 years ago
schwifty-i18n v0.2.1
Schwifty i18n
A Schwifty i18n plugin
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');