class ExtractI18::HTMLExtractor::Runner
Public Class Methods
new(args = {})
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 6 def initialize(args = {}) @files = file_list_from_pattern(args[:file_pattern]) @locale = args[:locale].presence @verbose = args[:verbose] end
Public Instance Methods
run()
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 27 def run each_translation do |file, document, node| puts "Found \"#{node.text}\" in #{file}:#{node.text}".green node.replace_text! document.save!(file) add_translation! I18n.default_locale, node.key, node.text end end
run_interactive()
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 12 def run_interactive each_translation do |file, document, node| puts "Found \"#{node.text}\" in #{file}:#{node.text}".green next unless confirm 'Create a translation?', 'Yes', 'No', default: 'Yes' node.key = prompt 'Choose i18n key', default: node.key node.replace_text! document.save!(file) add_translations! node.key, node.text, default_locale: @locale puts end end
test_run()
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 37 def test_run each_translation do |file, _, node| puts "Found \"#{node.text}\" in #{file}:#{node.text}".green end end
Private Instance Methods
add_translation!(locale, key, value)
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 70 def add_translation!(locale, key, value) new_keys = i18n.missing_keys(locales: [locale]).set_each_value!(value) i18n.data.merge! new_keys puts "Added t(.#{key}), translated in #{locale} as #{value}:".green puts new_keys.inspect end
add_translations!(key, text, default_locale: nil)
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 54 def add_translations!(key, text, default_locale: nil) return prompt_and_add_translation!(default_locale, key, default_text: text) if default_locale prompt_and_add_translation!(I18n.default_locale, key, default_text: text) I18n.available_locales.each do |locale| next if locale == I18n.default_locale prompt_and_add_translation!(locale.to_s, key) end end
each_translation() { |file, document, node| ... }
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 81 def each_translation @files.each do |file| document = I18n::HTMLExtractor::ErbDocument.parse file nodes_to_translate = extract_all_nodes_to_translate(document) nodes_to_translate.each { |node| yield(file, document, node) } end end
extract_all_nodes_to_translate(document)
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 89 def extract_all_nodes_to_translate(document) Match::Finder.new(document).matches end
file_list_from_pattern(pattern)
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 45 def file_list_from_pattern(pattern) if pattern.present? Dir[Rails.root.join(pattern)] else Dir[Rails.root.join('app', 'views', '**', '*.erb')] - Dir[Rails.root.join('app', 'views', '**', '*.*.*.erb')] end end
i18n()
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 77 def i18n I18n::Tasks::BaseTask.new end
prompt_and_add_translation!(locale, key, default_text: nil)
click to toggle source
# File lib/extract_i18n/html_extractor/runner.rb, line 65 def prompt_and_add_translation!(locale, key, default_text: nil) out_text = prompt "Choose #{locale} value", default: default_text add_translation! locale, key, out_text end