Skip to content

Commit 0862d38

Browse files
author
Stefano Verna
committed
Ability to switch locale
1 parent 3df98a2 commit 0862d38

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ end
2525

2626
form do |f|
2727
# ...
28-
f.translated_inputs "Translated fields" do |t|
28+
f.translated_inputs "Translated fields", switch_locale: false do |t|
2929
t.input :title
3030
t.input :content
3131
end
3232
# ...
3333
end
3434
```
35+
If `switch_locale` is set, each tab will be rendered switching locale.
3536

3637
## Friendly ID
3738

lib/active_admin/globalize3/form_builder_extension.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ module Globalize3
33
module FormBuilderExtension
44
extend ActiveSupport::Concern
55

6-
def translated_inputs(name = "Translations", &block)
6+
def translated_inputs(name = "Translations", options = {}, &block)
7+
options.symbolize_keys!
8+
switch_locale = options.fetch(:switch_locale, false)
79
form_buffers.last << template.content_tag(:div, class: "activeadmin-translations") do
810
template.content_tag(:ul, class: "available-locales") do
911
I18n.available_locales.map do |locale|
1012
template.content_tag(:li) do
11-
template.content_tag(:a, I18n.t(:"active_admin.globalize3.language.#{locale}"), href:".locale-#{locale}")
13+
I18n.with_locale(switch_locale ? locale : I18n.locale) do
14+
template.content_tag(:a, I18n.t(:"active_admin.globalize3.language.#{locale}"), href:".locale-#{locale}")
15+
end
1216
end
1317
end.join.html_safe
1418
end <<
@@ -18,7 +22,9 @@ def translated_inputs(name = "Translations", &block)
1822
fields = proc do |form|
1923
form.input(:locale, as: :hidden)
2024
form.input(:id, as: :hidden)
21-
block.call(form)
25+
I18n.with_locale(switch_locale ? locale : I18n.locale) do
26+
block.call(form)
27+
end
2228
end
2329
inputs_for_nested_attributes(
2430
for: [:translations, translation ],

0 commit comments

Comments
 (0)