Parent

Included Modules

Class/Module Index [+]

Quicksearch

Gherkin::I18n

Attributes

iso_code[R]

Public Class Methods

all() click to toggle source

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

# File lib/gherkin/i18n.rb, line 18
def all
  LANGUAGES.keys.sort.map{|iso_code| get(iso_code)}
end
code_keyword_for(gherkin_keyword) click to toggle source
# File lib/gherkin/i18n.rb, line 51
def code_keyword_for(gherkin_keyword)
  gherkin_keyword.gsub(/[\s',!]/, '').strip
end
code_keywords() click to toggle source
# File lib/gherkin/i18n.rb, line 47
def code_keywords
  rubify(all.map{|i18n| i18n.code_keywords}).flatten.uniq.sort
end
get(iso_code) click to toggle source
# File lib/gherkin/i18n.rb, line 22
def get(iso_code)
  languages[iso_code] ||= new(iso_code)
end
keyword_regexp(*keywords) click to toggle source

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.

# File lib/gherkin/i18n.rb, line 33
def keyword_regexp(*keywords)
  unique_keywords = all.map do |i18n|
    keywords.map do |keyword|
      if keyword.to_s == 'step'
        i18n.step_keywords.to_a
      else
        i18n.keywords(keyword).to_a
      end
    end
  end
  
  unique_keywords.flatten.compact.map{|kw| kw.to_s}.sort.reverse.uniq.join('|').gsub(/\*/, '\*')
end
language_table() click to toggle source
# File lib/gherkin/i18n.rb, line 55
def language_table
  require 'stringio'
  require 'gherkin/formatter/pretty_formatter'
  require 'gherkin/formatter/model'
  io = StringIO.new
  pf = Gherkin::Formatter::PrettyFormatter.new(io, false, false)
  table = all.map do |i18n|
    Formatter::Model::Row.new([], [i18n.iso_code, i18n.keywords('name')[0], i18n.keywords('native')[0]], nil)
  end
  pf.table(table)
  io.string
end
new(iso_code) click to toggle source
# File lib/gherkin/i18n.rb, line 87
def initialize(iso_code)
  @iso_code = iso_code
  @keywords = LANGUAGES[iso_code]
  raise "Language not supported: #{iso_code.inspect}" if @iso_code.nil?
  @keywords['grammar_name'] = @keywords['name'].gsub(/\s/, '')
end
unicode_escape(word, prefix="\\u") click to toggle source
# File lib/gherkin/i18n.rb, line 68
def unicode_escape(word, prefix="\\u")
  word = word.unpack("U*").map do |c|
    if c > 127 || c == 32
      "#{prefix}%04x" % c
    else
      c.chr
    end
  end.join
end

Public Instance Methods

c(listener) click to toggle source
# File lib/gherkin/i18n.rb, line 111
def c(listener)
  require 'gherkin/c_lexer'
  CLexer[underscored_iso_code].new(listener)
end
code_keywords() click to toggle source

Keywords that can be used in code

# File lib/gherkin/i18n.rb, line 136
def code_keywords
  result = step_keywords.map{|keyword| self.class.code_keyword_for(keyword)}
  result.delete('*')
  result
end
js(listener) click to toggle source
# File lib/gherkin/i18n.rb, line 121
def js(listener)
  require 'gherkin/js_lexer'
  JsLexer[underscored_iso_code].new(listener)
end
keyword_table() click to toggle source
# File lib/gherkin/i18n.rb, line 148
def keyword_table
  require 'stringio'
  require 'gherkin/formatter/pretty_formatter'
  require 'gherkin/formatter/model'
  io = StringIO.new
  pf = Gherkin::Formatter::PrettyFormatter.new(io, false, false)

  gherkin_keyword_table = KEYWORD_KEYS.map do |key|
    Formatter::Model::Row.new([], [key, keywords(key).map{|keyword| %{"#{keyword}"}}.join(', ')], nil)
  end
  
  code_keyword_table = STEP_KEYWORD_KEYS.map do |key|
    code_keywords = keywords(key).reject{|keyword| keyword == '* '}.map do |keyword|
      %{"#{self.class.code_keyword_for(keyword)}"}
    end.join(', ')
    Formatter::Model::Row.new([], ["#{key} (code)", code_keywords], nil)
  end
  
  pf.table(gherkin_keyword_table + code_keyword_table)
  io.string
end
keywords(key) click to toggle source
# File lib/gherkin/i18n.rb, line 142
def keywords(key)
  key = key.to_s
  raise "No #{key.inspect} in #{@keywords.inspect}" if @keywords[key].nil?
  @keywords[key].split('|').map{|keyword| real_keyword(key, keyword)}
end
lexer(listener, force_ruby=false) click to toggle source
# File lib/gherkin/i18n.rb, line 94
def lexer(listener, force_ruby=false)
  begin
    if force_ruby
      rb(listener)
    else
      begin
        c(listener)
      rescue NameError, LoadError => e
        warn("WARNING: #{e.message}. Reverting to Ruby lexer.")
        rb(listener)
      end
    end
  rescue LoadError => e
    raise I18nLexerNotFound, "No lexer was found for #{i18n_language_name} (#{e.message}). Supported languages are listed in gherkin/i18n.yml."
  end
end
rb(listener) click to toggle source
# File lib/gherkin/i18n.rb, line 116
def rb(listener)
  require 'gherkin/rb_lexer'
  RbLexer[underscored_iso_code].new(listener)
end
step_keywords() click to toggle source

Keywords that can be used in Gherkin source

# File lib/gherkin/i18n.rb, line 131
def step_keywords
  STEP_KEYWORD_KEYS.map{|iso_code| keywords(iso_code)}.flatten.uniq
end
underscored_iso_code() click to toggle source
# File lib/gherkin/i18n.rb, line 126
def underscored_iso_code
  @iso_code.gsub(/[\s-]/, '_').downcase
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.