Class: I18n::JS::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n/js/segment.rb

Overview

Class which enscapulates a translations hash and outputs a single JSON translation file

Constant Summary collapse

OPTIONS =
[:namespace, :pretty_print, :js_extend, :sort_translation_keys].freeze
LOCALE_INTERPOLATOR =
/%\{locale\}/

Instance Method Summary collapse

Constructor Details

#initialize(file, translations, options = {}) ⇒ Segment

Returns a new instance of Segment.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/i18n/js/segment.rb', line 13

def initialize(file, translations, options = {})
  @file         = file
  # `#slice` will be used
  # But when activesupport is absent,
  # the core extension from `i18n` gem will be used instead
  # And it's causing errors (at least in test)
  #
  # So the input is wrapped by our class for better `#slice`
  @translations = Private::HashWithSymbolKeys.new(translations)
  @namespace    = options[:namespace] || 'I18n'
  @pretty_print = !!options[:pretty_print]
  @js_extend    = options.key?(:js_extend) ? !!options[:js_extend] : true
  @sort_translation_keys = options.key?(:sort_translation_keys) ? !!options[:sort_translation_keys] : true
end

Instance Method Details

#save!Object

Saves JSON file containing translations



29
30
31
32
33
34
35
36
37
# File 'lib/i18n/js/segment.rb', line 29

def save!
  if @file =~ LOCALE_INTERPOLATOR
    I18n.available_locales.each do |locale|
      write_file(file_for_locale(locale), @translations.slice(locale))
    end
  else
    write_file
  end
end