Class: Gherkin::I18n

Inherits:
Object
  • Object
show all
Defined in:
lib/gherkin/i18n.rb

Constant Summary collapse

KEYWORD_KEYS =
%w{name native feature background scenario scenario_outline examples given when then and but}
LANGUAGES =
YAML.load_file(File.dirname(__FILE__) + '/i18n.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ I18n

Returns a new instance of I18n.



42
43
44
45
46
47
# File 'lib/gherkin/i18n.rb', line 42

def initialize(key)
  @key = key
  @keywords = LANGUAGES[key]
  raise "Language not supported: #{key.inspect}" if @key.nil?
  @keywords['grammar_name'] = @keywords['name'].gsub(/\s/, '')
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



40
41
42
# File 'lib/gherkin/i18n.rb', line 40

def key
  @key
end

Class Method Details

.allObject

Used by code generators for other lexer tools like pygments lexer and textmate bundle



10
11
12
# File 'lib/gherkin/i18n.rb', line 10

def all
  LANGUAGES.keys.sort.map{|key| get(key)}
end

.get(key) ⇒ Object



14
15
16
# File 'lib/gherkin/i18n.rb', line 14

def get(key)
  languages[key] ||= new(key)
end

.keyword_regexp(*keywords) ⇒ Object

Returns all keyword translations and aliases of keywords, escaped and joined with |. This method is convenient for editor support and syntax highlighting engines for Gherkin, where there is typically a code generation tool to generate regular expressions for recognising the various I18n translations of Gherkin’s keywords.

The keywords arguments can be one of :feature, :background, :scenario, :scenario_outline, :examples, :step.



29
30
31
32
33
34
35
36
37
# File 'lib/gherkin/i18n.rb', line 29

def keyword_regexp(*keywords)
  unique_keywords = all.map do |lang|
    keywords.map do |keyword|
      lang.__send__("#{keyword}_keywords".to_sym)
    end
  end
  
  unique_keywords.flatten.compact.sort.reverse.uniq.join('|').gsub(/\*/, '\*')
end

.languagesObject



18
19
20
# File 'lib/gherkin/i18n.rb', line 18

def languages
  @languages ||= {}
end

Instance Method Details

#adverbsObject



98
99
100
101
# File 'lib/gherkin/i18n.rb', line 98

def adverbs
  # TODO: looks very similar to #step_keywords. Lose this? Where is it used from?
  %w{given when then and but}.map{|keyword| @keywords[keyword].split('|').map{|w| w.gsub(/[\s<']/, '')}}.flatten
end

#and_keywords(space = true) ⇒ Object



81
82
83
# File 'lib/gherkin/i18n.rb', line 81

def and_keywords(space=true)
  keywords('and', space)
end

#background_keywordsObject



69
70
71
# File 'lib/gherkin/i18n.rb', line 69

def background_keywords
  keywords('background')
end

#but_keywords(space = true) ⇒ Object



77
78
79
# File 'lib/gherkin/i18n.rb', line 77

def but_keywords(space=true)
  keywords('but', space)
end

#examples_keywordsObject



73
74
75
# File 'lib/gherkin/i18n.rb', line 73

def examples_keywords
  keywords('examples')
end

#feature_keywordsObject



57
58
59
# File 'lib/gherkin/i18n.rb', line 57

def feature_keywords
  keywords('feature')
end

#gwt_keywordsObject



89
90
91
# File 'lib/gherkin/i18n.rb', line 89

def gwt_keywords
  %w{given when then}.map{|key| keywords(key, true)}.flatten.uniq
end

#incomplete?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/gherkin/i18n.rb', line 53

def incomplete?
  KEYWORD_KEYS.detect{|key| @keywords[key].nil?}
end

#keywords(key, space = false) ⇒ Object



93
94
95
96
# File 'lib/gherkin/i18n.rb', line 93

def keywords(key, space=false)
  raise "No #{key} in #{@keywords.inspect}" if @keywords[key].nil?
  @keywords[key].split('|').map{|kw| space ? keyword_space(kw) : kw}
end

#sanitized_keyObject



49
50
51
# File 'lib/gherkin/i18n.rb', line 49

def sanitized_key
  @key.gsub(/[\s-]/, '')
end

#scenario_keywordsObject



61
62
63
# File 'lib/gherkin/i18n.rb', line 61

def scenario_keywords
  keywords('scenario')
end

#scenario_outline_keywordsObject



65
66
67
# File 'lib/gherkin/i18n.rb', line 65

def scenario_outline_keywords
  keywords('scenario_outline')
end

#step_keywordsObject



85
86
87
# File 'lib/gherkin/i18n.rb', line 85

def step_keywords
  %w{given when then and but}.map{|key| keywords(key, true)}.flatten.uniq
end