Class: Rake::Pipeline::I18n::Filters::I18nJsFilter

Inherits:
Filter
  • Object
show all
Includes:
FilterWithDependencies
Defined in:
lib/rake-pipeline-i18n-filters/i18n_js_filter.rb

Overview

A filter that compiles locale yml files into javascript appropriate for use by Ember#String#loc

Examples:

Rake::Pipeline.build do
  input "config/locales", "**/*.yml"
  output "public"

  # Compile each .yml file under the config/locales
  # directory.
  filter Rake::Pipeline::I18n::Filters::I18nJSFilter
end

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ I18nJsFilter

Returns a new instance of I18nJsFilter.

Parameters:

  • block (Proc)

    a block to use as the Filter’s #output_name_generator.

See Also:



25
26
27
28
# File 'lib/rake-pipeline-i18n-filters/i18n_js_filter.rb', line 25

def initialize(&block)
  block ||= proc { |input| input.sub(/\.(yml)$/, '.js') }
  super(&block)
end

Instance Method Details

#generate_output(inputs, output) ⇒ Object

Implement the #generate_output method required by the Filter API. Generates javascript from i18n yaml files appropriate for i18n-js

Parameters:

  • inputs (Array<FileWrapper>)

    an Array of FileWrapper objects representing the inputs to this filter.

  • output (FileWrapper)

    a single FileWrapper object representing the output.



38
39
40
41
42
43
44
# File 'lib/rake-pipeline-i18n-filters/i18n_js_filter.rb', line 38

def generate_output(inputs, output)
  output_hash = {}
  inputs.each do |input|
    output_hash.deep_merge! YAML.load(input.read)
  end  
  output.write "I18n.translations = #{output_hash.to_json};"
end