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



42
43
44
# File 'lib/gettext_i18n_rails_js/parser/base.rb', line 42

def gettext_function
  @gettext_function ||= "__"
end

Instance Method Details

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

We’re lazy and clumsy, 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

The PO file outputs to a “” string. single quotes are unescaped (if they are escaped) double quotes are escaped (if they are not already escaped)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gettext_i18n_rails_js/parser/base.rb', line 78

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

    next if key == ""

    results_for(key, file, line)
  end
end