From e4d934f32c3fdcf402470aa4e04c1a3db9f3a0d0 Mon Sep 17 00:00:00 2001 From: Miah Raihan Mahmud Arman Date: Tue, 28 Jul 2015 13:10:51 +0200 Subject: [PATCH 1/3] available_locales option in translated_inputs form builder function --- lib/active_admin/globalize/form_builder_extension.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/active_admin/globalize/form_builder_extension.rb b/lib/active_admin/globalize/form_builder_extension.rb index 0b11aedb..efb6a420 100644 --- a/lib/active_admin/globalize/form_builder_extension.rb +++ b/lib/active_admin/globalize/form_builder_extension.rb @@ -5,11 +5,12 @@ module FormBuilderExtension def translated_inputs(name = "Translations", options = {}, &block) options.symbolize_keys! + available_locales = options.fetch(:available_locales, I18n.available_locales.sort) switch_locale = options.fetch(:switch_locale, false) auto_sort = options.fetch(:auto_sort, true) template.content_tag(:div, class: "activeadmin-translations") do template.content_tag(:ul, class: "available-locales") do - (auto_sort ? I18n.available_locales.sort : I18n.available_locales).map do |locale| + (auto_sort ? available_locales.sort : available_locales).map do |locale| default = 'default' if locale == I18n.default_locale template.content_tag(:li) do I18n.with_locale(switch_locale ? locale : I18n.locale) do @@ -18,7 +19,7 @@ def translated_inputs(name = "Translations", options = {}, &block) end end.join.html_safe end << - (auto_sort ? I18n.available_locales.sort : I18n.available_locales).map do |locale| + (auto_sort ? available_locales.sort : available_locales).map do |locale| translation = object.translations.find { |t| t.locale.to_s == locale.to_s } translation ||= object.translations.build(locale: locale) fields = proc do |form| @@ -42,4 +43,3 @@ module ClassMethods end end end - From 1bffdb9e1523d7e042071728f1ee2073eef4195d Mon Sep 17 00:00:00 2001 From: Miah Raihan Mahmud Arman Date: Tue, 28 Jul 2015 13:20:13 +0200 Subject: [PATCH 2/3] default localte option in translated_inputs form builder method --- lib/active_admin/globalize/form_builder_extension.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/active_admin/globalize/form_builder_extension.rb b/lib/active_admin/globalize/form_builder_extension.rb index efb6a420..5b51fc11 100644 --- a/lib/active_admin/globalize/form_builder_extension.rb +++ b/lib/active_admin/globalize/form_builder_extension.rb @@ -8,10 +8,11 @@ def translated_inputs(name = "Translations", options = {}, &block) available_locales = options.fetch(:available_locales, I18n.available_locales.sort) switch_locale = options.fetch(:switch_locale, false) auto_sort = options.fetch(:auto_sort, true) + default_locale = options.fetch(:default_locale, I18n.default_locale) template.content_tag(:div, class: "activeadmin-translations") do template.content_tag(:ul, class: "available-locales") do (auto_sort ? available_locales.sort : available_locales).map do |locale| - default = 'default' if locale == I18n.default_locale + default = 'default' if locale == default_locale template.content_tag(:li) do I18n.with_locale(switch_locale ? locale : I18n.locale) do template.content_tag(:a, I18n.t(:"active_admin.globalize.language.#{locale}"), href:".locale-#{locale}", :class => default) From 44001c492292e410fe7163e28861911de6c52676 Mon Sep 17 00:00:00 2001 From: Raihan Mahmud Arman Date: Fri, 7 Aug 2015 16:18:41 +0200 Subject: [PATCH 3/3] Removed auto_sort option from translated_inputs form builder helper Updated readme --- README.md | 31 +++++++++++++++++++ .../globalize/form_builder_extension.rb | 7 ++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 9effb395..d5f473b3 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,37 @@ form do |f| # ... end +# You can also set locales to show in tabs +# For example we want to show English translation fields without tab, and want to show other languages within tabs +form do |f| + # ... + f.inputs do + Globalize.with_locale(:en) do + f.input :title + end + end + f.inputs "Translated fields" do + f.translated_inputs 'ignored title', switch_locale: false, available_locales: (I18n.available_locales - [:en]) do |t| + t.input :title + t.input :content + end + end + # ... +end + +# You can also set default language tab +# For example we want to make Bengali translation tab as default +form do |f| + # ... + f.inputs "Translated fields" do + f.translated_inputs 'ignored title', switch_locale: false, default_locale: :bn do |t| + t.input :title + t.input :content + end + end + # ... +end + ``` If `switch_locale` is set, each tab will be rendered switching locale. diff --git a/lib/active_admin/globalize/form_builder_extension.rb b/lib/active_admin/globalize/form_builder_extension.rb index 5b51fc11..e68aaef2 100644 --- a/lib/active_admin/globalize/form_builder_extension.rb +++ b/lib/active_admin/globalize/form_builder_extension.rb @@ -5,13 +5,12 @@ module FormBuilderExtension def translated_inputs(name = "Translations", options = {}, &block) options.symbolize_keys! - available_locales = options.fetch(:available_locales, I18n.available_locales.sort) + available_locales = options.fetch(:available_locales, I18n.available_locales) switch_locale = options.fetch(:switch_locale, false) - auto_sort = options.fetch(:auto_sort, true) default_locale = options.fetch(:default_locale, I18n.default_locale) template.content_tag(:div, class: "activeadmin-translations") do template.content_tag(:ul, class: "available-locales") do - (auto_sort ? available_locales.sort : available_locales).map do |locale| + available_locales.map do |locale| default = 'default' if locale == default_locale template.content_tag(:li) do I18n.with_locale(switch_locale ? locale : I18n.locale) do @@ -20,7 +19,7 @@ def translated_inputs(name = "Translations", options = {}, &block) end end.join.html_safe end << - (auto_sort ? available_locales.sort : available_locales).map do |locale| + available_locales.map do |locale| translation = object.translations.find { |t| t.locale.to_s == locale.to_s } translation ||= object.translations.build(locale: locale) fields = proc do |form|