• Stars
    star
    214
  • Rank 184,678 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 12 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Translatable columns for Ruby on Rails, stored in the model table itself.

Traco

Build Status

Translatable attributes for Ruby on Rails 4.2+, stored in the model table itself.

Inspired by Iain Hecker's translatable_columns.

To store translations outside the model, see Sven Fuchs' Globalize.

Usage

Say you want Post#title and Post#body to support both English and Swedish values.

Write a migration to get database columns with locale suffixes, e.g. title_sv and title_en, like:

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
      t.string :title_sv, :title_en
      t.text :body_sv, :body_en

      t.timestamps
    end
  end
end

Don't create a database column named title without a suffix, since Traco will define a method with that name.

If you use a locale format like pt-BR, the column name would be title_pt_br.

Declare the attributes in the model:

class Post < ActiveRecord::Base
  translates :title, :body
end

You can still use your accessors like title_sv and title_sv= in forms, validations and other code, but you also get:

#title: Shows the title in the current locale. If blank, falls back to default locale. Otherwise nil.

#title(locale: :en): Shows the English title, without falling back. (To fall back, do I18n.with_locale(:en) { post.title } instead.)

#title=: Assigns the title to the column for the current locale, if present. Raises if the column doesn't exist.

#title?: Is the title present? Respects the fallback setting.

#title?(locale: :en): Is the title present in English, without falling back? (To fall back, do I18n.with_locale(:en) { post.title? } instead.)

.human_attribute_name(:title_sv): Extends this standard method to return "Title (Swedish)" if you have a translation key i18n.languages.sv = "Swedish" and "Title (SV)" otherwise. Rails uses this method to build validation error messages and form labels.

.translatable_attributes: Returns an array like [:title, :body].

.locale_columns(:title): Returns an array like [:title_sv, :title_en] sorted with current locale first, then default locale, and then alphabetically. Suitable for looping in forms:

<% Post.locale_columns(:title).each do |column| %>
  <p>
    <%= form.label column %>
    <%= form.text_field column %>
  </p>
<% end %>

Or perhaps for things like:

attr_accessible *locale_columns(:title)

validates *locale_columns(:title), :uniqueness => true

You can also pass multiple attributes if you like:

attr_accessible *locale_columns(:title, :body)

The return value will be sorted like [:title_sv, :title_en, :body_sv, :body_en].

.current_locale_column(:title): Returns :title_sv if :sv is the current locale. Suitable for some SQL queries, such as sorting.

.locales_for_attribute(:title): Returns an array like [:sv, :en] sorted with current locale first, then default locale, and then alphabetically.

And the equivalent methods for body, of course.

Please note that your translates :title, :body declaration must be called before you call locale_columns. Otherwise you will get an error like "NoMethodError: undefined method `locale_columns' for #<Class:0x00000003f69188>".

Fallbacks

By default, Traco will fall back to the default locale if there is no translation in the current locale.

You can specify e.g. translates :title, fallback: false to never fall back and instead return nil.

You can specify e.g. translates :title, fallback: :any to fall back first to the default locale, then to any other locale.

You can specify e.g. translates :title, fallback: [:sv] to explicitly declare fallbacks as an array of any length.

You can specify e.g. translates :title, fallback: :i18n to use the fallbacks from I18n.fallbacks.

You can override the default fallback strategy with a parameter passed to the reader: post.title(fallback: :any).

If you need to declare the default locale fallback, do post.title(fallback: :default).

Overriding methods

Methods are defined in an included module, so you can just override them and call Traco's implementation with super:

class Post < ActiveRecord::Base
  translates :title

  def title
    super.reverse
  end
end

Installation

Add this to your Gemfile:

gem "traco"

Then run

bundle

to install it.

Running the tests

bundle
rake

Benchmark

ruby benchmarks/overhead.rb

Contributors

  • Henrik Nyh
  • Andrii Malyshko
  • Tobias Bohwalli
  • Mario Alberto Chavez
  • Philip Arndt
  • PikachuEXE
  • Fernando Morgenstern
  • Tomáš Horáček
  • Joakim Kolsjö

License

MIT

More Repositories

1

attr_extras

Takes some boilerplate out of Ruby with methods like attr_initialize.
Ruby
553
star
2

devbook

Barsoom developer's handbook
230
star
3

ex-remit

A self-hosted web app for commit-by-commit code review, written using Phoenix LiveView.
Elixir
42
star
4

banktools-se

Validate and normalize Swedish bank account numbers, plusgiro and bankgiro.
Ruby
32
star
5

review

A tool for commit-by-commit code review of repositories on GitHub
Elixir
30
star
6

go_to_param

Rails "go_to" redirection param utilities.
Ruby
17
star
7

prawn_cocktail

Simple documents, templates and helpers on top of Prawn.
Ruby
16
star
8

prawn-print

Print/autoprint Prawn-generated PDFs from Adobe Reader.
Ruby
16
star
9

sie

Parser and generator for SIE files (https://sie.se/)
Ruby
15
star
10

content_translator

Small web service for handling content translations using WebTranslateIt. Written in elixir. Can run on a free heroku dyno.
Elixir
13
star
11

gridlook

Rails 5 app on Heroku to receive SendGrid event hooks and provide a searchable archive.
Ruby
9
star
12

net_http_timeout_errors

Provides a list of Net::HTTP timeout errors.
Ruby
8
star
13

excel-esv

Excel parsing and generation with the ease of CSV.
Ruby
7
star
14

bankline_csv_import_file

Generate Bankline CSV import files.
Ruby
4
star
15

ex_gridhook

ExGridhook is an app to recieve and persist sendgrid webhook events.
Elixir
4
star
16

banktools-de

Validate and normalize German Bankleitzahl (BLZ) and bank account numbers.
Ruby
4
star
17

prawn_cocktail_rails

Simple documents, templates and helpers on top of Prawn. In Ruby on Rails!
Ruby
4
star
18

debitech

Library for doing payments using DebiTech (DIBS)
Ruby
3
star
19

barsoom_utils

Various helpful utils
Ruby
3
star
20

banktools-global

Validate and normalize international bank account numbers like IBAN and BIC. Based on iban-tools.
Ruby
2
star
21

formatting

Rails-less formatting (numbers, currency) for your unit-testable code.
Ruby
2
star
22

banktools-dk

Validate and normalize Danish account numbers.
Ruby
2
star
23

supplier_payments

Ruby library for making BGC supplier payment files (leverantörsbetalningar).
Ruby
2
star
24

panko

Breadcrumbs. WiP!
Ruby
1
star
25

banktools-gb

Validate United Kingdom bank account numbers (with sort codes).
Ruby
1
star
26

banktools-at

Validate Austrian bank account numbers.
Ruby
1
star
27

heroku-buildpack-shell-tools

Ruby
1
star
28

change-ssh-to-https-in-gemfile-buildpack

Changes git@ URIs to https:// in Gemfile during deploy for when you can't change it elsewhere
Shell
1
star
29

minimapper-extras

Extras for Minimapper.
Ruby
1
star