From 1337cce48f798d5e57101008ed452624196b33a9 Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Thu, 12 Jan 2023 17:13:51 +0100
Subject: [PATCH] add localization to summernote

---
 .../javascripts/application/plugins/summernote.js     |  3 +++
 app/controllers/application_controller/with_locale.rb |  7 +++++++
 app/controllers/server/languages_controller.rb        |  2 +-
 app/models/language.rb                                | 11 ++++++-----
 app/views/admin/communication/blocks/edit.html.erb    |  2 +-
 app/views/admin/layouts/themes/_appstack.html.erb     |  3 ++-
 app/views/admin/layouts/themes/_pure.html.erb         |  3 ++-
 .../application/_summernote_localization.html.erb     |  3 +++
 app/views/layouts/application.html.erb                |  1 +
 app/views/layouts/devise.html.erb                     |  1 +
 app/views/server/languages/_form.html.erb             |  5 ++++-
 app/views/server/languages/index.html.erb             |  2 ++
 app/views/server/languages/show.html.erb              |  2 +-
 config/locales/en.yml                                 |  2 ++
 config/locales/fr.yml                                 |  2 ++
 ...230112151136_add_summernote_locale_in_languages.rb |  5 +++++
 db/schema.rb                                          |  3 ++-
 test/fixtures/languages.yml                           | 11 ++++++-----
 18 files changed, 51 insertions(+), 17 deletions(-)
 create mode 100644 app/views/application/_summernote_localization.html.erb
 create mode 100644 db/migrate/20230112151136_add_summernote_locale_in_languages.rb

diff --git a/app/assets/javascripts/application/plugins/summernote.js b/app/assets/javascripts/application/plugins/summernote.js
index eb7c0d24f..756bc99c5 100644
--- a/app/assets/javascripts/application/plugins/summernote.js
+++ b/app/assets/javascripts/application/plugins/summernote.js
@@ -111,9 +111,12 @@ $(function () {
 
     $('[data-provider="summernote"]').each(function () {
         var config = $(this).attr('data-summernote-config'),
+            locale = $('#summernote-locale').data('locale'),
             options = {};
         config = config || 'default';
         options = configs[config];
+        // if locale is undefined, summernote use default (en-US)
+        options['lang'] = locale;
         $(this).summernote(options);
     });
 
diff --git a/app/controllers/application_controller/with_locale.rb b/app/controllers/application_controller/with_locale.rb
index 196d8cac2..91322f474 100644
--- a/app/controllers/application_controller/with_locale.rb
+++ b/app/controllers/application_controller/with_locale.rb
@@ -3,6 +3,13 @@ module ApplicationController::WithLocale
 
   included do
     around_action :switch_locale
+
+    helper_method :current_language
+
+  end
+
+  def current_language
+    @current_language ||= Language.find_by(iso_code: I18n.locale.to_s)
   end
 
   protected
diff --git a/app/controllers/server/languages_controller.rb b/app/controllers/server/languages_controller.rb
index 471c53269..c1920985e 100644
--- a/app/controllers/server/languages_controller.rb
+++ b/app/controllers/server/languages_controller.rb
@@ -57,6 +57,6 @@ class Server::LanguagesController < Server::ApplicationController
   end
 
   def language_params
-    params.require(:language).permit(:name, :iso_code)
+    params.require(:language).permit(:name, :iso_code, :summernote_locale)
   end
 end
diff --git a/app/models/language.rb b/app/models/language.rb
index c9478e5b5..047a01e1a 100644
--- a/app/models/language.rb
+++ b/app/models/language.rb
@@ -2,11 +2,12 @@
 #
 # Table name: languages
 #
-#  id         :uuid             not null, primary key
-#  iso_code   :string
-#  name       :string
-#  created_at :datetime         not null
-#  updated_at :datetime         not null
+#  id                :uuid             not null, primary key
+#  iso_code          :string
+#  name              :string
+#  summernote_locale :string
+#  created_at        :datetime         not null
+#  updated_at        :datetime         not null
 #
 class Language < ApplicationRecord
 
diff --git a/app/views/admin/communication/blocks/edit.html.erb b/app/views/admin/communication/blocks/edit.html.erb
index 7ac416214..8dfdb9fb7 100644
--- a/app/views/admin/communication/blocks/edit.html.erb
+++ b/app/views/admin/communication/blocks/edit.html.erb
@@ -104,8 +104,8 @@
       },
       initSummernote(element) {
         var config = element.getAttribute('data-summernote-config') || 'default';
-
         $(element).summernote({
+          lang: '<%= current_language.summernote_locale unless current_language.summernote_locale.blank? %>',
           toolbar: window.SUMMERNOTE_CONFIGS[config].toolbar, // Dependent of app/assets/javascripts/admin/plugins/summernote.js
           followingToolbar: true,
           disableDragAndDrop: true,
diff --git a/app/views/admin/layouts/themes/_appstack.html.erb b/app/views/admin/layouts/themes/_appstack.html.erb
index 6191e7000..71a382245 100644
--- a/app/views/admin/layouts/themes/_appstack.html.erb
+++ b/app/views/admin/layouts/themes/_appstack.html.erb
@@ -29,4 +29,5 @@
   <%= javascript_include_tag 'admin/appstack' %>
   <%= render 'gdpr/cookie_consent' %>
   <%= render 'bugsnag' %>
-</body>
\ No newline at end of file
+  <%= render 'summernote_localization' %>
+</body>
diff --git a/app/views/admin/layouts/themes/_pure.html.erb b/app/views/admin/layouts/themes/_pure.html.erb
index 3e28659aa..c93f9af8a 100644
--- a/app/views/admin/layouts/themes/_pure.html.erb
+++ b/app/views/admin/layouts/themes/_pure.html.erb
@@ -20,4 +20,5 @@
   <%= javascript_include_tag 'admin/pure' %>
   <%= render 'gdpr/cookie_consent' %>
   <%= render 'bugsnag' %>
-</body>
\ No newline at end of file
+  <%= render 'summernote_localization' %>
+</body>
diff --git a/app/views/application/_summernote_localization.html.erb b/app/views/application/_summernote_localization.html.erb
new file mode 100644
index 000000000..0fb23463e
--- /dev/null
+++ b/app/views/application/_summernote_localization.html.erb
@@ -0,0 +1,3 @@
+<% unless current_language.summernote_locale.blank? %>
+  <script id="summernote-locale" data-locale="<%= current_language.summernote_locale %>" src="https://cdn.jsdelivr.net/npm/summernote@<%= SummernoteRails::Rails::VERSION.split('.').take(3).join('.') %>/dist/lang/summernote-<%= current_language.summernote_locale %>.js"></script>
+<% end %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index d650055e7..d7a2782bd 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -19,5 +19,6 @@
     <%= render 'footer' %>
     <%= render 'gdpr/cookie_consent' %>
     <%= render 'bugsnag' %>
+    <%= render 'summernote_localization' %>
   </body>
 </html>
diff --git a/app/views/layouts/devise.html.erb b/app/views/layouts/devise.html.erb
index 352fb3438..6c3eb195d 100644
--- a/app/views/layouts/devise.html.erb
+++ b/app/views/layouts/devise.html.erb
@@ -36,5 +36,6 @@
     <%= render 'footer' %>
     <%= render 'gdpr/cookie_consent' %>
     <%= render 'bugsnag' %>
+    <%= render 'summernote_localization' %>
   </body>
 </html>
diff --git a/app/views/server/languages/_form.html.erb b/app/views/server/languages/_form.html.erb
index 4284d412e..9ff935376 100644
--- a/app/views/server/languages/_form.html.erb
+++ b/app/views/server/languages/_form.html.erb
@@ -3,12 +3,15 @@
   <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
 
   <div class="row">
-    <div class="col-md-8">
+    <div class="col-md-4">
       <%= f.input :name %>
     </div>
     <div class="col-md-4">
       <%= f.input :iso_code %>
     </div>
+    <div class="col-md-4">
+      <%= f.input :summernote_locale %>
+    </div>
   </div>
 
   <% content_for :action_bar_right do %>
diff --git a/app/views/server/languages/index.html.erb b/app/views/server/languages/index.html.erb
index ccc92cb8f..61989513b 100644
--- a/app/views/server/languages/index.html.erb
+++ b/app/views/server/languages/index.html.erb
@@ -6,6 +6,7 @@
       <tr>
         <th><%= Language.human_attribute_name('name') %></th>
         <th><%= Language.human_attribute_name('iso_code') %></th>
+        <th><%= Language.human_attribute_name('summernote_locale') %></th>
         <th></th>
       </tr>
     </thead>
@@ -15,6 +16,7 @@
         <tr>
           <td><%= link_to language, [:server, language] %></td>
           <td><%= language.iso_code %></td>
+          <td><%= language.summernote_locale %></td>
           <td class="text-end">
             <div class="btn-group" role="group">
               <%= link_to t('edit'),
diff --git a/app/views/server/languages/show.html.erb b/app/views/server/languages/show.html.erb
index 72b772534..b2260d97c 100644
--- a/app/views/server/languages/show.html.erb
+++ b/app/views/server/languages/show.html.erb
@@ -8,7 +8,7 @@
       </div>
       <div class="table-responsive">
         <table class="<%= table_classes %>">
-          <% ['iso_code'].each do |variable| %>
+          <% ['iso_code', 'summernote_locale'].each do |variable| %>
             <tr>
               <td><%= Language.human_attribute_name(variable) %></td>
               <td class="text-end"><%= @language.public_send variable %></td>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 7111d6398..fc1d24889 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -43,6 +43,7 @@ en:
       language:
         iso_code: Iso code
         name: Name
+        summernote_locale: Summernote localization code
       user:
         admin_theme: Admin Theme
         email: Email
@@ -268,6 +269,7 @@ en:
       language:
         iso_code: ISO 639-1 code (cf <a href="https://fr.wikipedia.org/wiki/Liste_des_codes_ISO_639-1" target="_blank">WikiPedia</a>)
         name: Name in the language (= "Français", "Deutsch", ...)
+        summernote_locale: 'Name of summernote loca (fr-FR / en-US). Check here and ensure locale is available: <a href="https://github.com/summernote/summernote/tree/develop/src/lang" target="_blank">summernote repo</a>'
       user:
         mobile_phone: "International format (+XX). This number remains private. By filling this field, you accept to receive your two-factor authentication codes via SMS."
     include_blanks:
diff --git a/config/locales/fr.yml b/config/locales/fr.yml
index 844af3b00..3bc15636c 100644
--- a/config/locales/fr.yml
+++ b/config/locales/fr.yml
@@ -43,6 +43,7 @@ fr:
       language:
         iso_code: Code Iso
         name: Nom
+        summernote_locale: Nom de la loca Summernote
       user:
         admin_theme: Thème de l'administration
         email: Email
@@ -268,6 +269,7 @@ fr:
       language:
         iso_code: Code ISO 639-1 (cf <a href="https://fr.wikipedia.org/wiki/Liste_des_codes_ISO_639-1" target="_blank">WikiPedia</a>)
         name: Nom dans la langue (= "Français", "Deutsch", ...)
+        summernote_locale: 'Nom de la loca de summernote (fr-FR / en-US). Vérifier ici que la locale est disponible : <a href="https://github.com/summernote/summernote/tree/develop/src/lang" target="_blank">repo summernote</a>'
       user:
         mobile_phone: "Format international (+XX). Ce numéro reste privé. En renseignant ce champ, vous acceptez de recevoir vos codes de double authentification par SMS."
     include_blanks:
diff --git a/db/migrate/20230112151136_add_summernote_locale_in_languages.rb b/db/migrate/20230112151136_add_summernote_locale_in_languages.rb
new file mode 100644
index 000000000..d3c27c1fc
--- /dev/null
+++ b/db/migrate/20230112151136_add_summernote_locale_in_languages.rb
@@ -0,0 +1,5 @@
+class AddSummernoteLocaleInLanguages < ActiveRecord::Migration[7.0]
+  def change
+    add_column :languages, :summernote_locale, :string
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 8fdf9f506..40bb669c2 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema[7.0].define(version: 2023_01_11_083139) do
+ActiveRecord::Schema[7.0].define(version: 2023_01_12_151136) do
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
   enable_extension "plpgsql"
@@ -562,6 +562,7 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_11_083139) do
     t.string "iso_code"
     t.datetime "created_at", null: false
     t.datetime "updated_at", null: false
+    t.string "summernote_locale"
   end
 
   create_table "research_journal_papers", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
diff --git a/test/fixtures/languages.yml b/test/fixtures/languages.yml
index 314cf445e..7927df719 100644
--- a/test/fixtures/languages.yml
+++ b/test/fixtures/languages.yml
@@ -2,11 +2,12 @@
 #
 # Table name: languages
 #
-#  id         :uuid             not null, primary key
-#  iso_code   :string
-#  name       :string
-#  created_at :datetime         not null
-#  updated_at :datetime         not null
+#  id                :uuid             not null, primary key
+#  iso_code          :string
+#  name              :string
+#  summernote_locale :string
+#  created_at        :datetime         not null
+#  updated_at        :datetime         not null
 #
 en:
   name: English
-- 
GitLab