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

Translate CodeHarbor to German #1143

Merged
merged 8 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 app/assets/javascripts/flash_message.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ show_ajax_message = (msg, type) ->
$("#flash-#{type}").delay(6000).slideUp 'medium'

$(document).ajaxComplete (event, request) ->
msg = request.getResponseHeader("X-Message")
msg = decodeURI(request.getResponseHeader("X-Message"))
type = request.getResponseHeader("X-Message-Type")
show_ajax_message msg, type unless type == 'empty' #use whatever popup, notification or whatever plugin you want

Expand Down
10 changes: 6 additions & 4 deletions app/assets/javascripts/labels.coffee.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
max_label_length = <%= Label::MAX_LENGTH %>

verify_label_name = (label_name) ->
if label_name == ''
return I18n.t('labels.javascripts.cannot_be_empty');
if label_name.length > max_label_length
return I18n.t('labels.javascripts.too_long');
return 'OK';
return I18n.t('labels.javascripts.ok');

root = exports ? this;
root.verify_label_name = verify_label_name;
Expand All @@ -14,6 +15,7 @@ ready = ->
default_label_font_color = "000000"

$('.labels-select2-tag').select2
language: I18n.locale
width: '100%'
tags: true
multiple: true
Expand Down Expand Up @@ -74,7 +76,7 @@ ready = ->
term = $.trim(params.term)
selection = $('.labels-select2-tag').select2('data').map (element) -> element.id

if verify_label_name(term) != 'OK' || selection.includes(term)
if verify_label_name(term) != I18n.t('labels.javascripts.ok') || selection.includes(term)
return null;

return {
Expand All @@ -84,7 +86,7 @@ ready = ->
}

formatSelectionTooBig: (limit) ->
I18n.t('labels.javascripts.can_only_create_5')
I18n.t('labels.javascripts.max_limit_reached', {limit: limit})

$('.labels-select2-tag').on 'select2:select', clear_input
return
Expand All @@ -93,4 +95,4 @@ clear_input = ->
$('.labels-select2-tag').siblings(".select2").find("textarea").val("");
return

$(document).on 'turbolinks:load', ready
$(document).on('select2:locales:loaded', ready)
4 changes: 2 additions & 2 deletions app/assets/javascripts/tasks_export.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exportTaskStart = (taskID) ->
$messageDiv.html(response.message)
$actionsDiv.html(response.actions)
error: (_xhr, _textStatus, message) ->
alert('error:' + message);
alert("#{I18n.t('common.javascripts.error')}: #{message}");
})

exportConfirm = (taskId, accountLinkId, pushType) ->
Expand All @@ -52,7 +52,7 @@ exportConfirm = (taskId, accountLinkId, pushType) ->
else
$taskDiv.addClass 'import-export-failure'
error: (_xhr, _textStatus, message) ->
alert('error:' + message)
alert("#{I18n.t('common.javascripts.error')}: #{message}")
})

root = exports ? this;
Expand Down
11 changes: 7 additions & 4 deletions app/assets/javascripts/tasks_form.coffee
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
ready =->
initializeLoadSelect2()
ready = ->
initializeFileTypeSelection()
initializeVisibilityWarning()

$(document).on('turbolinks:load', ready)

initializeLoadSelect2 = ->
$('#task_programming_language_id').select2
language: I18n.locale
tags: false
width: '100%'
multiple: false

$('.my-group').select2
language: I18n.locale
width: '100%'
multiple: true
closeOnSelect: false
Expand All @@ -30,3 +29,7 @@ initializeVisibilityWarning = ->
warning_message.removeClass('d-none')
$('#task_access_level_public').on 'change', ->
warning_message.addClass('d-none')


$(document).on('turbolinks:load', ready)
$(document).on('select2:locales:loaded', initializeLoadSelect2)
8 changes: 4 additions & 4 deletions app/assets/javascripts/tasks_import.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ importStart = () ->
success: (response) ->
if response.status == 'failure'
alert(response.message)
error: (a, b, c) ->
alert('error: ' + c)
error: (_xhr, _textStatus, message) ->
alert("#{I18n.t('common.javascripts.error')}: #{message}")
})

importConfirm = (importId, subfileId, importType) ->
Expand All @@ -61,6 +61,6 @@ importConfirm = (importId, subfileId, importType) ->
$taskDiv.addClass 'import-export-success'
else
$taskDiv.addClass 'import-export-failure'
error: (a, b, c) ->
alert('error: ' + c)
error: (_xhr, _textStatus, message) ->
alert("#{I18n.t('common.javascripts.error')}: #{message}")
})
14 changes: 6 additions & 8 deletions app/assets/javascripts/tasks_index.coffee
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
ready = ->
initializeSelect2()
initCollapsable($('.description'), '95px')
window.addEventListener 'resize', -> initCollapsable($('.description'), '95px')
initializeDynamicHideShow()
initializeFilter()
initializeIndexComments()
initializeInputFieldEnterCallback()

$(document).on('turbolinks:load', ready)

initializeDynamicHideShow = ->
$('body').on 'click', '.more-btn-wrapper', (event) ->
Expand All @@ -16,10 +14,12 @@ initializeDynamicHideShow = ->

initializeSelect2 = ->
$('.defaultSelect2').select2
language: I18n.locale
minimumResultsForSearch: 10
width: '100%'

$('.language-box').select2
language: I18n.locale
width: '100%'
multiple: true
closeOnSelect: false
Expand Down Expand Up @@ -101,14 +101,8 @@ intializeAdvancedFilter = ->
$drop.removeClass('fa-caret-down').addClass('fa-caret-up')

if $('#order_param')
# $('#' + order.value).addClass('active')
$('#order_created').addClass('active')

# $('#order_rating').click ->
# $('#order_rating').addClass('active')
# $('#order_created').removeClass('active')
# document.getElementById('order_param').value = 'order_rating'

$('#order_created').click ->
$('#order_created').addClass('active')
$('#order_rating').removeClass('active')
Expand Down Expand Up @@ -142,3 +136,7 @@ initializeInputFieldEnterCallback = ->
return
$('.search-submit-button-tag').click();
event.preventDefault();


$(document).on('turbolinks:load', ready)
$(document).on('select2:locales:loaded', initializeSelect2)
4 changes: 2 additions & 2 deletions app/assets/javascripts/tasks_ratings.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ initializeRatings = ->
$('.overall-rating[data-rating='+num+']').removeClass("fa-star").removeClass("fa-solid").addClass("fa-star-half-stroke")
else
$('.overall-rating[data-rating='+num+']').removeClass("fa-star-half-stroke").removeClass("fa-solid").addClass("fa-star").addClass("fa-regular")
error: (a, b, c) ->
alert("error:" + c);
error: (_xhr, _textStatus, message) ->
alert("#{I18n.t('common.javascripts.error')}: #{message}");
})
33 changes: 29 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def require_user!
def flash_to_headers
return unless request.xhr?

response.headers['X-Message'] = flash_message
response.headers['X-Message'] = ERB::Util.url_encode(flash_message)
response.headers['X-Message-Type'] = flash_type.to_s

flash.discard # don't want the flash to appear when you reload page
Expand Down Expand Up @@ -97,10 +97,35 @@ def mnemosyne_trace
end
end

def switch_locale(&) # rubocop:disable Metrics/AbcSize
session[:locale] = sanitize_locale(params[:custom_locale] || params[:locale] || session[:locale])
locale = session[:locale] || http_accept_language.compatible_language_from(I18n.available_locales) || I18n.default_locale
def sanitized_locale_param
sanitize_locale(params[:locale])
end

def sanitized_session_locale
sanitize_locale(session[:locale])
end

def sanitized_user_preferred_locale
return if current_user.nil?

sanitize_locale(current_user.preferred_locale)
end

def choose_locale
sanitized_locale_param ||
sanitized_session_locale ||
sanitized_user_preferred_locale ||
http_accept_language.compatible_language_from(I18n.available_locales) ||
I18n.default_locale
end

def switch_locale(&)
locale = choose_locale
session[:locale] = locale
Sentry.set_extras(locale:)
if current_user.present? && locale != sanitized_user_preferred_locale
current_user.update(preferred_locale: locale)
end
I18n.with_locale(locale, &)
end

Expand Down
3 changes: 1 addition & 2 deletions app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ def leave

def share_message
user = User.find_by(email: params[:user])
text = t('collections.share_message.text', user: current_user.name, collection: @collection.title)
Message.new(sender: current_user, recipient: user, param_type: 'collection', param_id: @collection.id, text:)
Message.new(sender: current_user, recipient: user, param_type: 'collection', param_id: @collection.id)
end

def push_exercises
Expand Down
37 changes: 10 additions & 27 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def leave
def request_access
flash[:notice] = t('.success_notice')
@group.admins.each do |admin|
send_access_request_message(admin, @group)
send_message(admin, :group_requested)

AccessRequestMailer.send_access_request(current_user, admin, @group).deliver_now
end
Expand All @@ -77,9 +77,9 @@ def remove_task

def grant_access
@group.grant_access(@user)
send_grant_access_messages(@user, @group)
send_message(@user, :group_accepted)

Message.where(sender: @user, recipient: current_user, param_type: 'group', param_id: @group.id).destroy_all
Message.where(sender: @user, recipient: current_user, param_type: :group_requested, param_id: @group.id).destroy_all
redirect_to @group, notice: t('.success_notice')
end

Expand All @@ -90,9 +90,9 @@ def delete_from_group

def deny_access
@group.users.delete(@user)
send_deny_access_message(@user, @group)
send_message(@user, :group_declined)

Message.where(sender: @user, recipient: current_user, param_type: 'group', param_id: @group.id).destroy_all
Message.where(sender: @user, recipient: current_user, param_type: :group_requested, param_id: @group.id).destroy_all
redirect_to @group, notice: t('.success_notice')
end

Expand All @@ -108,29 +108,12 @@ def demote_admin

private

def send_access_request_message(admin, group)
# type is one of [:group_requested, :group_accepted, :group_declined]
def send_message(recipient, type)
Message.create(sender: current_user,
recipient: admin,
text: t('groups.send_access_request_message.message', user: current_user.name, group: group.name),
param_type: 'group',
param_id: group.id,
sender_status: 'd')
end

def send_deny_access_message(user, group)
Message.create(sender: current_user,
recipient: user,
text: t('groups.send_deny_access_message.message', user: current_user.name, group: group.name),
param_type: 'group_declined',
sender_status: 'd')
end

def send_grant_access_messages(user, group)
Message.create(sender: current_user,
recipient: user,
text: t('groups.send_grant_access_messages.message', user: current_user.name, group: group.name),
param_type: 'group_accepted',
param_id: group.id,
recipient:,
param_type: type,
param_id: @group.id,
sender_status: 'd')
end

Expand Down
21 changes: 21 additions & 0 deletions app/helpers/authenticated_url_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module AuthenticatedUrlHelper
class << self
def add_query_parameters(url, parameters)
parsed_url = URI.parse url

# Add the given parameters to the query string
query_params = CGI.parse(parsed_url.query || '').with_indifferent_access
query_params.merge!(parameters)

# Add the query string back to the URL
parsed_url.query = URI.encode_www_form(query_params)

# Return the full URL
parsed_url.to_s
rescue URI::InvalidURIError
url
end
end
end
6 changes: 6 additions & 0 deletions app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ import 'jquery-ui/themes/base/sortable.css'
import { I18n } from "i18n-js";
import locales from "../../tmp/locales.json";

Promise.all(
Object.keys(locales).map(locale => import(`select2/dist/js/i18n/${locale}`))
).then(() =>
document.dispatchEvent(new Event("select2:locales:loaded"))
);

// Fetch user locale from html#lang.
// This value is being set on `app/views/layouts/application.html.slim` and
// is inferred from `ACCEPT-LANGUAGE` header.
Expand Down
8 changes: 6 additions & 2 deletions app/mailers/access_request_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ def send_access_request(user, admin, group)
@user = user
@admin = admin
@group = group
mail(to: @admin.email, subject: "#{user.name} wants to access your Group '#{group.name}'")
I18n.with_locale(@admin.preferred_locale || I18n.default_locale) do
mail(to: @admin.email, subject: t('groups.access_request_mailer.subject', user: @user.name, group: @group.name))
end
end

def send_contribution_request(author, exercise, user)
@author = author
@exercise = exercise
@user = user
mail(to: @author.email, subject: "#{user.name} wants to contribute to your Exercise '#{exercise.title}'")
I18n.with_locale(@author.preferred_locale || I18n.default_locale) do
mail(to: @author.email, subject: t('tasks.access_request_mailer.subject', user: @user.name, task: @exercise.title))
end
end
end
17 changes: 16 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Message < ApplicationRecord
validates :text, presence: true
validates :text, presence: true, unless: -> { %w[group_requested group_accepted group_declined collection].include?(param_type) }

belongs_to :sender, class_name: 'User', inverse_of: :sent_messages
belongs_to :recipient, class_name: 'User', inverse_of: :received_messages
Expand All @@ -16,6 +16,21 @@ def mark_as_deleted(user)
self.recipient_status = 'd' if recipient == user
end

def text # rubocop:disable Metrics/AbcSize
case param_type
when 'group_requested'
MrSerth marked this conversation as resolved.
Show resolved Hide resolved
I18n.t('groups.send_access_request_message.message', user: sender.name, group: Group.find(param_id).name)
when 'group_accepted'
I18n.t('groups.send_grant_access_messages.message', user: sender.name, group: Group.find(param_id).name)
when 'group_declined'
I18n.t('groups.send_deny_access_message.message', user: sender.name, group: Group.find(param_id).name)
when 'collection'
I18n.t('collections.share_message.text', user: sender.name, collection: Collection.find(param_id).title)
else
super
end
end

def self.parent_resource
User
end
Expand Down
Loading
Loading