Sha256: 8055ea3c7797c892e752935ffe6d7ad06af1f818121cd614a7b1a40d33fbcd60

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module I18n
  module JS

    # Class which enscapulates a translations hash and outputs a single JSON translation file
    class Segment
      attr_accessor :file, :translations, :namespace, :pretty_print

      def initialize(file, translations, options = {})
        @file         = file
        @translations = translations
        @namespace    = options[:namespace] || 'I18n'
        @pretty_print = !!options[:pretty_print]
      end

      # Saves JSON file containing translations
      def save!
        FileUtils.mkdir_p File.dirname(self.file)

        File.open(self.file, "w+") do |f|
          f << %(#{self.namespace}.translations || (#{self.namespace}.translations = {});\n)
          self.translations.each do |locale, translations|
            f << %(#{self.namespace}.translations["#{locale}"] = #{print_json(translations)};\n);
          end
        end
      end

      protected

      # Outputs pretty or ugly JSON depending on :pretty_print option
      def print_json(translations)
        if pretty_print
          JSON.pretty_generate(translations)
        else
          translations.to_json
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
i18n-js-3.0.0.rc9 lib/i18n/js/segment.rb