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

I want i18n-tasks to ensure a '.title' whenever I use a headline_with_flash in a view #138

Closed
jmuheim opened this issue Mar 25, 2015 · 6 comments

Comments

@jmuheim
Copy link

jmuheim commented Mar 25, 2015

I'm using a method called headline_with_flash in most of my views:

h1 = headline_with_flash

The method looks like this:

  def headline_with_flash(options = {})
    t '.title', options
  end

The method lives in page_title_and_headline_helper.rb and I had to exclude this file using

search:
  exclude:
    - ...
    - '*/page_title_and_headline_helper.rb'

Otherwise, i18n-tasks would fail:

app/helpers/page_title_and_headline_helper.rb: cannot resolve relative key

So now I'd like i18n-tasks to make sure that whenever it meets a call to headline_with_flash in a view, it checks whether a corresponding .title key exists in the translations.

Is this possible?

@glebm
Copy link
Owner

glebm commented Mar 25, 2015

You can extend the scanner, or work around with a magic comment:

/ i18n-tasks-use t('.title')
= headline_with_flash

@jmuheim
Copy link
Author

jmuheim commented Mar 25, 2015

Extending the scanner sounds promising, but it looks very complicated. Could you point me into the right direction, please?

@glebm
Copy link
Owner

glebm commented Mar 28, 2015

You could try something like below (untested code):

In lib/my_i18n_scanner.rb:

class MyI18nScanner < I18n::Tasks::PatternScanner
  def scan_file(path, opts = {})
    keys = super
    if path =~ /\.(slim|haml)$/
      text = opts[:text] || read_file(path)
      text.scan(/^\s*=\s*headline_with_flash\b/) do |pattern|
        location = src_location(path, text, Regexp.last_match.offset(0).first)
        keys << [absolute_key('.title', path, location), data: location]
      end
    end
    keys
  end
end

In config/i18n-tasks.yml:

<% require '../lib/my_i18n_scanner' %>
search:
  scanner: MyI18nScanner

It would be great if i18n-tasks supported multiple scanners (#81).

@glebm
Copy link
Owner

glebm commented Mar 31, 2015

If this works out for you, please add a wiki article 👍

@glebm glebm closed this as completed Mar 31, 2015
@jmuheim
Copy link
Author

jmuheim commented Apr 1, 2015

I didn't have the time to really check it out. It's on my TODO list though. Thanks!

@glebm
Copy link
Owner

glebm commented Aug 8, 2015

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

2 participants