Skip to content
Snippets Groups Projects
Commit 3e65f7e4 authored by Arnaud Levy's avatar Arnaud Levy
Browse files

wip

parent ed6b57e3
No related branches found
No related tags found
No related merge requests found
class Admin::TranslationController < Admin::ApplicationController
def translate
@target = translation_params[:target]
# @response = LibreTranslate.translate text: translation_params[:text],
# from: translation_params[:from],
# to: translation_params[:to]
@response = {
'translatedText' => 'test'
}
end
protected
def translation_params
params.permit(:text, :from, :to, :target)
end
end
......@@ -3,39 +3,38 @@ module Admin::Translatable
included do
before_action :check_or_redirect_translatable_resource, only: [:show, :edit, :update, :destroy]
end
protected
# If we don't have a website, it will not work
protected
def check_or_redirect_translatable_resource
# Early return if language is correct
return if resource.language_id == current_website_language.id
# Look up for translation or translate (with blocks and all) from resource
translation = resource.find_or_translate!(current_website_language)
# Redirect to the translation
redirect_to_translation(translation)
end
# If we don't have a website, it will not work
def redirect_to_translation(translation)
if ['edit', 'update'].include?(action_name) || translation.newly_translated
# Safety net on update action if called on wrong language
# There's an attribute accessor named "newly_translated" that we set to true
# when we just created the translation. We use it to redirect to the form instead of the show.
redirect_to [:edit, :admin, translation.becomes(translation.class.base_class)]
else
# Safety net on destroy action if called on wrong language
redirect_to [:admin, translation.becomes(translation.class.base_class)]
end
end
def check_or_redirect_translatable_resource
# Early return if language is correct
return if resource.language_id == current_website_language.id
# Look up for translation or translate (with blocks and all) from resource
translation = resource.find_or_translate!(current_website_language)
# Redirect to the translation
redirect_to_translation(translation)
end
def resource_name
self.class.to_s.remove("Controller").demodulize.singularize.underscore
def redirect_to_translation(translation)
if ['edit', 'update'].include?(action_name) || translation.newly_translated
# Safety net on update action if called on wrong language
# There's an attribute accessor named "newly_translated" that we set to true
# when we just created the translation. We use it to redirect to the form instead of the show.
redirect_to [:edit, :admin, translation.becomes(translation.class.base_class)]
else
# Safety net on destroy action if called on wrong language
redirect_to [:admin, translation.becomes(translation.class.base_class)]
end
end
def resource
instance_variable_get("@#{resource_name}")
end
def resource_name
self.class.to_s.remove("Controller").demodulize.singularize.underscore
end
def resource
instance_variable_get("@#{resource_name}")
end
end
......@@ -46,6 +46,10 @@ module WithTranslations
@original_object ||= (self.original || self)
end
def is_a_translation?
self.original.present?
end
def original_with_translations
original_object.translations + [original_object]
end
......
......@@ -5,6 +5,16 @@
<div class="row">
<div class="col-md-8">
<%= osuny_panel t('content') do %>
<%= link_to 'Traduire',
admin_translate_path(
text: post.title,
from: post.original.language.iso_code,
to: post.language.iso_code,
target: 'communication_website_post_title'
),
method: :post,
class: 'action float-end',
remote: true if post.is_a_translation? %>
<%= f.input :title %>
<%= render 'admin/application/summary/form', f: f, about: post %>
<%= f.input :text, as: :summernote if strip_tags(post.text).present? %>
......
document.getElementById("<%= @target %>").value = "<%= @response['translatedText'] %>";
notyf = new Notyf();
notyf.open({
type: 'success',
position: {
x: 'center',
y: 'bottom'
},
message: "<%= t('admin.translation.done') %>",
duration: 9000,
ripple: true,
dismissible: true
});
\ No newline at end of file
......@@ -144,6 +144,8 @@ fr:
summary:
label: Résumé
hint: Un texte court, comme un chapô pour un article. Ne mettez pas tout le contenu ici, pour ça, il y a les blocs !
translation:
done: Traduction effectuée !
users_alerts:
already_confirmed: "Votre compte est déjà confirmé."
not_locked_html: "<i>%{model}</i> n'était pas verrouillé(e)."
......
......@@ -23,6 +23,7 @@ Rails.application.routes.draw do
post 'resend_confirmation_email' => 'users#resend_confirmation_email', on: :member
patch 'unlock' => 'users#unlock', on: :member
end
post 'translate' => 'translation#translate', as: :translate
put 'theme' => 'application#set_theme', as: :set_theme
put 'favorite' => 'users#favorite', as: :favorite
draw 'admin/administration'
......
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