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, :prefix, :suffix, :sort_translation_keys, :json_only].freeze
LOCALE_INTERPOLATOR =
/%\{locale\}/

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Segment.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/i18n/js/segment.rb', line 15

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
  @prefix       = options.key?(:prefix) ? options[:prefix] : nil
  @suffix       = options.key?(:suffix) ? options[:suffix] : nil
  @sort_translation_keys = options.key?(:sort_translation_keys) ? !!options[:sort_translation_keys] : true
  @json_only = options.key?(:json_only) ? !!options[:json_only] : false
end

Instance Method Details

#save!Object

Saves JSON file containing translations



34
35
36
37
38
39
40
41
42
# File 'lib/i18n/js/segment.rb', line 34

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