AdonisJS Lucid internationalization models.
npm
npm install @emilioforrer/adonis-translatable
yarn
yarn add @emilioforrer/adonis-translatable
Add the provider to your start/app.js file:
const providers = [
// ...
"@emilioforrer/providers/TranslatableProvider",
// ...
];
Basically this addon creates a new relationship called translations
to main model and delegates it's translatable attributes to the translation model.
So in order to work, you need to create a new model translations for your main model, add a trait and configure it.
Let's say we have a model App/Models/Role
and want to translate the name and description attributes, in order to do that follow the next steps.
- Let's create the translation table for the model
App/Models/Role
.
schema.createTable('roles_translations', (table) => {
table.increments()
table.timestamps()
/*
* Translation fields
*/
table.string('name').nullable()
table.text('description').nullable()
table.integer('role_id').unsigned()
table.string('language_code')
})
- Now let's create the translation model
App/Models/RoleTranslation
const Model = app.use('Model')
class RoleTranslation extends Model {
static get table () {
return 'roles_translations'
}
}
- Next, we need to add a
trait
to the main model (App/Models/Role
) and configure the options
const Model = app.use('Model')
class Role extends Model {
static boot () {
super.boot()
this.addTrait('Translatable', {
className: 'App/Models/RoleTranslation',
attributes: ['name', 'description']
})
}
}
- className: The name of the translation model. Defaults to:
''
. - primaryKey: This is the primary key that will be used to create the
translation
relationship. Defaults to:Model.primaryKey
. - foreignKey: This is the foreign key that will be used to create the
translation
relationship. Defaults to:${singlularized_table_name}_id
. - attributes: The list of attributes that we wish to be delegated to the translation model.
Great! If you are planning to contribute to the framework, make sure to adhere to following conventions, since a consistent code-base is always joy to work with.
Run the following command to see list of available npm scripts.
npm
npm run
yarn
yarn run
- Lint your code using standardJs. Run
yarn run lint
command to check if there are any linting errors. - Make sure you write tests for all the changes/bug fixes.
- Run
yarn run test
to lint and run your tests.