Use standard locale names for Globalize

It turns out it is not necessary to downcase and underscore
locale names to use the globalize-accessor gem. The gem
will automatically underscore the locale name when defining and
calling the accessor methods.
This commit is contained in:
Marko Lovic
2018-08-24 12:01:47 +02:00
committed by Javi Martín
parent 6fe7dc22bc
commit c7fcdd9b0e
10 changed files with 21 additions and 26 deletions

View File

@@ -6,12 +6,12 @@ module GlobalizeHelper
def locale_options
I18n.available_locales.map do |locale|
[name_for_locale(locale), neutral_locale(locale)]
[name_for_locale(locale), locale]
end
end
def display_translation?(locale)
same_locale?(neutral_locale(I18n.locale), neutral_locale(locale)) ? "" : "display: none;"
same_locale?(I18n.locale, locale) ? "" : "display: none;"
end
def translation_enabled_tag(locale, enabled)
@@ -23,7 +23,7 @@ module GlobalizeHelper
end
def enable_locale?(resource, locale)
resource.translated_locales.include?(neutral_locale(locale)) || locale == I18n.locale
resource.translated_locales.include?(locale) || locale == I18n.locale
end
def highlight_current?(locale)
@@ -34,10 +34,6 @@ module GlobalizeHelper
display_translation?(locale)
end
def neutral_locale(locale)
locale.to_s.downcase.underscore.to_sym
end
def globalize(locale, &block)
Globalize.with_locale(locale) do
yield

View File

@@ -1,6 +1,6 @@
module SiteCustomizationHelper
def site_customization_enable_translation?(locale)
I18nContentTranslation.existing_languages.include?(neutral_locale(locale)) || locale == I18n.locale
I18nContentTranslation.existing_languages.include?(locale) || locale == I18n.locale
end
def site_customization_display_translation?(locale)

View File

@@ -41,10 +41,13 @@ module TranslatableFormHelper
@template.capture do
@object.globalize_locales.each do |locale|
Globalize.with_locale(locale) do
localized_attr_name = @object.localized_attr_name_for(method, locale)
label_without_locale = @object.class.human_attribute_name(method)
final_options = @template.merge_translatable_field_options(options, locale)
.reverse_merge(label: label_without_locale)
@template.concat send(field_type, "#{method}_#{locale}", final_options)
@template.concat send(field_type, localized_attr_name, final_options)
end
end
end

View File

@@ -5,7 +5,7 @@ class Banner < ActiveRecord::Base
translates :title, touch: true
translates :description, touch: true
globalize_accessors locales: I18n.available_locales.map { |l| l.to_s.underscore.to_sym }
globalize_accessors
validates :title, presence: true,
length: { minimum: 2 }

View File

@@ -8,7 +8,7 @@ class Budget
accepted_content_types: [ "application/pdf" ]
translates :title, :description, touch: true
globalize_accessors locales: I18n.available_locales.map { |l| l.to_s.underscore.to_sym }
globalize_accessors
belongs_to :investment
belongs_to :status, class_name: 'Budget::Investment::Status'

View File

@@ -1,9 +1,9 @@
<% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{neutral_locale(locale)}",
id: "delete-#{locale}",
style: show_delete?(locale),
class: 'float-right delete js-delete-language',
data: { locale: neutral_locale(locale) } %>
data: { locale: locale } %>
<% end %>
@@ -13,7 +13,7 @@
<%= link_to name_for_locale(locale), "#",
style: css_to_display_translation?(resource, locale),
class: "js-globalize-locale-link #{highlight_current?(locale)}",
data: { locale: neutral_locale(locale) },
data: { locale: locale },
remote: true %>
</li>
<% end %>

View File

@@ -1,9 +1,9 @@
<% I18n.available_locales.each do |locale| %>
<%= link_to t("admin.translations.remove_language"), "#",
id: "delete-#{neutral_locale(locale)}",
id: "delete-#{locale}",
style: show_delete?(locale),
class: 'float-right delete js-delete-language',
data: { locale: neutral_locale(locale) } %>
data: { locale: locale } %>
<% end %>
@@ -13,7 +13,7 @@
<%= link_to name_for_locale(locale), "#",
style: site_customization_display_translation?(locale),
class: "js-globalize-locale-link #{highlight_current?(locale)}",
data: { locale: neutral_locale(locale) },
data: { locale: locale },
remote: true %>
</li>
<% end %>

View File

@@ -24,11 +24,9 @@
<%= image_tag(milestone.image_url(:large), { id: "image_#{milestone.id}", alt: milestone.image.title, class: "margin" }) if milestone.image.present? %>
<% globalize(neutral_locale(locale)) do %>
<p>
<%= text_with_links milestone.description %>
</p>
<% end %>
<p>
<%= text_with_links milestone.description %>
</p>
<% if milestone.documents.present? %>
<div class="document-link text-center">

View File

@@ -10,8 +10,7 @@ section "Creating banners" do
post_ended_at: rand((Time.current - 1.day)..(Time.current + 1.week)),
created_at: rand((Time.current - 1.week)..Time.current))
I18n.available_locales.map do |locale|
neutral_locale = locale.to_s.downcase.underscore.to_sym
Globalize.with_locale(neutral_locale) do
Globalize.with_locale(locale) do
banner.description = "Description for locale #{locale}"
banner.title = "Title for locale #{locale}"
banner.save!

View File

@@ -143,8 +143,7 @@ section "Creating investment milestones" do
Budget::Investment.all.each do |investment|
milestone = Budget::Investment::Milestone.new(investment_id: investment.id, publication_date: Date.tomorrow)
I18n.available_locales.map do |locale|
neutral_locale = locale.to_s.downcase.underscore.to_sym
Globalize.with_locale(neutral_locale) do
Globalize.with_locale(locale) do
milestone.description = "Description for locale #{locale}"
milestone.title = I18n.l(Time.current, format: :datetime)
milestone.save!