Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Admin: open source translations module
Damian Legawiec8faf1c06adf8
diff --git a/admin/app/controllers/spree/admin/translations_controller.rb b/admin/app/controllers/spree/admin/translations_controller.rbnew file mode 100644index 0000000000..a6a493f543--- /dev/null+++ b/admin/app/controllers/spree/admin/translations_controller.rb@@ -0,0 +1,74 @@+module Spree+ module Admin+ class TranslationsController < Spree::Admin::BaseController+ before_action :set_resource, only: [:edit, :update]+ before_action :load_data, only: [:edit]+ before_action :set_translation_locale, only: [:edit, :update]++ def edit; end++ def update+ locale_translation_params = permitted_translation_params.to_h.transform_values do |translations|+ translations[@selected_translation_locale]+ end++ Mobility.with_locale(@selected_translation_locale) do+ @resource.update!(locale_translation_params)+ end++ flash[:success] = Spree.t('notice_messages.translations_saved')++ redirect_to spree.edit_admin_translation_path(+ @resource,+ resource_type: @resource.class.to_s,+ translation_locale: @selected_translation_locale+ )+ end++ private++ def permitted_translation_params+ params.require(:translation).permit(+ resource_class.translatable_fields.each_with_object({}) do |field, acc|+ acc[field] = current_store.supported_locales_list.map(&:to_sym)+ end+ )+ end++ def resource_class+ @resource_class ||= params[:resource_type].constantize+ end++ def set_resource+ @resource = if resource_class.respond_to?(:friendly)+ resource_class.friendly.find(params[:id])+ else+ resource_class.find(params[:id])+ end+ end++ def set_translation_locale+ @selected_translation_locale = params[:translation_locale].presence || @locales&.first || current_store.supported_locales_list.first+ end++ def load_data+ @locales = (current_store.supported_locales_list - [@default_locale]).sort++ case @resource+ when Spree::Product+ @resource_name = @resource.name+ @back_path = spree.edit_admin_product_path(@resource)+ when Spree::Taxon+ @resource_name = @resource.name+ @back_path = spree.edit_admin_taxonomy_taxon_path(@resource.taxonomy, @resource.id)+ when Spree::Taxonomy+ @resource_name = @resource.name+ @back_path = spree.edit_admin_taxonomy_path(@resource)+ when Spree::Store+ @resource_name = @resource.name+ @back_path = spree.edit_admin_store_path(@resource, section: "general-settings")+ end+ end+ end+ end+enddiff --git a/admin/app/helpers/spree/admin/translations_helper.rb b/admin/app/helpers/spree/admin/translations_helper.rbnew file mode 100644index 0000000000..8f17d511d0--- /dev/null+++ b/admin/app/helpers/spree/admin/translations_helper.rb@@ -0,0 +1,14 @@+module Spree+ module Admin+ module TranslationsHelper+ def link_to_edit_translations(resource, classes: 'text-left dropdown-item')+ link_to_with_icon(+ 'language',+ Spree.t(:translations),+ spree.edit_admin_translation_path(resource, resource_type: resource.class.to_s),+ class: classes+ )+ end+ end+ end+enddiff --git a/admin/app/views/spree/admin/translations/_translations_unavailable.html.erb b/admin/app/views/spree/admin/translations/_translations_unavailable.html.erbnew file mode 100644index 0000000000..9419422212--- /dev/null+++ b/admin/app/views/spree/admin/translations/_translations_unavailable.html.erb@@ -0,0 +1,10 @@+<div class="container">+ <div class="card">+ <div class="card-body">+ <p>To use translations, configure more than one locale for the store.</p>+ <% if can?(:edit, current_store) %>+ <%= button_link_to('Configure store', spree.edit_admin_store_path(current_store)) %>+ <% end %>+ </div>+ </div>+</div>diff --git a/admin/app/views/spree/admin/translations/edit.html.erb b/admin/app/views/spree/admin/translations/edit.html.erbnew file mode 100644index 0000000000..a8a1349548--- /dev/null+++ b/admin/app/views/spree/admin/translations/edit.html.erb@@ -0,0 +1,47 @@+<% content_for :page_title do %>+ <%= page_header_back_button @back_path %>+ <span class="mr-3"><%= Spree.t(:translations_for, resource_name: @resource_name) %></span>+<% end %>+<% if @locales.any?%>+ <% if @locales.many? %>+ <%= form_with url: spree.edit_admin_translation_path(@resource, resource_type: @resource.class.to_s), class: "w-100 d-flex justify-content-end", data: { auto_submit_target: 'form', controller: 'auto-submit', turbo_frame: 'translations_table'}, method: :get do |f| %>+ <div class="form-inline gap-2">+ <%= f.label :translation_locale, Spree.t(:language) %>+ <%= f.select :translation_locale, options_for_select(@locales.map { |locale| [Spree.t('i18n.this_file_language', locale: locale), locale] }, @selected_translation_locale), {}, { class: 'custom-select', data: { action: 'change->auto-submit#submitForm' } } %>+ </div>+ <% end %>+ <% end %>+ <%= form_with url: spree.admin_translation_path(@resource, resource_type: @resource.class.to_s), method: :put, class: 'form-horizontal' do |f| %>+ <%= hidden_field_tag :translation_locale, @selected_translation_locale %>+ <%= turbo_frame_tag :translations_table, class: "table-responsive border rounded bg-white my-3", refresh: "morph" do %>+ <table class="table">+ <thead class="text-muted">+ <tr>+ <th scope="col" class="text-center"><%= Spree.t(:field) %></th>+ <th scope="col" class="text-center" style="width: 40%;"><%= Spree.t('i18n.this_file_language', locale: :en) %> (<%= Spree.t(:default) %>)</th>+ <th scope="col" class="text-center"><%= Spree.t('i18n.this_file_language', locale: @selected_translation_locale) %></th>+ </tr>+ </thead>+ <tbody>+ <% options = { resource: @resource, selected_translation_locale: @selected_translation_locale } %>+ <% case @resource %>+ <% when Spree::Product %>+ <%= render 'spree/admin/translations/products/form', options %>+ <% when Spree::Taxon %>+ <%= render 'spree/admin/translations/taxons/form', options %>+ <% when Spree::Taxonomy %>+ <%= render 'spree/admin/translations/taxonomies/form', options %>+ <% when Spree::Store %>+ <%= render 'spree/admin/translations/stores/form', options %>+ <% end %>+ </tbody>+ </table>+ <% end %>+ <div class="form-actions">+ <%= turbo_save_button_tag Spree.t('actions.update') %>+ <%= link_to Spree.t('actions.cancel'), @back_path, class: 'btn btn-light ml-auto' %>+ </div>+ <% end %>+<% else %>+ <%= render partial: 'spree/admin/translations/translations_unavailable' %>+<% end %>diff --git a/admin/app/views/spree/admin/translations/products/_form.html.erb b/admin/app/views/spree/admin/translations/products/_form.html.erbnew file mode 100644index 0000000000..f771519777--- /dev/null+++ b/admin/app/views/spree/admin/translations/products/_form.html.erb@@ -0,0 +1,23 @@+<% resource.class.translatable_fields.each do |field| %>+ <% if field == :meta_keywords %>+ <% next %>+ <% end %>+ <tr>+ <td class="text-left">+ <%= Spree.t(field) %>+ </td>+ <% [@default_locale, selected_translation_locale].each do |translation_locale| %>+ <% readonly = translation_locale == @default_locale %>+ <td <% if readonly %>style="width: 40%;"<% else %>style="width: 60%;"<% end %>>+ <% case field%>+ <% when :description %>+ <div class="tinymce-container mb-0">+ <%= text_area_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), { rows: 10, class: "form-control spree-rte", readonly: readonly, contenteditable: !readonly } %>+ </div>+ <% else %>+ <%= text_field_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), class: 'form-control', readonly: readonly %>+ <% end %>+ </td>+ <% end %>+ </tr>+<% end %>diff --git a/admin/app/views/spree/admin/translations/stores/_form.html.erb b/admin/app/views/spree/admin/translations/stores/_form.html.erbnew file mode 100644index 0000000000..b8a598666a--- /dev/null+++ b/admin/app/views/spree/admin/translations/stores/_form.html.erb@@ -0,0 +1,23 @@+<% resource.class.translatable_fields.each do |field| %>+ <% if field == :meta_keywords %>+ <% next %>+ <% end %>+ <tr>+ <td class="text-left">+ <%= Spree.t(field) %>+ </td>+ <% [@default_locale, selected_translation_locale].each do |translation_locale| %>+ <% readonly = translation_locale == @default_locale %>+ <td <% if readonly %>style="width: 40%;"<% else %>style="width: 60%;"<% end %>>+ <% case field%>+ <% when :description %>+ <div class="trix-container mb-0">+ <%= rich_text_area_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), { contenteditable: !readonly } %>+ </div>+ <% else %>+ <%= text_field_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), class: 'form-control', readonly: readonly %>+ <% end %>+ </td>+ <% end %>+ </tr>+<% end %>diff --git a/admin/app/views/spree/admin/translations/taxonomies/_form.html.erb b/admin/app/views/spree/admin/translations/taxonomies/_form.html.erbnew file mode 100644index 0000000000..37ed9232ee--- /dev/null+++ b/admin/app/views/spree/admin/translations/taxonomies/_form.html.erb@@ -0,0 +1,20 @@+<% resource.class.translatable_fields.each do |field| %>+ <tr>+ <td class="text-left">+ <%= Spree.t(field) %>+ </td>+ <% [@default_locale, selected_translation_locale].each do |translation_locale| %>+ <% readonly = translation_locale == @default_locale %>+ <td <% if readonly %>style="width: 40%;"<% else %>style="width: 60%;"<% end %>>+ <% case field%>+ <% when :description %>+ <div class="trix-container mb-0">+ <%= rich_text_area_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), { contenteditable: !readonly } %>+ </div>+ <% else %>+ <%= text_field_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), class: 'form-control', readonly: readonly %>+ <% end %>+ </td>+ <% end %>+ </tr>+<% end %>diff --git a/admin/app/views/spree/admin/translations/taxons/_form.html.erb b/admin/app/views/spree/admin/translations/taxons/_form.html.erbnew file mode 100644index 0000000000..3b6fc1cd66--- /dev/null+++ b/admin/app/views/spree/admin/translations/taxons/_form.html.erb@@ -0,0 +1,37 @@+<% resource.class.translatable_fields.each do |field| %>+ <% if field == :pretty_name %>+ <% next %>+ <% end %>+ <tr>+ <td class="text-left">+ <%= Spree.t(field) %>+ </td>+ <% [@default_locale, selected_translation_locale].each do |translation_locale| %>+ <% readonly = translation_locale == @default_locale %>+ <td <% if readonly %>style="width: 40%;"<% else %>style="width: 60%;"<% end %>>+ <% case field%>+ <% when :description %>+ <div class="trix-container mb-0">+ <%= rich_text_area_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), { contenteditable: !readonly } %>+ </div>+ <% when :permalink %>+ <% parent_permalink = resource.parent.present? ? I18n.with_locale(translation_locale) { resource.parent.permalink } + "/" : "" %>+ <div class="form-group">+ <div class="input-group">+ <% if parent_permalink.present? %>+ <div class="input-group-prepend">+ <span class="input-group-text">+ <%= parent_permalink %>+ </span>+ </div>+ <% end %>+ <%= text_field_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field)&.delete_prefix(parent_permalink), class: 'form-control', readonly: readonly %>+ </div>+ </div>+ <% else %>+ <%= text_field_tag "translation[#{field}][#{translation_locale}]", resource.get_field_with_locale(translation_locale, field), class: 'form-control', readonly: readonly %>+ <% end %>+ </td>+ <% end %>+ </tr>+<% end %>