Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Traco as i18n db backend #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ActiveAdmin Translate Changelog

## 0.3.0 - June 3, 2014

- Support for [Traco](https://github.com/barsoom/traco) as i18n backend

## 0.2.2 - November 16, 2012

- Include SCSS mixins.
Expand Down
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
# ActiveAdmin Translate

Translate your [Globalize3](https://github.com/svenfuchs/globalize3) ActiveModel translations in
[ActiveAdmin](https://github.com/gregbell/active_admin), using [jQueryUI tabs](http://jqueryui.com/tabs/) to switch
Translate your [Globalize3](https://github.com/svenfuchs/globalize3)
or [Traco](https://github.com/barsoom/traco) ActiveModel translations
in [ActiveAdmin](https://github.com/gregbell/active_admin),
using [jQueryUI tabs](http://jqueryui.com/tabs/) to switch
between the locales.

## Installation

Add the gem to your `Gemfile`
Add the gem to your `Gemfile` with your i18n db backend `globalize3` or `traco`

```ruby
gem 'activeadmin-translate'
gem 'globalize3' # OR
gem 'traco'
```

and install it with Bundler:
Expand Down Expand Up @@ -37,9 +41,16 @@ You need to import the SASS for styling the tabs to `app/assets/stylesheets/acti
@import "active_admin/translate";
```

If `ActiveAdmin` has not already loaded jQuery UI tabs library,
you need to manually require the file in `app/assets/stylesheets/active_admin.js.coffee`:

```js
#= require jquery.ui.tabs
```

## Usage

### Make your translations accessible
### Make your translations accessible (Globalize3 only)

In order to access the translations of your model and be able to write them on save, you need to make attributes
accessible in your model. Globalize3 stores the model translations in a separate table that is accessible as
Expand All @@ -54,14 +65,22 @@ end

### Translate your ActiveAdmin forms


To translate your form you need to use the `translate_inputs` form helper:

```ruby
form do |f|
# globalize3:
f.translate_inputs do |t|
t.input :title
t.input :description
end

# traco:
f.translate_inputs do |locale|
t.input :"title_#{locale}"
t.input :"description_#{locale}"
end
end
```

Expand All @@ -71,7 +90,7 @@ To show the attributes for all locales, you use the `translate_attributes_table_

```ruby
show do |model|
panel 'Globalized Model' do
panel 'Localized Model' do
translate_attributes_table_for model do
row :title
row :description do |p|
Expand Down
3 changes: 1 addition & 2 deletions activeadmin-translate.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
gem.authors = ['Michael Kessler']
gem.email = %w([email protected])
gem.summary = %q{Translate models with ActiveAdmin.}
gem.description = %q{Translate your models in ActiveAdmin with Globalize3.}
gem.description = %q{Translate your models in ActiveAdmin with Globalize3 or Traco.}
gem.homepage = 'https://github.com/netzpirat/activeadmin-translate'

gem.files = Dir['{app,lib,config}/**/*'] + %w(LICENSE README.md CHANGELOG.md CONTRIBUTING.md)
Expand All @@ -14,6 +14,5 @@ Gem::Specification.new do |gem|
gem.version = ActiveAdmin::Translate::VERSION

gem.add_dependency 'activeadmin'
gem.add_dependency 'globalize3'
gem.add_dependency 'railties'
end
15 changes: 15 additions & 0 deletions lib/active_admin/translate/backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ActiveAdmin
module Translate
class << self
attr_accessor :backend

def traco?
backend == :traco
end

def globalize?
backend == :globalize
end
end
end
end
28 changes: 28 additions & 0 deletions lib/active_admin/translate/form_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,34 @@ def tab_script
# @param [Proc] block the block for the additional inputs
#
def locale_fields(name, block)
if ActiveAdmin::Translate.traco?
locale_fields_for_traco(name, block)
else
locale_fields_for_globalize(name, block)
end
end


def locale_fields_for_traco(name, block)
buffer = form_buffers.dup

fieldset = ::I18n.available_locales.map do |locale|
@form_buffers = ["".html_safe]

fields = proc do
block.call(locale)
end

inputs(name: name, :id => field_id(locale), :class => "inputs locale locale-#{locale}", &fields)
end.join.html_safe

@form_buffers = buffer

fieldset
end


def locale_fields_for_globalize(name, block)
::I18n.available_locales.map do |locale|
translation = object.translation_for(locale)
translation.instance_variable_set(:@errors, object.errors) if locale == I18n.default_locale
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/translate/version.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ActiveAdmin
module Translate
# The current released version
VERSION = '0.2.2'
VERSION = '0.3.0'
end
end
10 changes: 9 additions & 1 deletion lib/activeadmin-translate.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
require 'active_admin'
require 'globalize3'
require 'active_admin/translate/backend'

ActiveAdmin::Translate.backend = begin
require 'traco'
:traco
rescue LoadError
require 'globalize'
:globalize
end

require 'active_admin/version'
require 'active_admin/translate/engine'
Expand Down