Skip to content

Commit

Permalink
Feature : internationalization part 21 - Controllers: language, login…
Browse files Browse the repository at this point in the history
…, mappings, notes and ncbo annotator plus + MultiLanguages helper (#501)

* internationalization of mappings controller

* internationalization of notes controller

* internationalization of login controller

* internationalization of language controller

* internationalization of ncbo annotator plus controller

* update of en.yml file

* internationalization of MultiLanguages helper

* skip annotator page weird test

* put back annotator page test

* include translation helper in notes controller

* create a class function for controllers translating

* remove binding.pry in  notes_controller.rb

---------

Co-authored-by: Bilel KIHAL <[email protected]>
Co-authored-by: Syphax bouazzouni <[email protected]>
  • Loading branch information
3 people authored Feb 28, 2024
1 parent aaff740 commit a88898c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 34 deletions.
7 changes: 6 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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
Expand Down Expand Up @@ -712,6 +712,11 @@ def submission_metadata
def request_lang
helpers.request_lang
end

def self.t(*args)
I18n.t(*args)
end

private
def not_found_record(exception)
@error_message = exception.message
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/language_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def set_locale_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"
flash.now[:notice] = t('language.translation_not_available', language: language)
logger.error flash.now[:notice]
end
end
Expand Down
20 changes: 10 additions & 10 deletions app/controllers/login_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create

redirect_to redirect, allow_other_host: true
else
@errors << "Invalid account name/password combination"
@errors << t('login.invalid_account_combination')
render :action => 'index'
end
else
Expand All @@ -59,7 +59,7 @@ def create_omniauth

redirect_to redirect
else
@errors = ["#{params[:provider]} authentication failed"]
@errors = [t('login.authentication_failed', provider: params[:provider])]
render :action => 'index'
end
end
Expand Down Expand Up @@ -90,10 +90,10 @@ def destroy
old_user = session[:user]
session[:user] = session[:admin_user]
session.delete(:admin_user)
flash[:success] = "Logged out <b>#{old_user.username}</b>, returned to <b>#{session[:user].username}</b>".html_safe
flash[:success] = t('login.admin_logged_out', old_user: old_user.username, user: session[:user].username).html_safe
else
session[:user] = nil
flash[:success] = "You have successfully logged out"
flash[:success] = t('login.user_logged_out')
end
redirect_to request.referer || "/"
end
Expand All @@ -114,7 +114,7 @@ def send_pass
if resp.nil?
redirect_to "/lost_pass_success"
else
flash[:notice] = resp.errors.first + ". Please try again."
flash[:notice] = resp.errors.first + t('login.try_again_notice')
redirect_to "/lost_pass"
end
end
Expand All @@ -129,7 +129,7 @@ def reset_password
login(@user)
render "users/edit"
else
flash[:notice] = @user.errors.first + ". Please reset your password again."
flash[:notice] = @user.errors.first + t('login.reset_password_again')
redirect_to "/lost_pass"
end
end
Expand All @@ -139,19 +139,19 @@ def reset_password
def login(user)
return unless user
session[:user] = user
custom_ontologies_text = session[:user].customOntology && !session[:user].customOntology.empty? ? "The display is now based on your <a href='/account#custom_ontology_set'>Custom Ontology Set</a>." : ""
notice = "Welcome <b>" + user.username.to_s + "</b>! " + custom_ontologies_text
custom_ontologies_text = session[:user].customOntology && !session[:user].customOntology.empty? ? t('login.custom_ontology_set') : ""
notice = t('login.welcome') + user.username.to_s + "</b>! " + custom_ontologies_text
flash[:success] = notice.html_safe
end

def validate(params)
errors=[]

if params[:username].nil? || params[:username].length <1
errors << "Please enter an account name"
errors << t('login.error_account_name')
end
if params[:password].nil? || params[:password].length <1
errors << "Please enter a password"
errors << t('login.error_password')
end

return errors
Expand Down
22 changes: 11 additions & 11 deletions app/controllers/mappings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def index
# Adding external and interportal mappings to the dropdown list
if ontology_acronym.to_s == EXTERNAL_MAPPINGS_GRAPH
mapping_count = ontologies_mapping_count[ontology_acronym.to_s] || 0
select_text = "External Mappings (#{number_with_delimiter(mapping_count, delimiter: ',')})" if mapping_count >= 0
select_text = t('mappings.external_mappings', number_with_delimiter: number_with_delimiter(mapping_count, delimiter: ',')) if mapping_count >= 0
ontology_acronym = EXTERNAL_URL_PARAM_STR
elsif ontology_acronym.to_s.start_with?(INTERPORTAL_MAPPINGS_GRAPH)
mapping_count = ontologies_mapping_count[ontology_acronym.to_s] || 0
select_text = "Interportal Mappings - #{ontology_acronym.to_s.split("/")[-1].upcase} (#{number_with_delimiter(mapping_count, delimiter: ',')})" if mapping_count >= 0
select_text = t('mappings.interportal_mappings', acronym: ontology_acronym.to_s.split("/")[-1].upcase, number_with_delimiter: number_with_delimiter(mapping_count, delimiter: ',')) if mapping_count >= 0
ontology_acronym = INTERPORTAL_URL_PARAM_STR + ontology_acronym.to_s.split("/")[-1]
else
ontology = ontologies_hash[ontology_acronym.to_s]
Expand All @@ -62,7 +62,7 @@ def loader
"classes": ["http://bioontology.org/ontologies/BiomedicalResourceOntology.owl#Image_Algorithm",
"http://purl.org/incf/ontology/Computational_Neurosciences/cno_alpha.owl#cno_0000202"],

"name": 'This is the mappings produced to test the bulk load',
"name": t('mappings.test_bulk_load'),
"source": 'https://w3id.org/semapv/LexicalMatching',
"comment": 'mock data',
"relation": [
Expand Down Expand Up @@ -111,7 +111,7 @@ def show_mappings
if @ontology.nil?
ontology_acronym = params[:id]
if params[:id] == EXTERNAL_URL_PARAM_STR
@ontology_name = "External Mappings"
@ontology_name = t('mappings.external_mappings')
elsif params[:id].start_with?(INTERPORTAL_URL_PARAM_STR)
@ontology_name = params[:id].sub(":", " - ")
end
Expand All @@ -122,10 +122,10 @@ def show_mappings
if @target_ontology.nil?
if params[:target] == EXTERNAL_MAPPINGS_GRAPH
target_acronym = EXTERNAL_URL_PARAM_STR
@target_ontology_name = "External Mappings"
@target_ontology_name = t('mappings.external_mappings')
elsif params[:target].start_with?(INTERPORTAL_MAPPINGS_GRAPH)
target_acronym = "#{INTERPORTAL_URL_PARAM_STR}:#{params[:target].split("/")[-1]}"
@target_ontology_name = "Interportal - #{params[:target].split("/")[-1].upcase}"
@target_ontology_name = t('mappings.interportal_mappings', params: params[:target].split("/")[-1].upcase)
end
else
target_acronym = @target_ontology.acronym
Expand Down Expand Up @@ -196,7 +196,7 @@ def create
@delete_mapping_permission = check_delete_mapping_permission(@mapping_saved)
mapping = LinkedData::Client::Models::Mapping.find(@mapping_saved.id)
render turbo_stream: [
alert(type: 'success') { 'Mapping created' },
alert(type: 'success') {t('mappings.mapping_created')},
prepend('concept_mappings_table_content', partial: 'show_line', locals: { map: mapping, concept: @concept })
]
end
Expand All @@ -220,7 +220,7 @@ def update
render_turbo_stream(alert_error { JSON.pretty_generate errors })
else
render_turbo_stream(
alert_success { 'Mapping updated' },
alert_success { t('mappings.mapping_updated') },
replace(@mapping.id.split('/').last, partial: 'show_line', locals: { map: request_mapping, concept: @concept })
)
end
Expand All @@ -236,7 +236,7 @@ def destroy
map_uri = "#{MAPPINGS_URL}/#{CGI.escape(map_id)}"
result = LinkedData::Client::HTTP.delete(map_uri)
if result.status == 204
success_text = "#{map_id} deleted successfully"
success_text = t('mappings.mapping_deleted', map_id: map_id)
else
error = result.body
end
Expand Down Expand Up @@ -336,14 +336,14 @@ def mapping_form_values

def request_mapping
mapping = LinkedData::Client::Models::Mapping.find(params[:id])
not_found("Mapping #{params[:id]} not found") if mapping.nil? || mapping.errors
not_found( t('mappings.mapping_not_found', id: params[:id]) ) if mapping.nil? || mapping.errors
mapping
end

def valid_values?(values)
errors = []
if values[:classes].reject(&:blank?).size != 2
errors << 'Source and target concepts need to be specified'
errors << t('mappings.error_of_source_and_target')
end
errors
end
Expand Down
13 changes: 7 additions & 6 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
class NotesController < ApplicationController
include TurboHelper
layout 'ontology'

NOTES_PROPOSAL_TYPES = {
ProposalNewClass: "New Class Proposal",
ProposalChangeHierarchy: "New Relationship Proposal",
ProposalChangeProperty: "Change Property Value Proposal"
ProposalNewClass: t('notes.new_class_proposal'),
ProposalChangeHierarchy: t('notes.new_relationship_proposal'),
ProposalChangeProperty: t('notes.change_property_value_proposal')
}

def show
Expand Down Expand Up @@ -100,7 +101,7 @@ def create
new_note = note.save
parent_type = params[:type].eql?("ontology") ? 'ontology' : 'class'
ontology_acronym = new_note.relatedOntology.first.split('/').last
success_message = 'New comment added successfully'
success_message = t('notes.new_comment_added')
locals = { note: new_note, ontology_acronym: ontology_acronym, parent_type: parent_type }
partial = 'notes/note_line'
container_id = "#{parent_type}_notes_table_body"
Expand Down Expand Up @@ -128,10 +129,10 @@ def destroy
if note.errors
response[:errors] = note.errors
else
response[:success] = "Note #{note_id} was deleted successfully"
response[:success] = t('notes.note_deleted_successfully', note_id: note_id)
end
else
response[:errors] = "Note #{note_id} was not found in the system"
response[:errors] = t('notes.note_not_found', note_id: note_id)
end
parent_type = params[:parent_type]
alerts_container_id = "notes_#{parent_type}_list_table_alerts"
Expand Down
8 changes: 4 additions & 4 deletions app/helpers/multi_languages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def request_lang
end

def portal_language_help_text
"Indicate the language in which the interfaces should appear"
t('language.portal_language_help_text')
end
def portal_languages
{
Expand Down Expand Up @@ -49,7 +49,7 @@ def portal_language_selector

def search_language_help_text
content_tag(:div, style: 'width: 300px; text-align: center') do
"Indicate the language on which to perform the search, restricting text matching exclusively to terms with that language"
t('language.search_language_help_text')
end
end
def search_languages
Expand Down Expand Up @@ -80,9 +80,9 @@ def content_languages(submission = @submission || @submission_latest)
end
def content_language_help_text
content_tag(:div, style: 'width: 350px;') do
concat content_tag(:div, "Indicate the language on which the content of this resource will be displayed.")
concat content_tag(:div, t('language.content_language_help_text_1'))
concat(content_tag(:div, class: "mt-1" ) do
content_tag(:span, "The available languages are specified by the resource admin.") + edit_sub_languages_button
content_tag(:span, t('language.content_language_help_text_2')) + edit_sub_languages_button
end)
end
end
Expand Down
36 changes: 35 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ en:

acceptance: Acceptance
additional_parameters: 'Additional parameters explained at '
language:
translation_not_available: "%{language} translation not available"
portal_language_help_text: Indicate the language in which the interfaces should appear
search_language_help_text: Indicate the language on which to perform the search, restricting text matching exclusively to terms with that language
content_language_help_text_1: Indicate the language on which the content of this resource will be displayed.
content_language_help_text_2: The available languages are specified by the resource admin.
collections:
error_valid_collection: "Error: You must provide a valid collection id"
concepts:
Expand Down Expand Up @@ -493,6 +499,16 @@ en:
visit_the_site: visit the full site

login:
invalid_account_combination: "Invalid account name/password combination"
authentication_failed: "%{provider} authentication failed"
admin_logged_out: Logged out <b>%{old_user}</b>, returned to <b>%{user}</b>
user_logged_out: You have successfully logged out
try_again_notice: ". Please try again."
reset_password_again: ". Please reset your password again."
custom_ontology_set: "The display is now based on your <a href='/account#custom_ontology_set'>Custom Ontology Set</a>."
welcome: "Welcome <b>"
error_account_name: Please enter an account name
error_password: Please enter a password
enter_email: Enter your username
enter_password: Enter your password
forgot_password: Forgot password?
Expand Down Expand Up @@ -522,6 +538,16 @@ en:
ontology_visits: Ontology visits

mappings:
external_mappings: "External Mappings (%{number_with_delimiter})"
interportal_mappings: "Interportal Mappings - %{acronym} (%{number_with_delimiter})"
test_bulk_load: This is the mappings produced to test the bulk load
external_mappings: External Mappings
interportal_mappings: Interportal - %{acronym} (%{number_with_delimiter})
mapping_created: Mapping created
mapping_updated: Mapping updated
mapping_deleted: "%{map_id} deleted successfully"
mapping_not_found: "Mapping %{id} not found"
error_of_source_and_target: Source and target concepts need to be specified
mapping_issue: "Mapping issue with '%{mapping}' : %{message}"
find_mappings: Find mappings of a class/concept
intro: Find all the mappings of an ontology
Expand Down Expand Up @@ -967,10 +993,13 @@ en:
hide_advanced_options: Hide advanced options
no_result_was_found: No result was found
notes:
comment: Comment
new_class_proposal: New Class Proposal
new_relationship_proposal: New Relationship Proposal
change_property_value_proposal: Change Property Value Proposal
new_comment_added: New comment added successfully
note_deleted_successfully: Note %{note_id} was deleted successfully
note_not_found: Note %{note_id} was not found in the system
comment: Comment
new_comment:
subject: Subject
comment: Comment
Expand Down Expand Up @@ -1065,6 +1094,11 @@ en:
bioportal_user: BioPortal User ID

annotator_plus:
query: "Query: %{query}"
retrieved_annotations: "Retrieved %{annotations} annotations: %{time}s"
completed_massage_for_annotated: "Completed massage for annotated classes: %{time}s"
failed_get_class_details: "Failed to get class details for: %{details}"
not_found_message: Not Found
title: Annotator Plus
annotator: Annotator &plus;
intro: Get annotations for text with ontology classes
Expand Down

0 comments on commit a88898c

Please sign in to comment.