Skip to content

Instantly share code, notes, and snippets.

@oc
Created September 13, 2009 01:19
Show Gist options
  • Save oc/186037 to your computer and use it in GitHub Desktop.
Save oc/186037 to your computer and use it in GitHub Desktop.
desc "Update Scala's I18N Language traits with file = /path/to/lib/cucumber/languages.yml"
task :update_scala_i18n, :file do |t, args|
# Limitations: Will only choose first variant of translation and will CamelCase multiple words
def e(str); str.gsub(/([\\'])|(\|.*)/, '').split(/[- _?()]/).map {|w| w.capitalize}.join; end
# Parse Cucumber language.yml and write the (new) Languages.scala file
out = File.new("cuke4duke/src/main/scala/cuke4duke/Languages.scala", "w")
out << "package cuke4duke\n"
YAML.load_file(args.file).each do |key, lang|
trait = [e(lang['name']), e(lang['given']), e(lang['when']), e(lang['then'])]
# Avoid duplicate definitions of same keyword (see:Uzbek) and already defined English keywords
unless trait.size != trait.uniq.size || trait[1] == 'Given' || trait[2] == 'When' || trait[3] == 'Then'
out << "trait #{trait[0]} { self: ScalaDsl =>\n"
out << " val #{trait[1]} = self.Given\n"
out << " val #{trait[2]} = self.When\n"
out << " val #{trait[3]} = self.Then\n"
out << "}\n"
else
puts "Couldn't generate language: #{trait[0]}"
end
end
out.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment