Class: I18nGenerator::LabelWriter
- Inherits:
-
Object
- Object
- I18nGenerator::LabelWriter
- 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
- FILE_HEADER_TEXT =
<<-HEADER # THIS FILE IS GENERATED - PLEASE DO NOT EDIT (https://github.com/rulefinancial/i18n_generator) HEADER
Instance Method Summary collapse
-
#content_for_label(label, language) ⇒ Object
Returns the content for a Label in a given language Description is included if present.
-
#initialize(output_directory, bundle_prefix) ⇒ LabelWriter
constructor
A new instance of LabelWriter.
-
#label_filename(language) ⇒ Object
Produces the filename for a resource bundle.
-
#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.
-
#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.
-
#write_labels(labels, languages) ⇒ Object
Orchestrates the processing of Labels to be written Writes files to output location.
Constructor Details
#initialize(output_directory, bundle_prefix) ⇒ LabelWriter
Returns a new instance of LabelWriter.
15 16 17 18 19 20 21 |
# File 'lib/i18n_generator/label_writer.rb', line 15 def initialize(output_directory, bundle_prefix) raise 'LabelWriter requires an output path' unless output_directory raise 'LabelWriter requires a filename prefix' unless bundle_prefix @output_path = output_directory @bundle_prefix = bundle_prefix 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
59 60 61 62 63 |
# File 'lib/i18n_generator/label_writer.rb', line 59 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
75 76 77 |
# File 'lib/i18n_generator/label_writer.rb', line 75 def label_filename(language) @bundle_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
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/i18n_generator/label_writer.rb', line 27 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
68 69 70 71 |
# File 'lib/i18n_generator/label_writer.rb', line 68 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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/i18n_generator/label_writer.rb', line 41 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 |