Skip to content

Commit

Permalink
Merge pull request #768 from ontoportal-lirmm/feature/support-languag…
Browse files Browse the repository at this point in the history
…e-countries

Feature: Support countries as languages
  • Loading branch information
Bilelkihal authored Oct 7, 2024
2 parents 74e0c3e + db8e863 commit c5826d3
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ gem 'inline_svg'
# ISO language codes and flags
gem 'flag-icons-rails', '~> 3.4'
gem 'iso-639', '~> 0.3.6'
gem 'countries', '~> 5.7'

# Custom API client
gem 'ontologies_api_client', git: 'https://github.com/ontoportal-lirmm/ontologies_api_ruby_client.git', branch: 'master'
Expand Down Expand Up @@ -179,4 +180,4 @@ group :test do

# Testing framework for Rails
gem 'rspec-rails'
end
end
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ GEM
coderay (1.1.3)
color (1.8)
concurrent-ruby (1.3.4)
countries (5.7.2)
unaccent (~> 0.3)
crack (1.0.0)
bigdecimal
rexml
Expand Down Expand Up @@ -523,6 +525,7 @@ GEM
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unaccent (0.4.0)
unicode-display_width (2.5.0)
uri (0.13.1)
version_gem (1.1.4)
Expand Down Expand Up @@ -568,6 +571,7 @@ DEPENDENCIES
capybara
chart-js-rails
color (~> 1.8)
countries (~> 5.7)
dalli
debug
deepl-rb
Expand Down
11 changes: 4 additions & 7 deletions app/components/language_field_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@

class LanguageFieldComponent < ViewComponent::Base

include FlagIconsRails::Rails::ViewHelpers
include FlagIconsRails::Rails::ViewHelpers, MultiLanguagesHelper

def initialize(value:, label: nil, auto_label: false, icon: nil)
super
@value = value
@lang_code = nil
@label = label
@icon = icon

iso = ISO_639.find(value.to_s.split('/').last)
if iso
@lang_code = iso.alpha2
@label ||= iso.english_name if auto_label
end
@lang_code, label = find_language_code_name(value)
@label ||= label if auto_label
@lang_code = @lang_code.split('-').last if @lang_code
end

def lang_code
Expand Down
5 changes: 2 additions & 3 deletions app/helpers/collections_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module CollectionsHelper
def get_collections(ontology, add_colors: false)
collections = ontology.explore.collections(language: request_lang)
generate_collections_colors(collections) if add_colors
collections
collections.sort_by{ |x| helpers.main_language_label(x.prefLabel) }
end

def get_collection(ontology, collection_uri)
Expand Down Expand Up @@ -64,7 +64,7 @@ def link_to_collection(collection, selected_collection_id)
pref_label_lang, pref_label_html = get_collection_label(collection)
tooltip = pref_label_lang.to_s.eql?('@none') ? '' : "data-controller='tooltip' data-tooltip-position-value='right' title='#{pref_label_lang.upcase}'"
<<-EOS
<a id="#{collection['@id']}" href="#{collection_path(collection['@id'], request_lang)}"
<a id="#{collection['@id']}" href="#{collection_path(collection['@id'], request_lang)}"
data-turbo="true" data-turbo-frame="collection" data-collectionid="#{collection['@id']}"
#{tooltip}
class="#{selected_collection_id.eql?(collection['@id']) ? 'active' : nil}">
Expand All @@ -91,4 +91,3 @@ def generate_collections_colors(collections)
end
end
end

24 changes: 20 additions & 4 deletions app/helpers/multi_languages_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,31 @@ def content_languages(submission = @submission || @submission_latest)

# Transform each language into a select option
submission_lang = submission_lang.map do |lang|
lang = lang.split('/').last.upcase
lang = ISO_639.find(lang.to_s.downcase)
next nil unless lang
[lang.alpha2, lang.english_name]
code, name = find_language_code_name(lang)
next nil unless code
[code, name]
end.compact

[submission_lang, current_lang]
end

def find_language_code_name(language)
original_lang = language.to_s.split('/').last.upcase
lang, country = original_lang.split('-')

if country
lang = ISO3166::Country.find_country_by_alpha2(country)
return nil unless lang

[original_lang, lang.nationality]
else
lang = ISO_639.find(lang.to_s.downcase)
return nil unless lang

[lang.alpha2, lang.english_name]
end
end

def content_language_help_text
content_tag(:div, style: 'width: 350px;') do
concat content_tag(:div, t('language.content_language_help_text_1'))
Expand Down

0 comments on commit c5826d3

Please sign in to comment.