Skip to content

Commit

Permalink
Merge branch '429_load_translation_file'
Browse files Browse the repository at this point in the history
Closes NUBIC#429

* 429_load_translation_file:
  recording changes
  change variable name and spec description NUBIC#429
  change method name NUBIC#429
  specify source of survey when parsing NUBIC#429
  • Loading branch information
Mark Yoon committed Mar 29, 2013
2 parents c16dc58 + 5e1dbe6 commit 2c301c4
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ History for Surveyor
### Features

- Routes are namespaced (e.g. `surveyor.available_surveys_path`) and may be mounted at a different root (e.g. `mount Surveyor::Engine, :at => '/instruments'`) (#398, #421)
- Surveyor::Parser.parse_file takes an options[:filename] parameter, used to locate translations (#429)
- Surveyor::Parser allows translations to be specified inline using a hash (#429)

### Fixes

Expand Down
4 changes: 3 additions & 1 deletion features/internationalization.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ Feature: Internationalization
Given I parse
"""
survey "One language is never enough" do
translations :es => "translations/languages.es.yml", :he => "translations/languages.he.yml", :ko => "translations/languages.ko.yml"
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"=>"제 이름은 ... 입니다"}}}}}
section_one "One" do
g_hello "Hello" do
q_name "What is your name?"
Expand Down
15 changes: 14 additions & 1 deletion lib/surveyor/parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
%w(survey survey_translation survey_section question_group question dependency dependency_condition answer validation validation_condition).each {|model| require model }

require 'yaml'

module Surveyor
class ParserError < StandardError; end
class Parser
Expand All @@ -8,6 +11,9 @@ class << self; attr_accessor :options, :log end
attr_accessor :context

# Class methods
def self.parse_file(filename, options={})
self.parse(File.read(filename),{:filename => filename}.merge(options))
end
def self.parse(str, options={})
self.ensure_attrs
self.options = options
Expand Down Expand Up @@ -177,9 +183,16 @@ def clear(context)
# SurveySection model
module SurveyorParserSurveyTranslationMethods
def parse_and_build(context, args, original_method, reference_identifier)
dir = Surveyor::Parser.options[:filename].nil? ? Dir.pwd : File.dirname(Surveyor::Parser.options[:filename])
# build, no change in context
args[0].each do |k,v|
context[:survey].translations << self.class.new(:locale => k.to_s, :translation => File.read(Rails.root.join("surveys", v)))
case v
when Hash
trans = YAML::dump(v)
when String
trans = File.read(File.join(dir,v))
end
context[:survey].translations << self.class.new(:locale => k.to_s, :translation => trans)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/surveyor_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace :surveyor do
file = File.join(Rails.root, ENV["FILE"])
raise "File does not exist: #{file}" unless FileTest.exists?(file)
puts "--- Parsing #{file} ---"
Surveyor::Parser.parse(File.read(file), {:trace => Rake.application.options.trace})
Surveyor::Parser.parse_file(file, {:trace => Rake.application.options.trace})
puts "--- Done #{file} ---"
end
desc "generate and load survey from REDCap Data Dictionary (specify FILE=surveys/redcap.csv)"
Expand Down
78 changes: 77 additions & 1 deletion spec/lib/parser_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe Surveyor::Parser do
Expand Down Expand Up @@ -33,4 +34,79 @@
Surveyor::Parser.new.parse("survey 'hi' do\n end").is_a?(Survey).should be_true
end

end

context 'when a translation is specified as a Hash' do
it 'should should treat the hash as an inline translation' do
survey = <<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..."}}}}}
section_one "One" do
g_hello "Hello" do
q_name "What is your name?"
a_name :string, :help_text => "My name is..."
end
end
end
END
s = Surveyor::Parser.new.parse(survey)
s.is_a?(Survey).should == true
s.translations.size.should == 1
s.translation(:es)['title'].should == "Un idioma nunca es suficiente"
end
end

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
t = YAML::dump({'title' => 'Un idioma nunca es suficiente'})
tf = Tempfile.new('surveyor:parser_spec.rb',dir);tf.write(t);tf.flush
s = <<END
survey "One language is never enough" do
translations :es =>'#{File.basename(tf.path)}'
section_one "One" do
g_hello "Hello" do
q_name "What is your name?"
a_name :string, :help_text => "My name is..."
end
end
end
END
sf = Tempfile.new('surveyor:parser_spec.rb',dir);sf.write(s);sf.flush
Surveyor::Parser.parse(File.read(sf))
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
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|
t = YAML::dump({'title' => 'Un idioma nunca es suficiente'})
tf = Tempfile.new('surveyor:parser_spec.rb',dir);tf.write(t);tf.flush
s = <<END
survey "One language is never enough" do
translations :es =>'#{File.basename(tf.path)}'
section_one "One" do
g_hello "Hello" do
q_name "What is your name?"
a_name :string, :help_text => "My name is..."
end
end
end
END

sf = Tempfile.new('surveyor:parser_spec.rb',dir);sf.write(s);sf.flush
Surveyor::Parser.parse_file(sf)
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

0 comments on commit 2c301c4

Please sign in to comment.