Class: I18n::JS::Segment
- Inherits:
-
Object
- Object
- I18n::JS::Segment
- 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
-
#initialize(file, translations, options = {}) ⇒ Segment
constructor
A new instance of Segment.
-
#save! ⇒ Object
Saves JSON file containing translations.
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, = {}) @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 = [:namespace] || 'I18n' @pretty_print = !![:pretty_print] @js_extend = .key?(:js_extend) ? !![:js_extend] : true @prefix = .key?(:prefix) ? [:prefix] : nil @suffix = .key?(:suffix) ? [:suffix] : nil @sort_translation_keys = .key?(:sort_translation_keys) ? !![:sort_translation_keys] : true @json_only = .key?(:json_only) ? !![: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 |