Make portuguese locale work

There was a problem with the portuguese locale.
The locale was pt-BR, but `globalize_accessors` gem
doesn't allow the creation of methods using locales
with that format.

To avoid transforming pt-BR to pt and lose the distinction
of the different variations of the language, a function has
been added to transform pt-BR into pt_br (without changing
the locale itself). That way, when globalize uses the locales,
all of them will have a valid format (downcased and underscored)
AND they will be always the same (comparing pt-BR with pt_br
doesn't work).
This commit is contained in:
iagirre
2018-04-12 10:03:45 +02:00
parent 285e02ce96
commit 8b9d1ebd33
5 changed files with 20 additions and 16 deletions

View File

@@ -10,16 +10,16 @@ module GlobalizeHelper
def locale_options
I18n.available_locales.map do |locale|
[name_for_locale(locale), locale]
[name_for_locale(locale), neutral_locale(locale)]
end
end
def display_translation?(locale)
I18n.locale == locale ? "" : "display: none"
neutral_locale(I18n.locale) == neutral_locale(locale) ? "" : "display: none"
end
def css_to_display_translation?(resource, locale)
resource.translated_locales.include?(locale) || locale == I18n.locale ? "" : "display: none"
resource.translated_locales.include?(neutral_locale(locale)) || locale == I18n.locale ? "" : "display: none"
end
def disable_translation?(locale)
@@ -38,4 +38,8 @@ module GlobalizeHelper
I18n.locale == locale ? '' : 'display: none'
end
def neutral_locale(locale)
locale.to_s.downcase.underscore.to_sym
end
end

View File

@@ -7,8 +7,8 @@ class Budget
max_file_size: 3.megabytes,
accepted_content_types: [ "application/pdf" ]
translates :description, touch: true
globalize_accessors locales: [:en, :es, :fr, :nl, :val]
translates :title, :description, touch: true
globalize_accessors locales: [:en, :es, :fr, :nl, :val, :pt_br]
belongs_to :investment

View File

@@ -24,7 +24,7 @@
class: "margin",
id: "image_#{milestone.id}" }) if milestone.image.present? %>
<p>
<% Globalize.with_locale(locale) do %>
<% Globalize.with_locale(neutral_locale(locale)) do %>
<%= text_with_links milestone.description %>
<% end %>
</p>
@@ -49,4 +49,4 @@
</section>
</div>
</div>
</div>
</div>

View File

@@ -3,13 +3,13 @@
<%= link_to name_for_locale(locale), "#",
style: css_to_display_translation?(@milestone, locale),
class: "js-globalize-locale-link #{highlight_current?(locale)}",
data: { locale: locale },
data: { locale: neutral_locale(locale) },
remote: true %>
<%= link_to t("admin.milestones.form.remove_language"), "#",
id: "delete-#{locale}",
style: show_delete?(locale),
style: show_delete?(neutral_locale(locale)),
class: 'float-right delete-language',
data: { locale: locale } %>
data: { locale: neutral_locale(locale) } %>
</span>
<% end %>

View File

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