Class: Guard::I18next

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/i18next.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
    :output         => Pathname.pwd.join('app', 'assets', 'javascripts', 'i18n'),
}

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ I18next

Initialize Guard::i18next

Parameters:

  • watchers (Array<Guard::Watcher>) (defaults to: [])

    the watchers in the Guard block

  • options (Hash) (defaults to: {})

    the options for the Guard

Options Hash (options):

  • :output (String)

    the output directory



19
20
21
22
23
24
# File 'lib/guard/i18next.rb', line 19

def initialize(watchers = [], options = {})
  watchers = [] if !watchers
  defaults = DEFAULT_OPTIONS.clone

  super(watchers, defaults.merge(options))
end

Instance Method Details

#run_on_change(paths) ⇒ Object

Gets called when watched paths and files have changes.

Parameters:

  • paths (Array<String>)

    the changed paths and files

Raises:

  • (:task_has_failed)

    when stop has failed



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/guard/i18next.rb', line 31

def run_on_change(paths)
  Dir::mkdir(options[:output]) unless File.directory?(options[:output])

  paths.each do |locale_path|
    filename = File.basename(locale_path, ".yml")
    input = File.new(locale_path, 'r')
    locale = YAML.load(input.read)
    input.close

    File.open(options[:output] + "/#{filename}.json", "w") do |f|
        f.puts locale.to_json
    end
  end
end