Skip to content

Commit

Permalink
Merge pull request #28607 from code-dot-org/fix-dsl-i18n-keys
Browse files Browse the repository at this point in the history
Fix dsl i18n keys
  • Loading branch information
Hamms committed Jun 13, 2019
2 parents 6b5e7f8 + 10cec1c commit 9dbaf37
Show file tree
Hide file tree
Showing 87 changed files with 83,867 additions and 29,539 deletions.
4 changes: 2 additions & 2 deletions dashboard/app/dsl/content_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ def i18n_strings
content3
pre_title
).each do |property|
strings[@hash[property]] = @hash[property] unless @hash[property].blank?
strings[property] = @hash[property] unless @hash[property].blank?
end
{@name => strings}
{@name => strings.stringify_keys}
end
end
7 changes: 2 additions & 5 deletions dashboard/app/dsl/contract_match_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ def answer(text) @hash[:answers] << text end

def i18n_strings
strings = super[@name]
strings['answers'] = @hash[:answers] unless @hash[:answers].empty?

@hash[:answers].each do |answer|
strings[answer] = answer
end

{@name => strings}
{@name => strings.deep_stringify_keys}
end
end
16 changes: 6 additions & 10 deletions dashboard/app/dsl/match_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@ def layout(text) @hash[:layout] = text end

def i18n_strings
strings = super[@name]
@hash[:questions].each do |question|
text = question[:text]
strings[text] = text
%i(
questions
answers
).each do |property|
strings[property] = @hash[property] unless @hash[property].blank?
end
@hash[:answers].each do |answer|
text = answer[:text]
strings[text] = text
feedback = answer[:feedback]
strings[feedback] = feedback if feedback.present?
end
{@name => strings}
{@name => strings.deep_stringify_keys}
end
end
8 changes: 3 additions & 5 deletions dashboard/app/dsl/text_match_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ def placeholder(text) @hash[:placeholder] = text end
def i18n_strings
strings = super[@name]

@hash[:answers].each do |answer|
strings[answer] = answer
end
strings[@hash[:placeholder]] = @hash[:placeholder] unless @hash[:placeholder].blank?
strings['answers'] = @hash[:answers] unless @hash[:answers].empty?
strings['placeolder'] = @hash[:placeholder] unless @hash[:placeholder].blank?

{@name => strings}
{@name => strings.deep_stringify_keys}
end
end
4 changes: 2 additions & 2 deletions dashboard/app/helpers/levels_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -654,15 +654,15 @@ def match_answer_as_iframe(path, width)
)
end

def render_multi_or_match_content(text, level = @level)
def render_multi_or_match_content(text)
return unless text

path, width = text.split(',')
return match_answer_as_image(path, width) if %w(.jpg .png .gif).include? File.extname(path)
return match_answer_as_embedded_blockly(path) if File.extname(path).ends_with? '_blocks'
return match_answer_as_iframe(path, width) if File.extname(path) == '.level'

level.localized_text(text)
text
end

def level_title
Expand Down
39 changes: 35 additions & 4 deletions dashboard/app/models/levels/dsl_defined.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,45 @@ def dsl_default
"Enter the level definition here.\n"
end

def localized_text(text)
I18n.t(
text,
def localized_property(property)
# We have to manually check for default here rather than just passing
# self.send(property) directly because some properties used here (like
# questions and answer for multi and match levels) expect to return an
# array of values, and if you pass an array as default to I18n.t it
# actually only returns the first element
localized = I18n.t(
property,
scope: ['data', type.underscore, name],
separator: I18n::Backend::Flatten::SEPARATOR_ESCAPE_CHAR,
default: text,
default: nil,
smart: true
)

source = try(property) || properties[property]
# When the result of I18n.t is a hash (or an array of hashes), they always
# have symbol keys regardless of the input format. We always want strings,
# so convert the value here.
self.class.resolve_partially_localized(localized, source)
end

# Localized content for DSL-Defined levels can be any combination of strings,
# arrays, or hashes. In every case, when we do not yet have a translation for
# a piece of content it comes back as nil. When that happens, we want to
# default back to the source (English) content; but we also want to make sure
# that we can deal with cases when _some_ of the content has been translated,
# but other parts return nil.
def self.resolve_partially_localized(localized, source)
if localized.blank?
source
elsif localized.is_a? Hash
source.deep_merge(localized).deep_stringify_keys
elsif localized.is_a? Array
localized.zip(source).map do |loc, src|
resolve_partially_localized(loc, src)
end
else
localized
end
end

def self.setup(data)
Expand Down
12 changes: 5 additions & 7 deletions dashboard/app/models/levels/match.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ def question_content_class

# Shuffle the answers until they are different from the original answers (if
# possible), but retain the original indexes for validation.
def shuffled_indexed_answers
indexed_answers = answers.each_with_index.map do |answer, i|
answer.merge({'index' => i})
end
return indexed_answers if indexed_answers.length <= 1 # avoid infinite loop
def shuffled_answer_indexes
answer_indexes = (0...answers.size).to_a
return answer_indexes if answer_indexes.length <= 1 # avoid infinite loop

shuffled_answers = indexed_answers.shuffle until shuffled_answers && shuffled_answers != indexed_answers
shuffled_answers
shuffled_indexes = answer_indexes.shuffle until shuffled_indexes && shuffled_indexes != answer_indexes
shuffled_indexes
end

def supports_markdown?
Expand Down
9 changes: 5 additions & 4 deletions dashboard/app/views/levels/_content.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
/ Set 'data' and 'app' to use this partial.
/ Note that content_class, source_level, hide_header, and postcontent are optional.
- app, data, content_class, source_level, hide_header, postcontent = %i(app data content_class source_level hide_header postcontent).map{ |x| local_assigns[x] }
- level = source_level || @level

- unless data['title'].blank? || hide_header
%h1!= render_multi_or_match_content(data['title'], source_level || @level) unless app == 'external'
%h1!= render_multi_or_match_content(level.localized_property('title')) unless app == 'external'
%div.content-level{class: (content_class unless content_class.blank?)}
- unless app == 'external'
- unless data['content1'].blank?
%p.content.content1!= render_multi_or_match_content(data['content1'], source_level || @level)
%p.content.content1!= render_multi_or_match_content(level.localized_property('content1'))
- unless data['content2'].blank?
%p.content.content2!= render_multi_or_match_content(data['content2'], source_level || @level)
%p.content.content2!= render_multi_or_match_content(level.localized_property('content2'))
- unless data['content3'].blank?
%p.content.content3!= render_multi_or_match_content(data['content3'], source_level || @level)
%p.content.content3!= render_multi_or_match_content(level.localized_property('content3'))
/ Markdown support using the 'markdown' property.
- if data['markdown'].present?
#markdown
Expand Down
8 changes: 4 additions & 4 deletions dashboard/app/views/levels/_match_answer.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
.content
.column
%ul.match_questions
- level.questions.each do |question|
%li{style: "height: #{level.height}px"}!= render_multi_or_match_content(question['text'], level)
- level.localized_property(:questions).each do |question|
%li{style: "height: #{level.height}px"}!= render_multi_or_match_content(question['text'])
.column.match_answerdest
%ul.match_answers
- level.answers.each do |answer|
%li.answer.answerlist{style: "height: #{level.height}px"}!= render_multi_or_match_content(answer['text'], level)
- level.localized_property(:answers).each do |answer|
%li.answer.answerlist{style: "height: #{level.height}px"}!= render_multi_or_match_content(answer['text'])
.clear
5 changes: 3 additions & 2 deletions dashboard/app/views/levels/_multi_answer.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
- correct_answers = data['answers'].each_with_index.map do |answer, index|
- next unless answer['correct']
- letter = standalone ? nil : Multi.value_to_letter(index)
- {text: answer['text'], index: index, letter: letter}
- {index: index, letter: letter}
- end.compact
- unless correct_answers.empty?
- localized_answers = level.localized_property(:answers)
#markdown.teacher.hide-as-student
%h3= t('teacher.answer')
.content
Expand All @@ -21,4 +22,4 @@
.item-answer-letter{style: "display: inline-block"}
= "#{correct_answer[:letter]}."
&nbsp;
!= render_multi_or_match_content(correct_answer[:text], level)
!= render_multi_or_match_content(localized_answers[correct_answer[:index]]['text'])
9 changes: 5 additions & 4 deletions dashboard/app/views/levels/_single_match.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
.mainblock
.column
%ul.match_questions
- level.questions.each do |question|
%li{style: "height: #{level.height}px"}!= render_multi_or_match_content(question['text'], level)
- level.localized_property(:questions).each do |question|
%li{style: "height: #{level.height}px"}!= render_multi_or_match_content(question['text'])
.column.match_answerdest
%ul.match_slots.draggablecolumn
- level.answers.each do |_|
Expand All @@ -36,8 +36,9 @@

.column
%ul.match_answers.draggablecolumn
- level.shuffled_indexed_answers.each do |answer|
%li.answer.answerlist{style: "height: #{level.height}px", originalIndex: answer['index']}!= render_multi_or_match_content(answer['text'], level)
- localized_answers = level.localized_property(:answers)
- level.shuffled_answer_indexes.each do |index|
%li.answer.answerlist{style: "height: #{level.height}px", originalIndex: index}!= render_multi_or_match_content(localized_answers[index]['text'])

.clear
- if standalone
Expand Down
9 changes: 5 additions & 4 deletions dashboard/app/views/levels/_single_multi.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

= render partial: 'levels/content', locals: {app: app, data: data, content_class: question_content_class, source_level: level, hide_header: !standalone}

- if data['questions']
- question_text = render_multi_or_match_content(data['questions'][0]['text'], level)
.multi-question!= question_text
- localized_questions = level.localized_property(:questions)
- if localized_questions.count
.multi-question!= render_multi_or_match_content(localized_questions[0]["text"])

- answers_class = 'answers ' + (question_content_blank ? 'question-content-blank' : '')

Expand Down Expand Up @@ -62,6 +62,7 @@

.mainblock
%form#voteform{onsubmit: 'return false;'}
- localized_answers = level.localized_property(:answers)
- data['answers'].each_with_index do |answer, i|
-# When LevelGroup-hosted, with students (or teachers doing PD), set all answers false before rendering page.
- correct = include_multi_answers?(standalone) ? answer['correct'] : false
Expand All @@ -78,7 +79,7 @@
="#{multi_answer_characters[i]}. "
&nbsp;
%label.item-label{style: "height: #{height}"}
!= render_multi_or_match_content(answer['text'], level)
!= render_multi_or_match_content(localized_answers[i]['text'])
- if use_tight_layout && !force_wrap_layout
%br/

Expand Down
2 changes: 1 addition & 1 deletion dashboard/app/views/levels/_single_text_match.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
.mainblock
= render partial: 'levels/content', locals: {app: app, data: data, source_level: level, hide_header: !standalone}
- height = data['height'] || '80'
%textarea.response{placeholder: data['placeholder'] ? level.localized_text(data['placeholder']) : t(app + '.placeholder'), style: "width: 700px; height: #{height}px;", readonly: @view_options.readonly_workspace}
%textarea.response{placeholder: data['placeholder'] ? level.localized_property(:placeholder) : t(app + '.placeholder'), style: "width: 700px; height: #{height}px;", readonly: @view_options.readonly_workspace}

- if standalone
= render partial: 'levels/dialog', locals: {app: app}
Expand Down

0 comments on commit 9dbaf37

Please sign in to comment.