Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n for some of the devise related pages #1059

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/tmp
config/secrets.yml
config/sunspot.yml
config/tess.yml
# config/tess.yml
config/ingestion.yml
tmp/
solr/test/
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ end

group :development, :test do
gem 'byebug'
gem 'pry-rails'
gem 'pry-byebug'
gem 'rubocop'
gem 'simplecov'
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ GEM
pry-byebug (3.10.1)
byebug (~> 11.0)
pry (>= 0.13, < 0.15)
pry-rails (0.3.9)
pry (>= 0.10.4)
psych (5.1.2)
stringio
public_activity (2.0.2)
Expand Down Expand Up @@ -817,6 +819,7 @@ DEPENDENCIES
pg
private_address_check
pry-byebug
pry-rails
public_activity
puma
pundit
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1008,4 +1008,10 @@ td.day .calendar-text {
.source-log {
max-height: 20pc;
overflow-y: scroll;
}
}
span.translation_missing {
background-color: #ffcccc;
}
span.translation_present {
background-color: #ccffcc;
}
15 changes: 15 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class ApplicationController < ActionController::Base
include PublicActivity::StoreController

before_action :configure_permitted_parameters, if: :devise_controller?
before_action :locale_from_params_or_session

# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
Expand Down Expand Up @@ -142,4 +143,18 @@ def configure_permitted_parameters
def allow_embedding
response.headers.delete 'X-Frame-Options'
end

def locale_from_params_or_session
loc_ids = Rails.application.config.i18n.available_locales.map(&:to_s)
loc = session['locale']
I18n.locale = if params[:locale] && loc_ids.include?(params[:locale])
params[:locale].to_sym
elsif !loc.blank? && loc_ids.include?(loc.to_s)
loc.to_sym
else
Rails.application.config.i18n.default_locale
end
session['locale'] = I18n.locale.to_s
end

end
25 changes: 25 additions & 0 deletions app/controllers/localization_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class LocalizationController < ApplicationController

skip_before_action :authenticate_user!, :authenticate_user_from_token!

def change_locale
# Get locale from params into session
# See also ApplicationController#locale_from_params_or_session
if params[:locale]
session[:locale] = params[:locale]
else
if session[:locale].to_s == 'en'
session[:locale] = 'fr'
else
session[:locale] = 'en'
end
end

if params[:back_to]
redirect_to params[:back_to]

Check warning

Code scanning / CodeQL

URL redirection from remote source Medium

Untrusted URL redirection depends on a
user-provided value
.
else
render body: ''
end
end

end
32 changes: 32 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,38 @@ def archived_notice(resource)
class: 'alert alert-warning mb-4 archived-notice')
end

def language_menu_item
# Luckily we are supporting bilingual en/fr, so only toggle is needed
# Show locale for the one we aren't currently using ...
if I18n.locale == :fr
switch = :en
text = 'English'
else
switch = :fr
text = 'Français'
end
# Add the font awesome exchange icon to the text
text = (icon('exchange', class: 'pr-3') + text).html_safe

self_url_params = { controller: controller_name, action: action_name, id: params[:id] }
url = url_for controller: '/localization',
action: :change_locale,
locale: switch,
back_to: url_for(self_url_params)
menu_item text, url, class: "ml-3"
end

if ENV["HIGHLIGHT_TRANSLATIONS"]
def t(...)
# Note: missing translation will be a span contained in this one
# (So the CSS of that span will get precedence).
super do |translation, resolved_key|
content_tag(:span, translation, title: resolved_key,
class: "translation_present")
end
end
end

def unverified_notice(resource)
content_tag('div', t('warnings.unverified', resource_type: resource.model_name.human.downcase),
class: 'alert alert-warning mb-4 unverified-notice')
Expand Down
49 changes: 49 additions & 0 deletions app/models/content_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,55 @@ def self.identifiers_dot_org_key
'p'
end

def self.logo_directory
File.join(File.path(Rails.root), 'config', 'data', 'logos')
end

def self.find_logo_file_name(slug)
if File.exist?(File.join(logo_directory, "#{slug}.jpg"))
return "#{slug}.jpg"
elsif File.exist?(File.join(logo_directory, "#{slug}.png"))
return "#{slug}.png"
end
nil
end

def self.load_from_array(array)
owner = User.get_default_user
array.each do |provider|
slug = provider["slug"]

sources = provider['sources']
provider.delete('sources')

content_provider = ContentProvider.find_by(slug: slug)
next if content_provider
content_provider = ContentProvider.new(provider)
content_provider.user = owner
content_provider.save!

image_file_name = find_logo_file_name(slug)
if image_file_name
content_provider.image_file_name = image_file_name
File.open(File.join(logo_directory, image_file_name)) do |f|
content_provider.image = f
content_provider.save!
end
end

if sources
sources.each do |source|
Source.create!(content_provider: content_provider,
url: source['url'],
method: source['method'],
enabled: true,
approval_status: :approved,
user: owner)
end
end
end
end

def add_editor(editor)
if !editor.nil? and !editors.include?(editor) and !user.nil? and user.id != editor.id
editors << editor
Expand Down
4 changes: 3 additions & 1 deletion app/views/devise/sessions/_omniauth_options.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<% if config.options[:logo] %>
<%= image_tag(config.options[:logo], class: "omniauth-logo omniauth-#{provider}") -%>
<% else %>
<%= "Log in with #{t("authentication.omniauth.providers.#{provider}", default: provider.to_s.titleize)}" -%>
<%= t('authentication.omniauth.log_in_with',
provider: t("authentication.omniauth.providers.#{provider}",
default: provider.to_s.titleize)) -%>
<% end %>
<% end -%>
<% end -%>
Expand Down
9 changes: 5 additions & 4 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
<div class="h4"><%= t('authentication.password.title') %></div>
<p><%= t('authentication.password.description') %></p>
<%= f.input :login, label: 'Email or username', autofocus: true %>
<%= f.input :password, input_html: { autocomplete: 'off' } %>
<%= f.input :login, label: t('authentication.password.email_or_username'), autofocus: true %>
<%= f.input :password, label: t('authentication.password.password'),
input_html: { autocomplete: 'off' } %>
<% if devise_mapping.rememberable? -%>
<%= f.input :remember_me, as: :boolean %>
<%= f.input :remember_me, label: t('authentication.password.remember_me'), as: :boolean %>
<% end -%>
<div class="actions">
<%= f.submit "Log in", :class => 'btn btn-primary' %>
<%= f.submit t('authentication.password.log_in'), :class => 'btn btn-primary' %>
</div>
<% end %>
<%= render "devise/shared/links" %>
Expand Down
15 changes: 10 additions & 5 deletions app/views/devise/shared/_links.html.erb
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
<ul class="shared-links">
<%- if controller_name != 'sessions' %>
<li>Already on <%= TeSS::Config.site['title_short'] %>? <%= link_to 'Log in', new_session_path(resource_name) %></li>
<li><%= t('devise.links.already_on', title: TeSS::Config.site['title_short']) %>
<%= link_to t('devise.links.log_in'), new_session_path(resource_name) %></li>
<% end -%>

<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
<li>New to <%= TeSS::Config.site['title_short'] %>? <%= link_to 'Register', new_registration_path(resource_name) %></li>
<li><%= t('devise.links.new_to', title: TeSS::Config.site['title_short']) %>
<%= link_to t('devise.links.register'), new_registration_path(resource_name) %></li>
<% end -%>

<%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %>
<li>Forgot your password? <%= link_to 'Request a reset', new_password_path(resource_name) %></li>
<li><%= t('devise.links.forgot_your_password') %>
<%= link_to t('devise.links.request_a_reset'), new_password_path(resource_name) %></li>
<% end -%>

<%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
<li>Didn’t receive confirmation instructions? <%= link_to 'Resend', new_confirmation_path(resource_name) %></li>
<li><%= t('devise.links.didnt_receive_confirmation_instructions') %>
<%= link_to t('devise.links.resend'), new_confirmation_path(resource_name) %></li>
<% end -%>

<%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
<li>Didn't receive unlock instructions? <%= link_to 'Resend', new_unlock_path(resource_name) %></li>
<li><%= t('devise.links.didnt_receive_unlock_instructions') %>
<%= link_to t('devise.links.resend'), new_unlock_path(resource_name) %></li>
<% end -%>
</ul>
4 changes: 4 additions & 0 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
directory_tabs, main_tabs = tabs.partition { |t| TeSS::Config.site['directory_tabs']&.include?(t[:feature]) }
%>

<%= menu_group(pull: 'right') do %>
<%= language_menu_item %>
<% end %>

<%= menu_group(pull: 'right') do %>
<% main_tabs.each do |t| %>
<%= menu_item t("features.#{t[:feature]}.short"), t[:link] %>
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Application < Rails::Application

# locales
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', 'overrides', '**', '*.{rb,yml}')] unless Rails.env.test?
config.i18n.available_locales = [:en]
config.i18n.available_locales = [:en, :fr]
config.i18n.default_locale = :en

config.active_record.yaml_column_permitted_classes = [
Expand Down
Loading
Loading