Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with some Erubis syntax / interpolated strings #119

Open
toupeira opened this issue Feb 6, 2014 · 0 comments
Open

Problems with some Erubis syntax / interpolated strings #119

toupeira opened this issue Feb 6, 2014 · 0 comments

Comments

@toupeira
Copy link

toupeira commented Feb 6, 2014

GetText's ERB parser uses the standard ERB library, while Rails uses Erubis which has some syntax extensions, such as -%> to avoid adding empty lines to the output. It also uses RubyParser to parse the generated code, which has the same problems solved by the RubyGettextExtractor included in gettext_i18n_rails.

To work around this I added the following code in lib/tasks/gettext.rake:

# use the Ruby parser from gettext_i18n_rails, which supports "#{_('...')}" syntax
require 'gettext_i18n_rails/ruby_gettext_extractor'
GetText::Tools::XGetText.add_parser(RubyGettextExtractor)

# add a custom ERB parser which uses Erubis and RubyGettextExtractor
class ErubisGettextExtractor < GetText::ErbParser
  def self.target?(file)
    File.extname(file) == '.erb'
  end

  # adapted from gettext/tools/parser/erb.rb
  def parse
    content = IO.read(@path)
    src = ActionView::Template::Handlers::Erubis.new(content).src

    # Force the src encoding back to the encoding in magic comment
    # or original content.
    encoding = detect_encoding(src) || content.encoding
    src.force_encoding(encoding)

    # Remove magic comment prepended by erb in Ruby 1.9.
    src = src.gsub(MAGIC_COMMENT, "")

    RubyGettextExtractor.parse_string(src, @path)
  end
end

GetText::Tools::XGetText.add_parser(ErubisGettextExtractor)

It would be great if something like this could be included in the gem, and both of these extractors could either be made the default, or at least mentioned in the README ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant