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

Support i18n-tasks-use hints within JavaScript comment lines #322

Closed
cbeckr opened this issue Dec 5, 2018 · 3 comments
Closed

Support i18n-tasks-use hints within JavaScript comment lines #322

cbeckr opened this issue Dec 5, 2018 · 3 comments
Milestone

Comments

@cbeckr
Copy link

cbeckr commented Dec 5, 2018

i18n-tasks does not pick up i18n-tasks-use hints written in JavaScript, e.g.:

// i18n-tasks-use t('some.key')

The reason for this lies in RubyAstScanner's MAGIC_COMMENT_PREFIX, which looks as follows: /\A.\s*i18n-tasks-use\s+/.
The first dot is assuming a single comment character, so the line does not get picked up.

@glebm
Copy link
Owner

glebm commented Dec 5, 2018

RubyAstScanner is only used to scan .rb files.
For .js and other files, the following is used instead:

IGNORE_LINES = {
'opal' => /^\s*#(?!\si18n-tasks-use)/,
'haml' => /^\s*-\s*#(?!\si18n-tasks-use)/,
'slim' => %r{^\s*(?:-#|/)(?!\si18n-tasks-use)},
'coffee' => /^\s*#(?!\si18n-tasks-use)/,
'erb' => /^\s*<%\s*#(?!\si18n-tasks-use)/
}.freeze

PR welcome

@glebm glebm added this to the v0.9.29 milestone Mar 31, 2019
@glebm
Copy link
Owner

glebm commented Mar 31, 2019

Note that magic comments are actually supported but the call must match the following pattern:

TRANSLATE_CALL_RE = /(?<=^|[^\w'\-.]|[^\w'\-]I18n\.|I18n\.)t(?:ranslate)?/

@glebm glebm closed this as completed in 792c194 Mar 31, 2019
@zealot128
Copy link

Thanks for the tip. Because the IGNORE_LINES is frozen, i cannot modify IGNORE_LINES directly, but have to inherit from PatternScanner and reset it in an initializer.

class JsScanner < I18n::Tasks::Scanners::PatternScanner
  IGNORE_LINES = I18n::Tasks::Scanners::PatternScanner::IGNORE_LINES.merge(
    'js' => re=%r{^\s*// *(?!\si18n-tasks-use)}, 
    'ts' => re
  )
  def initialize(**args)
    super
    @ignore_lines_res = (config[:ignore_lines] || IGNORE_LINES).each_with_object({}) do |(ext, re), h|
      h[ext.to_s] = Regexp.new(re)
    end
  end
end
I18n::Tasks.add_scanner 'JsScanner', only: %w(*.js *.ts)

To make globs work in the use, one cannot use "*" but must simulate ruby in the comment:

// i18n-tasks-use t("js.feedback.behavior.options.#{key}")
$t("...")

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

3 participants