🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

@stouder-io/adonis-translatable

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stouder-io/adonis-translatable

Translatable fields for AdonisJS models.

latest
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

@stouder-io/adonis-translatable

Translatable fields for AdonisJS models.

npm

Installation

This package is available in the npm registry.

pnpm i @stouder-io/adonis-translatable

Usage

After installing the package, you can now decorate your translatable fields with the @translation decorator.

class Post extends BaseModel {
  @column()
  declare id: number

  @translation()
  declare title: Translation

  @translation()
  declare body: Translation
}

In your migrations, the translatable fields must be of type json.

export default class extends BaseSchema {
  protected tableName = 'posts'

  async up() {
    this.schema.createTable(this.tableName, (table) => {
      table.increments('id')
      table.json('title')
      table.json('body')
    })
  }
  
  async down() {
    this.schema.dropTable(this.tableName)
  }
}

When using your model, you can now access the translated fields.

const post = await Post.find(1)
post.title.get('fr')

You can access it and throw if it doesn't exist.

const post = await Post.find(1)
post.title.getOrFail('fr')

You can also set the translated fields.

const post = await Post.find(1)
post.title.set('fr', 'Mon titre')

Or fully replace the translations.

const post = await Post.find(1)
post.title = Translation.from({
  fr: 'Mon titre',
  en: 'My title',
})

Keywords

translation

FAQs

Package last updated on 07 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts