Skip to content

Commit

Permalink
:default locale mandatory w/ translations NUBIC#426
Browse files Browse the repository at this point in the history
Default locales are specified by e.g.

"translation :en => :default"

multiple default locales can be specified, they will all point to the
survey text.
  • Loading branch information
flockom committed Apr 2, 2013
1 parent 5c4df15 commit d2ea2f8
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 42 deletions.
10 changes: 9 additions & 1 deletion features/internationalization.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Feature: Internationalization
Given I parse
"""
survey "One language is never enough" do
translations :en => :default
translations :es => {"title"=>"Un idioma nunca es suficiente", "survey_sections"=>{"one"=>{"title"=>"Uno"}}, "question_groups"=>{"hello"=>{"text"=>"¡Hola!"}}, "questions"=>{"name"=>{"text"=>"¿Cómo se llama Usted?", "answers"=>{"name"=>{"help_text"=>"Mi nombre es..."}}}}}
translations :he => {"title"=>"ידיעת שפה אחת אינה מספיקה", "survey_sections"=>{"one"=>{"title"=>"אחת"}}, "question_groups"=>{"hello"=>{"text"=>"שלום"}}, "questions"=>{"name"=>{"text"=>"מה שמך?", "answers"=>{"name"=>{"help_text"=>"שמי..."}}}}}
translations :ko => {"title"=>"한가지 언어로는 충분치 않습니다.", "survey_sections"=>{"one"=>{"title"=>"하나"}}, "question_groups"=>{"hello"=>{"text"=>"안녕하십니까"}}, "questions"=>{"name"=>{"text"=>"성함이 어떻게 되십니까?", "answers"=>{"name"=>{"help_text"=>"제 이름은 ... 입니다"}}}}}
Expand All @@ -19,12 +20,19 @@ Feature: Internationalization
end
end
"""
Then there should be 3 translations with
Then there should be 4 translations with
| locale |
| en |
| es |
| he |
| ko |
When I start the survey
Then I should see "One language is never enough"
And I should see "One"
And I should see "Hello"
And I should see "What is your name?"
And I should see "My name is..."
When I start the survey in "en"
Then I should see "One language is never enough"
And I should see "One"
And I should see "Hello"
Expand Down
12 changes: 10 additions & 2 deletions lib/surveyor/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ def method_missing(missing_method, *args, &block)
resolve_dependency_condition_references
resolve_question_correct_answers
report_lost_and_duplicate_references
report_missing_default_locale
Surveyor::Parser.rake_trace("", -2)
if context[:survey].save
Surveyor::Parser.rake_trace "Survey saved."
Expand Down Expand Up @@ -122,6 +123,11 @@ def full(method_name)
def block_models
%w(survey survey_section question_group)
end
def report_missing_default_locale
if !self.context[:survey].translations.empty? && self.context[:survey].translations.select{|t|YAML::load(t.translation)=={}}.empty?
Surveyor::Parser.raise_error("No default locale specified for translations.",true)
end
end
def report_lost_and_duplicate_references
Surveyor::Parser.raise_error("Bad references: #{self.context[:bad_references].join("; ")}", true) unless self.context[:bad_references].empty?
Surveyor::Parser.raise_error("Duplicate references: #{self.context[:duplicate_references].join("; ")}", true) unless self.context[:duplicate_references].empty?
Expand Down Expand Up @@ -188,9 +194,11 @@ def parse_and_build(context, args, original_method, reference_identifier)
args[0].each do |k,v|
case v
when Hash
trans = YAML::dump(v)
trans = YAML::dump(v)
when String
trans = File.read(File.join(dir,v))
trans = File.read(File.join(dir,v))
when :default
trans = YAML::dump({})
end
context[:survey].translations << self.class.new(:locale => k.to_s, :translation => trans)
end
Expand Down
133 changes: 94 additions & 39 deletions spec/lib/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,65 @@
end


context 'when a translation is specified as a Hash' do
it 'should should treat the hash as an inline translation' do
describe 'translations' do

it 'should produce the survey text for :default locale' do
survey_text = <<END
survey "One language is never enough" do
translations :en => :default, :es =>{'questions' => {'name' =>{ 'text' => '¡Hola!'}}}
section_one "One" do
label_name "Hello!"
end
end
END
survey = Surveyor::Parser.new.parse(survey_text)
survey.is_a?(Survey).should == true
survey.translations.size.should == 2
question = survey.sections.first.questions.first
question.translation(:en)[:text].should == "Hello!"
question.translation(:es)[:text].should == "¡Hola!"
end


it 'should raise an error w/o :default locale' do
survey_text = <<END
survey "One language is never enough" do
translations :es =>{'questions' => {'name' =>{ 'text' => '¡Hola!'}}}
section_one "One" do
label_name "Hello!"
end
end
END
s = Survey.all.size
expect {survey = Surveyor::Parser.new.parse(survey_text)}.to raise_error
Survey.all.size.should == s
end


it 'should allow multiple default locales' do
survey_text = <<END
survey "Just don't talk about tyres" do
translations :'en-US' => :default, :'en-GB' => :default, :es =>{'questions' => {'name' =>{'text' => '¡Hola!'}}}
section_one "One" do
label_name "Hello!"
end
end
END

survey = Surveyor::Parser.new.parse(survey_text)
survey.is_a?(Survey).should == true
survey.translations.size.should == 3
question = survey.sections.first.questions.first
question.translation(:'en-US')[:text].should == "Hello!"
question.translation(:'en-GB')[:text].should == "Hello!"
question.translation(:es)[:text].should == "¡Hola!"
end

context 'when a translation is specified as a Hash' do
it 'should should treat the hash as an inline translation' do
survey_text = <<END
survey "One language is never enough" do
translations :es => {"title"=>"Un idioma nunca es suficiente", "survey_sections"=>{"one"=>{"title"=>"Uno"}}, "question_groups"=>{"hello"=>{"text"=>"¡Hola!"}}, "questions"=>{"name"=>{"text"=>"¿Cómo se llama Usted?", "answers"=>{"name"=>{"help_text"=>"Mi nombre es..."}}}}}
translations :en => :default, :es => {"title"=>"Un idioma nunca es suficiente", "survey_sections"=>{"one"=>{"title"=>"Uno"}}, "question_groups"=>{"hello"=>{"text"=>"¡Hola!"}}, "questions"=>{"name"=>{"text"=>"¿Cómo se llama Usted?", "answers"=>{"name"=>{"help_text"=>"Mi nombre es..."}}}}}
section_one "One" do
g_hello "Hello" do
q_name "What is your name?"
Expand All @@ -46,26 +100,26 @@
end
end
END
survey = Surveyor::Parser.new.parse(survey_text)
survey.is_a?(Survey).should == true
survey.translations.size.should == 1
survey.translation(:es)['title'].should == "Un idioma nunca es suficiente"
survey = Surveyor::Parser.new.parse(survey_text)
survey.is_a?(Survey).should == true
survey.translations.size.should == 2
survey.translation(:es)['title'].should == "Un idioma nunca es suficiente"
end
end
end

context 'when a translation is specified as a String' do
context 'when a translation is specified as a String' do

context 'when the survey filename is not given' do
it 'should look for the translation file relative to pwd' do
Dir.mktmpdir do |dir|
FileUtils.cd(dir) do
translation_yaml = YAML::dump({'title' => 'Un idioma nunca es suficiente'})
translation_temp_file = Tempfile.new('parser_spec_translation.yml',dir)
translation_temp_file.write(translation_yaml)
translation_temp_file.flush
survey_text = <<END
context 'when the survey filename is not given' do
it 'should look for the translation file relative to pwd' do
Dir.mktmpdir do |dir|
FileUtils.cd(dir) do
translation_yaml = YAML::dump({'title' => 'Un idioma nunca es suficiente'})
translation_temp_file = Tempfile.new('parser_spec_translation.yml',dir)
translation_temp_file.write(translation_yaml)
translation_temp_file.flush
survey_text = <<END
survey "One language is never enough" do
translations :es =>'#{File.basename(translation_temp_file.path)}'
translations :es =>'#{File.basename(translation_temp_file.path)}', :en => :default
section_one "One" do
g_hello "Hello" do
q_name "What is your name?"
Expand All @@ -74,27 +128,27 @@
end
end
END
survey_temp_file = Tempfile.new('parser_spec_survey.rb',dir)
survey_temp_file.write(survey_text)
survey_temp_file.flush
Surveyor::Parser.parse(File.read(survey_temp_file.path))
survey = Survey.where(:title=>'One language is never enough').first
survey.nil?.should == false
survey.translation(:es)['title'].should == "Un idioma nunca es suficiente"
survey_temp_file = Tempfile.new('parser_spec_survey.rb',dir)
survey_temp_file.write(survey_text)
survey_temp_file.flush
Surveyor::Parser.parse(File.read(survey_temp_file.path))
survey = Survey.where(:title=>'One language is never enough').first
survey.nil?.should == false
survey.translation(:es)['title'].should == "Un idioma nunca es suficiente"
end
end
end
end
end
context 'when the survey filename is given' do
it 'should look for the translation file relative to the survey directory' do
Dir.mktmpdir do |dir|
translation_yaml = YAML::dump({'title' => 'Un idioma nunca es suficiente'})
context 'when the survey filename is given' do
it 'should look for the translation file relative to the survey directory' do
Dir.mktmpdir do |dir|
translation_yaml = YAML::dump({'title' => 'Un idioma nunca es suficiente'})
translation_temp_file = Tempfile.new('surveyor:parser_spec.rb',dir)
translation_temp_file.write(translation_yaml)
translation_temp_file.flush
survey_text = <<END
survey "One language is never enough" do
translations :es =>'#{File.basename(translation_temp_file.path)}'
translations :es =>'#{File.basename(translation_temp_file.path)}', :en => :default
section_one "One" do
g_hello "Hello" do
q_name "What is your name?"
Expand All @@ -103,13 +157,14 @@
end
end
END
survey_temp_file = Tempfile.new('surveyor:parser_spec.rb',dir)
survey_temp_file.write(survey_text)
survey_temp_file.flush
Surveyor::Parser.parse_file(survey_temp_file.path)
survey = Survey.where(:title=>'One language is never enough').first
survey.nil?.should == false
survey.translation(:es)['title'].should == "Un idioma nunca es suficiente"
survey_temp_file = Tempfile.new('surveyor:parser_spec.rb',dir)
survey_temp_file.write(survey_text)
survey_temp_file.flush
Surveyor::Parser.parse_file(survey_temp_file.path)
survey = Survey.where(:title=>'One language is never enough').first
survey.nil?.should == false
survey.translation(:es)['title'].should == "Un idioma nunca es suficiente"
end
end
end
end
Expand Down

0 comments on commit d2ea2f8

Please sign in to comment.