GithubHelp home page GithubHelp logo

emilioforrer / adonis-translatable Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 1.0 87 KB

AdonisJS Lucid internationalization models.

License: MIT License

JavaScript 100.00%
adonis adonijs i18n translate database table lucid model model-translations adonis-translatable

adonis-translatable's Introduction

Adonis Translatable

AdonisJS Lucid internationalization models.

Installation

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",
   // ...
 ];

How it works

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.

How to use

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.

  1. 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')
})
  1. 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'
  }
}
  1. 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']
    })
  }
}

Translatable trait options

  • 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.

Development

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

Tests & Linting

  1. Lint your code using standardJs. Run yarn run lint command to check if there are any linting errors.
  2. Make sure you write tests for all the changes/bug fixes.
  3. Run yarn run test to lint and run your tests.

adonis-translatable's People

Contributors

eforrer avatar emilioforrer avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

lart5

adonis-translatable's Issues

Add it to the provider array description issue

There is a mistake in the description.

Instead of this:

const providers = [
    // ...
    "@emilioforrer/providers/TranslatableProvider",
    // ...
  ];

Should be like this:

const providers = [
    // ...
    "@emilioforrer/adonis-translatable/providers/TranslatableProvider",
    // ...
  ];

Translation attribute cannot bind to main model when call .toJSON()

Firstly, thanks for your great code :)

I have an issue when use this package: the translation attributes cannot bind to main model when call .toJSON() method

For example:
const result = Role.all() const roles = result.toJSON()

roles does not have name attribute

I have to access to roles.translation.name to get value

Could you help me?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.