module GettextI18nRailsJs::Parser::Base
Attributes
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.
Public Instance Methods
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
# File lib/gettext_i18n_rails_js/parser/base.rb, line 72 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
Protected Instance Methods
# File lib/gettext_i18n_rails_js/parser/base.rb, line 87 def cleanup_value(value) value .tr("\n", "\n") .tr("\t", "\t") .tr("\0", "\0") end
# File lib/gettext_i18n_rails_js/parser/base.rb, line 102 def results_for(key, file, line) [ cleanup_value(key), [file, line].join(":") ] end
# File lib/gettext_i18n_rails_js/parser/base.rb, line 94 def separator_for(value) if value == "n#{gettext_function}" "\000" else "\004" end end