Skip to content

Instantly share code, notes, and snippets.

@ceres629
Last active Nov 18, 2015
Embed
What would you like to do?
#lib/gettext_scanner.rb
require 'i18n/tasks/scanners/pattern_scanner'
class GettextScanner < I18n::Tasks::Scanners::PatternScanner
QUOTES = ["'".freeze, '"'.freeze].freeze
VALID_KEY_CHARS = /(?:[[:word:]]|[-\s.?'!,\(\)\)\/“”–;<>À-ž])/
VALID_KEY_RE = /^(#{VALID_KEY_CHARS}|[%:\#{@}\[\]])+$/
def translate_call_re
/(?<=^|[^\w'\-])t|_(?:ranslate)?/
end
def valid_key?(key)
key =~ VALID_KEY_RE
end
def scan_file(path)
keys = []
text = read_file(path)
text.scan(@pattern) do |match|
src_pos = Regexp.last_match.offset(0).first
location = occurrence_from_position(path, text, src_pos)
next if exclude_line?(location.line, path)
key = match_to_key(match, path, location)
next unless key
next unless valid_key?(key)
keys << [key, location]
end
keys
rescue Exception => e
raise ::I18n::Tasks::CommandError.new(e, "Error scanning #{path}: #{e.message}")
end
end
I18n::Tasks.add_scanner "GettextScanner", only: %w(*.haml *.erb)
#config/i18n-tasks.yml.erb
<% require './lib/gettext_scanner.rb' %>
# i18n-tasks finds and manages missing and unused translations: https://github.com/glebm/i18n-tasks
# The "main" locale.
base_locale: en
## All available locales are inferred from the data by default. Alternatively, specify them explicitly:
# locales: [es, fr]
## Reporting locale, default: en. Available: en, ru.
# internal_locale: en
# Read and write translations.
data:
yaml:
write:
# do not wrap lines at 80 characters
line_width: -1
# Find translate calls
search:
scanner: GettextScanner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment