Class: I18nGenerator::LabelWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_generator/label_writer.rb

Overview

Responsible for processing a list of Labels for output and writing them to output files

Constant Summary collapse

FILENAME_PREFIX =
"labels_"
FILE_HEADER_TEXT =
<<-HEADER
  # THIS FILE IS GENERATED - PLEASE DO NOT EDIT (https://github.com/rulefinancial/i18n_generator)
HEADER

Instance Method Summary collapse

Constructor Details

#initialize(output_directory) ⇒ LabelWriter

Returns a new instance of LabelWriter.



17
18
19
20
# File 'lib/i18n_generator/label_writer.rb', line 17

def initialize(output_directory)
  raise 'LabelWriter requires an output path' unless output_directory
  @output_path = output_directory
end

Instance Method Details

#content_for_label(label, language) ⇒ Object

Returns the content for a Label in a given language Description is included if present



58
59
60
61
62
# File 'lib/i18n_generator/label_writer.rb', line 58

def content_for_label(label, language)
  content = []
  content << "\# #{label.description}" if label.description
  content << "#{label.id}=#{translation_for_label(label, language)}"
end

#label_filename(language) ⇒ Object

Produces the filename for a resource bundle



74
75
76
# File 'lib/i18n_generator/label_writer.rb', line 74

def label_filename(language)
  FILENAME_PREFIX + language.to_s + ".properties"
end

#prepare_output(labels, languages) ⇒ Object

Processes a list of Label with target languages Returns a hash keyed by language, containing the lines of the resource bundle to be written



26
27
28
29
30
31
32
33
34
35
# File 'lib/i18n_generator/label_writer.rb', line 26

def prepare_output(labels, languages)
  output_files = Hash.new {|k,v| k[v] = [FILE_HEADER_TEXT]}

  labels.each do |label|
    languages.each do |language|
      output_files[language] << content_for_label(label, language)
    end
  end
  output_files
end

#translation_for_label(label, language) ⇒ Object

Returns the translation for a label in a given language If the target language is not present, English will be used



67
68
69
70
# File 'lib/i18n_generator/label_writer.rb', line 67

def translation_for_label(label, language)
  translation = label.send(language)
  "#{translation && !translation.empty? ? translation : label.en}"
end

#write_labels(labels, languages) ⇒ Object

Orchestrates the processing of Labels to be written Writes files to output location



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/i18n_generator/label_writer.rb', line 40

def write_labels(labels, languages)
  output_files = prepare_output(labels, languages)

  Dir.mkdir(@output_path) unless Dir.exists?(@output_path)

  output_files.keys.each_with_index do |language, idx|
    output_filename = File.join(@output_path,label_filename(language))

    File.open(output_filename, 'w') do |file|
      file.write(output_files[language].join("\n"))
      puts "[#{idx+1}/#{output_files.size}] Generated [#{label_filename(language)}]"
    end
  end
end