Giter Club home page Giter Club logo

i18n-active_record's Introduction

I18n::Backend::ActiveRecord Ruby Style Guide Tests Status Linter Status

This repository contains the I18n ActiveRecord backend and support code that has been extracted from the I18n gem: http://github.com/svenfuchs/i18n. It is fully compatible with Rails 4, 5 and 6.

Installation

For Bundler put the following in your Gemfile:

gem 'i18n-active_record', require: 'i18n/active_record'

After updating your bundle, run the installer

$ rails g i18n:active_record:install

It creates a migration:

class CreateTranslations < ActiveRecord::Migration
  def change
    create_table :translations do |t|
      t.string :locale
      t.string :key
      t.text :value
      t.text :interpolations
      t.boolean :is_proc, default: false

      t.timestamps
    end
  end
end

To specify table name use:

$ rails g i18n:active_record:install MyTranslation

With the translation model you will be able to manage your translation, and add new translations or languages through it.

By default the installer creates a new file in config/initializers named i18n_active_record.rb with the following content.

require 'i18n/backend/active_record'

Translation = I18n::Backend::ActiveRecord::Translation

if Translation.table_exists?
  I18n.backend = I18n::Backend::ActiveRecord.new

  I18n::Backend::ActiveRecord.send(:include, I18n::Backend::Memoize)
  I18n::Backend::Simple.send(:include, I18n::Backend::Memoize)
  I18n::Backend::Simple.send(:include, I18n::Backend::Pluralization)

  I18n.backend = I18n::Backend::Chain.new(I18n::Backend::Simple.new, I18n.backend)
end

To perform a simpler installation use:

$ rails g i18n:active_record:install --simple

It generates:

require 'i18n/backend/active_record'
I18n.backend = I18n::Backend::ActiveRecord.new

You may also configure whether the ActiveRecord backend should use destroy or delete when cleaning up internally.

I18n::Backend::ActiveRecord.configure do |config|
  config.cleanup_with_destroy = true # defaults to false
end

To configure the ActiveRecord backend to cache translations(might be useful in production) use:

I18n::Backend::ActiveRecord.configure do |config|
  config.cache_translations = true # defaults to false
end

Usage

You can now use I18n.t('Your String') to lookup translations in the database.

Missing Translations

Usage

In order to make the I18n::Backend::ActiveRecord::Missing module working correctly pluralization rules should be configured properly. The i18n.plural.keys translation key should be present in any of the backends. See https://github.com/svenfuchs/i18n-active_record/blob/master/lib/i18n/backend/active_record/missing.rb for more information.

en:
  i18n:
    plural:
      keys:
        - :zero
        - :one
        - :other

Interpolations

The interpolations field in the translations table is used by I18n::Backend::ActiveRecord::Missing to store the interpolations seen the first time this Translation was requested. This will help translators understand what interpolations to expect, and thus to include when providing the translations.

The interpolations field is otherwise unused since the "value" in Translation#value is actually used for interpolation during actual translations.

Examples

Contributing

Test suite

The test suite can be run with:

bundle exec rake

By default it runs the tests for SQLite database, to specify a database the DB env variable can be used:

DB=postgres bundle exec rake
DB=mysql bundle exec rake

To run tests for a specific rails version see Appraisal:

bundle exec appraisal rails-4 rake test

Maintainers

  • Sven Fuchs
  • Tim Masliuchenko

i18n-active_record's People

Contributors

abime avatar agustinf avatar avakhov avatar cbeer avatar dependabot[bot] avatar dorianmariecom avatar gingermusketeer avatar jeremyw avatar kaspth avatar mejackreed avatar mpataki avatar pawelnguyen avatar petergoldstein avatar plwalters avatar slbug avatar svenfuchs avatar timfjord avatar tomfast avatar vipera avatar westonganger avatar

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.