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

Add a translation helper for locale switching. #3507

Open
wants to merge 1 commit into
base: main
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
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ path "./" do
gem "refinerycms-resources"
end

# routing-filter needs a newer release than version 0.6.3
gem 'refinerycms-i18n', github: 'refinery/refinerycms-i18n', branch: 'master'
gem 'routing-filter', github: 'svenfuchs/routing-filter', branch: 'master'

# Add support for refinerycms-acts-as-indexed
gem 'refinerycms-acts-as-indexed', ['~> 3.0', '>= 3.0.0']
Expand Down
36 changes: 35 additions & 1 deletion core/app/helpers/refinery/translation_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Refinery
module TranslationHelper

# Overrides Rails' core I18n.t() function to produce a more helpful error message.
# The default one wreaks havoc with CSS and makes it hard to understand the problem.
def t(key, options = {})
Expand All @@ -14,5 +13,40 @@ def t(key, options = {})
def translated_field(record, field)
Refinery::TranslatedFieldPresenter.new(record).call(field)
end

# Markup for the translation links on index pages
def switch_locale(object, edit_path, field = :title)
return unless Refinery::I18n.frontend_locales.many?

path_minus_locale = remove_locale_from_path(edit_path)

object.translations
.sort_by { |t| Refinery::I18n.frontend_locales.index(t.locale) }
.select { |t| t.send(field).present? }
.map { |t| locale_markup(path_minus_locale, t.locale) }
.join(' ')
end

private

def locale_markup(path, locale)
link_to locale_marker(locale.upcase), locale_edit_path(path, locale), class: [:locale, :edit], title: locale.upcase
end

# just adding back the switch-locale with the new locale
def locale_edit_path(path, locale)
"#{path}?switch_locale=#{locale}"
end

def locale_marker(locale)
tag.span locale_text_icon(locale), class: [locale, :locale_marker]
end

def remove_locale_from_path(edit_path)
uri = URI.parse(edit_path)
queries = URI.decode_www_form(uri.query || '').to_h
uri.query = queries.except('switch-locale').presence
uri.to_s
end
end
end
13 changes: 10 additions & 3 deletions images/app/views/refinery/admin/images/_grid_view.html.erb
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
<ul id="image_grid" class="<%= ['clearfix', 'pagination_frame', 'images_list', pagination_css_class].compact.join(' ') %>">
<% @images.each_with_index do |image, index| -%>
<li id="image_<%= image.id %>" class="image_<%= index % 4 %>">
<%
edit_url = refinery.edit_admin_image_path(image)
delete_url = refinery.admin_image_path(image, params.to_unsafe_h.slice(:page) )
delete_options = {
class: 'confirm-delete',
data: { confirm: t('message', scope: 'refinery.admin.delete', title: image.title) } }
%>

<%= image_fu image, '149x149#c', title: image.title %>
<span class="actions">

<%= action_icon :preview, image.url, t('view_live_html', scope: 'refinery.admin.images') %>
<%= action_icon :edit, refinery.edit_admin_image_path(image), t('edit', scope: 'refinery.admin.images') %>
<%= action_icon :delete, refinery.admin_image_path(image, params.to_unsafe_h.slice(:page)), t('delete', scope: 'refinery.admin.images'), class: 'confirm-delete',
data: { confirm: t('message', scope: 'refinery.admin.delete', title: image.title) } %>
<%= action_icon :edit, edit_url, t('edit', scope: 'refinery.admin.images') %>
<%= action_icon :delete, delete_url, t('delete', scope: 'refinery.admin.images'), delete_options %>
<%= action_icon :info, '#', "Title: #{image.title} Alt text: #{image.alt}" %>

</span>
Expand Down
2 changes: 1 addition & 1 deletion images/app/views/refinery/admin/images/_list_view.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<% date = Date.parse(date_time.to_s) %>
<h3><%= l(date, format: :long ) %></h3>
<ul class='images_list'>
<%= render partial: 'list_view_image', collection: image_group %>
<%= render partial: "list_view_image", collection: image_group, as: :image %>
</ul>
<% end %>
</div>
39 changes: 16 additions & 23 deletions images/app/views/refinery/admin/images/_list_view_image.html.erb
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
<li id="sortable_<%= list_view_image.id %>" class="clearfix record <%= cycle('on', 'on-hover') %>">
<%
edit_url = refinery.edit_admin_image_path(image)
delete_url = refinery.admin_image_path(image)
delete_options = {
class: 'confirm-delete',
data: { confirm: t('message', scope: 'refinery.admin.delete', title: image.title) } }
%>
<li id="sortable_<%= image.id %>" class="clearfix record <%= cycle('on', 'on-hover') %>">
<span class="title">
<%= translated_field(list_view_image, :title) %>
<%= translated_field(image, :title) %>
</span>
<span class='locales'>
<%= switch_locale(image, edit_url, :image_title).html_safe if Refinery::I18n.frontend_locales.many? %>
</span>

<% if Refinery::I18n.frontend_locales.many? %>
<span class='locales'>
<% list_view_image.translations.sort_by{ |t| Refinery::I18n.frontend_locales.index(t.locale)}.each do |translation| %>
<% if translation.image_title.present? %>
<%= link_to refinery.edit_admin_image_path(list_view_image, switch_locale: translation.locale),
class: 'locale', title: translation.locale.upcase do %>

<div class="<%=translation.locale %> locale_marker">
<%= locale_text_icon(translation.locale.upcase) %>
</div>
<% end %>
<% end %>
<% end %>
</span>
<% end %>

<span class="preview"><%= list_view_image.image_name %></span>
<span class="preview"><%= image.image_name %></span>
<span class="actions">

<%= action_icon :preview, list_view_image.url, t('view_live_html', scope: 'refinery.admin.images') %>
<%= action_icon :edit, refinery.edit_admin_image_path(list_view_image), t('edit', scope: 'refinery.admin.images') %>
<%= action_icon :delete, refinery.admin_image_path(list_view_image), t('delete', scope: 'refinery.admin.images'), class: 'confirm-delete',
data: { confirm: t('message', scope: 'refinery.admin.delete', title: list_view_image.title)} %>
<%= action_icon :preview, image.url, t('view_live_html', scope: 'refinery.admin.images') %>
<%= action_icon :edit, edit_url, t('edit', scope: 'refinery.admin.images') %>
<%= action_icon :delete, delete_url, t('delete', scope: 'refinery.admin.images'), delete_options %>
</span>
</li>
1 change: 1 addition & 0 deletions images/spec/support/shared_examples/image_deleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
before do
raise "please set let(:initial_path)" if initial_path.blank?
ensure_on(initial_path)
initialize_context
end

let(:image_count) {[Refinery::Image.count, Refinery::Images.pages_per_admin_index].min}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class PagesDialogsController < ::Refinery::Admin::DialogsController
helper :'refinery/admin/pages'

def link_to
# Get the switch_local variable to determine the locale we're currently editing

# Get the switch_locale variable to determine the locale we're currently editing
# Set up Mobility with our current locale
Mobility.locale = if params[:switch_locale].present? && Refinery::I18n.built_in_locales.keys.map(&:to_s).include?(params[:switch_locale])
Mobility.locale = params[:switch_locale]
Expand Down
19 changes: 3 additions & 16 deletions pages/app/views/refinery/admin/pages/_page.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,9 @@
<%= translated_field(page, :title) %>
<%= page_meta_information page %>
</span>

<% if Refinery::I18n.frontend_locales.many? %>
<span class='locales'>
<% page.translations.sort_by{ |t| Refinery::I18n.frontend_locales.index(t.locale)}.each do |translation| %>
<% if translation.title.present? %>
<%= link_to refinery.admin_edit_page_path(page.nested_url, switch_locale: translation.locale),
class: 'locale', title: translation.locale.upcase do %>

<div class="<%=translation.locale %> locale_marker">
<%= locale_text_icon(translation.locale.upcase) %>
</div>
<% end %>
<% end %>
<% end %>
</span>
<% end %>
<span class='locales'>
<%= switch_locale(page, edit_url, :title,).html_safe if Refinery::I18n.frontend_locales.many? %>
</span>

<span class='actions'>
<%= action_icon(:preview, page.url, t('.view_live_html')) %>
Expand Down
47 changes: 17 additions & 30 deletions resources/app/views/refinery/admin/resources/_resource.html.erb
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
<%
edit_url = refinery.edit_admin_resource_path(resource)
delete_url = refinery.admin_resource_path(resource)
delete_options = {data: {confirm: t('message', scope: 'refinery.admin.delete', title: resource.title)}}
edit_url = refinery.edit_admin_resource_path(resource)
delete_url = refinery.admin_resource_path(resource)
delete_options = { data: { confirm: t('message', scope: 'refinery.admin.delete', title: resource.title) } }
%>
<li class="clearfix record <%= cycle('on', 'on-hover') %>">
<li class="clearfix record <%= cycle('on', 'on-hover') %>" >

<span class="title <%= resource.ext.try(:downcase) %>">
<span class="title <%= resource.ext.try(:downcase) %>" >
<%= translated_field(resource, :title) %>
</span>
</span >
<span class='locales' >
<%= switch_locale(resource, edit_url, :resource_title) if Refinery::I18n.frontend_locales.many? %>
</span >

<% if Refinery::I18n.frontend_locales.many? %>
<span class='locales'>
<% resource.translations.sort_by{ |t| Refinery::I18n.frontend_locales.index(t.locale)}.each do |translation| %>
<% if translation.resource_title.present? %>
<%= link_to refinery.edit_admin_resource_path(resource, switch_locale: translation.locale),
class: 'locale', title: translation.locale.upcase do %>

<div class="<%=translation.locale %> locale_marker">
<%= locale_text_icon(translation.locale.upcase) %>
</div>
<% end %>
<% end %>
<% end %>
</span>
<% end %>

<span class="preview">
<span class="preview" >
<%= resource.file_name %> - <%= number_to_human_size(resource.size) %>
</span>
</span >

<span class="actions">
<%= action_icon :download, resource.url, t('.download', size: number_to_human_size(resource.size)) %>
<%= action_icon :edit, edit_url, t('edit', scope: 'refinery.admin.resources') %>
<%= action_icon :delete, delete_url, t('delete', scope: 'refinery.admin.resources'), delete_options %>
</span>
</li>
<span class="actions" >
<%= action_icon :download, resource.url, t('.download', size: number_to_human_size(resource.size)) %>
<%= action_icon :edit, edit_url, t('edit', scope: 'refinery.admin.resources') %>
<%= action_icon :delete, delete_url, t('delete', scope: 'refinery.admin.resources'), delete_options %>
</span >
</li >