Skip to content

Commit

Permalink
Merge pull request #228 from ontoportal-lirmm/feature/support-multi-l…
Browse files Browse the repository at this point in the history
…ingual

Feature | Support multi-lingual: UI Internationalization
  • Loading branch information
syphax-bouazzouni committed Sep 5, 2023
1 parent 8e8ce1a commit 71c852a
Show file tree
Hide file tree
Showing 34 changed files with 1,439 additions and 564 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ gem 'flamegraph'
gem 'graphql-client'
gem 'haml', '~> 5.1'
gem 'i18n'
gem 'rails-i18n', '~> 7.0.0'
gem 'iconv'
gem 'multi_json'
gem 'mysql2', '0.5.3'
Expand Down
24 changes: 23 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.2.2)
crass (1.0.6)
css_parser (1.14.0)
addressable
cube-ruby (0.0.3)
daemons (1.4.1)
dalli (3.2.4)
Expand Down Expand Up @@ -170,6 +172,8 @@ GEM
haml (>= 4.0)
nokogiri (>= 1.6.0)
ruby_parser (~> 3.5)
htmlbeautifier (1.4.2)
htmlentities (4.3.4)
http-accept (1.7.0)
http-cookie (1.0.5)
domain_name (~> 0.5)
Expand Down Expand Up @@ -197,6 +201,19 @@ GEM
loofah (2.20.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
lookbook (1.5.5)
actioncable
activemodel
css_parser
htmlbeautifier (~> 1.3)
htmlentities (~> 4.3.4)
listen (~> 3.0)
railties (>= 5.0)
redcarpet (~> 3.5)
rouge (>= 3.26, < 5.0)
view_component (> 2.0, < 4)
yard (~> 0.9.25)
zeitwerk (~> 2.5)
lz4-ruby (0.3.3)
mail (2.8.1)
mini_mime (>= 0.1.1)
Expand Down Expand Up @@ -278,6 +295,9 @@ GEM
nokogiri (>= 1.6)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
rails-i18n (7.0.6)
i18n (>= 0.7, < 2)
railties (>= 6.0.0, < 8)
rails_autolink (1.1.8)
actionview (> 3.1)
activesupport (> 3.1)
Expand Down Expand Up @@ -450,6 +470,7 @@ DEPENDENCIES
jquery-ui-rails
jsbundling-rails
listen
lookbook (~> 1.5.5)
multi_json
mysql2 (= 0.5.3)
net-ftp (~> 0.2.0)
Expand All @@ -463,6 +484,7 @@ DEPENDENCIES
puma (~> 5.0)
rack-mini-profiler
rails (= 7.0.3)
rails-i18n (~> 7.0.0)
rails_autolink
rdoc
recaptcha (~> 5.9.0)
Expand All @@ -486,4 +508,4 @@ DEPENDENCIES
will_paginate (~> 3.0)

BUNDLED WITH
2.3.23
2.4.12
23 changes: 23 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,29 @@
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

before_action :set_locale

# Sets the locale based on the locale cookie or the value returned by detect_locale.
def set_locale
I18n.locale = cookies[:locale] || detect_locale
cookies.permanent[:locale] = I18n.locale if cookies[:locale].nil?
end

# Returns detedted locale based on the Accept-Language header of the request or the default locale if none is found.
def detect_locale
languages = request.headers['Accept-Language']&.split(',')
supported_languages = I18n.available_locales

languages.each do |language|
language_code = language.split(/[-;]/).first.downcase.to_sym
return language_code if supported_languages.include?(language_code)
end

return I18n.default_locale
end


helper :all # include all helpers, all the time
helper_method :bp_config_json, :current_license, :using_captcha?
rescue_from ActiveRecord::RecordNotFound, with: :not_found_record
Expand Down
21 changes: 21 additions & 0 deletions app/controllers/language_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class LanguageController < ApplicationController

# set locale to the language selected by the user
def set_locale_language
language = params[:language].strip.downcase.to_sym
supported_languages = I18n.available_locales

if language
if supported_languages.include?(language)
cookies.permanent[:locale] = language
else
# in case we want to show a message if the language is not available
flash.now[:notice] = "#{language} translation not available"
logger.error flash.now[:notice]
end
end

redirect_to request.referer || root_path
end

end
15 changes: 6 additions & 9 deletions app/helpers/home_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
module HomeHelper

def render_footer_link(options = {})

link_content = options[:text].presence
link_content ||= image_tag(options[:img_src]) if options[:img_src].present?
link_content ||= content_tag(:i, '', class: options[:icon]) if options[:icon].present?

unless link_content.blank?
link_to(link_content, options[:url], target: options[:target], class: options[:css_class].to_s, style: options[:text].blank? ? 'text-decoration: none' : '')
end.to_s.html_safe

link_content = options[:text][I18n.locale] || options[:text][:en] if options[:text]
link_content ||= image_tag(options[:img_src]) if options[:img_src]
link_content ||= content_tag(:i, '', class: options[:icon]) if options[:icon]

link_to(link_content, options[:url], target: options[:target], class: options[:css_class].to_s, style: options[:text].blank? ? 'text-decoration: none' : '').html_safe if link_content
end

end
10 changes: 4 additions & 6 deletions app/helpers/schemes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ def get_schemes_labels(schemes, main_uri)
end

def concept_label_to_show(submission: @submission_latest)
submission&.hasOntologyLanguage == 'SKOS' ? 'Concepts' : 'Classes'
submission&.hasOntologyLanguage == 'SKOS' ? 'concepts' : 'classes'
end

def section_name(section)
if section.eql?('classes')
concept_label_to_show(submission: @submission_latest || @submission)
else
section.capitalize
end
section = concept_label_to_show(submission: @submission_latest || @submission) if section.eql?('classes')

t("ontology_details.sections.#{section}")
end

def scheme_path(scheme_id = '')
Expand Down
6 changes: 6 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,15 @@ application.register("load-chart", LoadChartController)
import MetadataDownloaderController from "./metadata_downloader_controller"
application.register("metadata-downloader", MetadataDownloaderController)

import OntologyViewerTabsController from "./ontology_viewer_tabs_controller"
application.register("ontology-viewer-tabs", OntologyViewerTabsController)

import OntoportalAutocompleteController from "./ontoportal_autocomplete_controller"
application.register("ontoportal-autocomplete", OntoportalAutocompleteController)

import PlatformLanguageController from "./platform_language_controller"
application.register("platform-language", PlatformLanguageController)

import ShowModalController from "./show_modal_controller"
application.register("show-modal", ShowModalController)

Expand Down
5 changes: 3 additions & 2 deletions app/javascript/controllers/language_change_controller.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="language-change"
// This controller is used to change the language of the Concepts, Schemes and Collections
export default class extends Controller {

dispatchLangChangeEvent() {

debugger
this.element.dispatchEvent(new CustomEvent('lang_changed', {
bubbles: true,
cancelable: true,
Expand All @@ -14,6 +15,6 @@ export default class extends Controller {
}
}
}));

}

}
21 changes: 21 additions & 0 deletions app/javascript/controllers/platform_language_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Controller } from "@hotwired/stimulus"
import { Turbo } from "@hotwired/turbo-rails";
import { getCookie } from "../mixins/cookie";

// Connects to data-controller="platform-language"
// this controller is used to change the language of the whole platform
export default class extends Controller {

connect() {
const locale = getCookie('locale');

const option = document.querySelector(`#language-select option[value="${locale}"]`);
option && (option.selected = true);

}

handleLangChanged(event) {
const userPreferedLanguage = event.target.value;
Turbo.visit(`/locale/${userPreferedLanguage}`, { action: "replace" });
}
}
8 changes: 8 additions & 0 deletions app/javascript/mixins/cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const getCookie = (name) => {
const cookieValue = document.cookie.match('(^|[^;]+)\\s*' + name + '\\s*=\\s*([^;]+)');
return cookieValue ? cookieValue.pop() : '';
}

export const setCookie = (name, value, days) => {
document.cookie = `${name}=${value};max-age=${days * 24 * 60 * 60}`;
}
Loading

0 comments on commit 71c852a

Please sign in to comment.