Module: GettextI18nRailsJs::Parser::Base

Extended by:
Base
Included in:
Base, Handlebars, Javascript
Defined in:
lib/gettext_i18n_rails_js/parser/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gettext_functionObject

The gettext function name can be configured at the module level as gettext_function. This is to provide a way to avoid conflicts with other javascript libraries. You only need to define the base function name to replace “_” and all the other variants (s_, n_, N_) will be deduced automatically.



39
40
41
# File 'lib/gettext_i18n_rails_js/parser/base.rb', line 39

def gettext_function
  @gettext_function
end

Instance Method Details

#parse(file, _msgids = []) ⇒ Object

We’re lazy and klumsy, so this is a regex based parser that looks for invocations of the various gettext functions. Once captured, we scan them once again to fetch all the function arguments. Invoke regex captures like this:

javascript source: “#{ __(‘hello’) } #{ __(”wor)ld“) }” matches: [0]: __(‘hello’) [1]: __ [2]: ‘hello’

javascript source: __(‘item’, ‘items’, 33) matches: [0]: __(‘item’, ‘items’, 33) [1]: __ [2]: ‘item’, ‘items’, 33

handlebars source: “_ ”foo“}” matches: [0]: __(‘foo’) [1]: __ [2]: ‘foo’

handlebars source: “_ ”foo“ ”foos“ 3}” matches: [0]: __(‘foo’, ‘foos’, 3) [1]: __ [2]: ‘foo’, ‘foos’, 3



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/gettext_i18n_rails_js/parser/base.rb', line 73

def parse(file, _msgids = [])
  collect_for(file) do |function, arguments, line|
    key = arguments.scan(
      /('(?:[^'\\]|\\.)*'|"(?:[^"\\]|\\.)*"|`(?:[^`\\]|\\.)*`)/
    ).collect do |match|
      match.first[1..-2]
    end.join(separator_for(function))

    next if key == ""
    results_for(key, file, line)
  end
end