Skip to content
Snippets Groups Projects
Unverified Commit 4ee7914a authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

cc

parent 3c0bc2d0
No related branches found
No related tags found
No related merge requests found
......@@ -62,28 +62,25 @@ class Communication::Website::Menu < ApplicationRecord
item_translation.menu = menu_translation
item_translation.parent = parent_translation
case item_translation.kind
when 'blank', 'url'
# Nothing to do
when 'program', 'diploma', 'volume', 'paper'
# TODO: Translate Education & Research Models
when 'page', 'category', 'post'
translated_about = item.about.translation_for(menu_translation.language)
if translated_about.present?
item_translation.about = translated_about
elsif item.children.any?
# Convert to a blank menu item to translate children correctly
item_translation.kind = 'blank'
item_translation.about = nil
else
# Skip menu item if no translation and no children to translate
return
# TODO : I18n
# For now, only pages, posts, categories are handled.
# We need to translate programs, diplomas, volumes and papers
set_item_translation_attributes(item_translation, item, menu_translation)
# If no translation and no children to translate, translation won't be save, as about is nil and kind requires one.
if item_translation.save
item.children.ordered.each do |child|
translate_menu_item!(child, menu_translation, item_translation)
end
end
end
item_translation.save
item.children.ordered.each do |child|
translate_menu_item!(child, menu_translation, item_translation)
def set_item_translation_attributes(item_translation, item, menu_translation)
return unless item.about.present? && item.about.respond_to?(:translation_for)
# Search for the target translation based on the given language.
item_translation.about = item.about.translation_for(menu_translation.language)
# If no target translation found, convert to a blank menu item if item has children.
item_translation.kind = 'blank' if item_translation.about.nil? && item.children.any?
end
end
end
......@@ -50,12 +50,6 @@ module WithTranslations
def translate!(language)
translation = self.dup
# Translate parent if needed
if respond_to?(:parent_id) && parent_id.present?
parent_translation = parent.find_or_translate!(language)
translation.parent_id = parent_translation&.id
end
# Inherits from original_id or set it to itself
translation.assign_attributes(
original_id: original_object.id,
......@@ -64,6 +58,8 @@ module WithTranslations
# Handle publication
translation.published = false if respond_to?(:published)
# Translate parent if needed
translation.parent_id = translate_parent!(language)&.id if respond_to?(:parent_id)
# Handle featured image if object has one
translate_attachment(translation, :featured_image) if respond_to?(:featured_image) && featured_image.attached?
translation.save
......@@ -76,6 +72,11 @@ module WithTranslations
protected
def translate_parent!(language)
return nil if parent_id.nil?
parent.find_or_translate!(language)
end
def translate_blocks!(translation)
blocks.ordered.each do |block|
block.translate!(translation)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment