diff --git a/features/internationalization.feature b/features/internationalization.feature index 9411809f..176dc854 100644 --- a/features/internationalization.feature +++ b/features/internationalization.feature @@ -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"=>"제 이름은 ... 입니다"}}}}} @@ -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" diff --git a/lib/surveyor/parser.rb b/lib/surveyor/parser.rb index f6284db5..952cf940 100644 --- a/lib/surveyor/parser.rb +++ b/lib/surveyor/parser.rb @@ -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." @@ -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? @@ -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 diff --git a/spec/lib/parser_spec.rb b/spec/lib/parser_spec.rb index 33f4ba90..1aef1ca4 100644 --- a/spec/lib/parser_spec.rb +++ b/spec/lib/parser_spec.rb @@ -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 = < :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 = <{'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 = < :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 = < {"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?" @@ -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 = < '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 = <'#{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?" @@ -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 = <'#{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?" @@ -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