Low commit activity in last 3 years
No release in over a year
Manages attribute translations translations
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

JSON Buddy Translatable

Allows you to store text data in multiple languages. Based on translateable, but with a few differences:

  1. Support for key methods
  2. No extra table dependency
  3. Single attribute
  4. Support for jsonb and text format attributes

Translations

class Post < ActiveRecord::Base
  include BuddyTranslatable
  translatable :title
  # translatable :title, default_key: :de, available_locales: [:de, :en]
  # default_key: by default use `I18n.default_locale`
  # available_locales: by default `I18n.available_locales`
end

post = Post.create(title: { de: 'Hallo', en: 'Hello' })

# getter using current locale
I18n.locale = :en
post.title #=> Hello
I18n.locale = :de
post.title #=> Hallo

# Getter using methods
post.title_en #=> Hello
post.title_de #=> Hallo
post.title_for(:de) #=> Hallo
post.title_data_for(:en) #=> Hello
post.title_data # => return all data (Hash)

# Update current locale and maintain others
I18n.locale = :en
post.update(title: 'Hello changed')
post.title_en # => Hello changed
post.title_de # => Hallo
post.title_es # => Hallo (return default key if not found)
post.title_es = 'Hola' # => Set value for a specific locale
post.title_es # => Hola (return defined value)

# Replace all data
post.update(title: { en: 'En val', de: 'De val' })
post.title_en # => En val
post.title_es # => De val # default_key value is used when not defined

Requirements

  • ActiveRecord >= 4.2
  • I18n

Installation

Add this line to your application's Gemfile:

gem 'buddy_translatable', '>= 1.0'

Configure your available locales in your rails app, like: I18n.available_locales = %i[de en es]

And then execute: bundle install

Usage

class TestModel < ActiveRecord::Base
  include BuddyTranslatable

  translatable :title, default_key: :de
end

Migration

Attributes must be JSONB type.

class AddTitleToPosts < ActiveRecord::Migration
  def change
    add_column :posts, :title, :jsonb, null: false, default: {}
    add_column :posts, :key, :jsonb, null: false, default: {}
    
    # for databases that does not support jsonb format
    # add_column :posts, :title, :text, null: false, default: '{}'
  end
end

Filters

  • Filter for an item with exact value in any locale
# where_<attr_name>_with(value: String)
Post.where_key_with('ebay')
  • Filter for items with any locale that contains the provided value
# where_<attr_name>_like(value: String)
Post.where_key_like('bay')
  • Filter for items where current locale has the exact provided value
# where_<attr_name>_eq(value: String, locale?: Symbol)
Post.where_key_eq('ebay')

Development

Run tests with:
docker-compose run test bash -c "rspec"
Check code style:
docker-compose run test bash -c "rubocop"

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/owen2345/buddy_translatable.

License

The gem is available as open source under the terms of the MIT License.