Sha256: 9cf1451ab14d084df2d0b3b96dfab89512c6781e47fa35cb9d2b840d9c547c9b

Contents?: true

Size: 815 Bytes

Versions: 26

Compression:

Stored size: 815 Bytes

Contents

require 'fileutils'

class Translate::File
  attr_accessor :path

  def initialize(path)
    self.path = path
  end

  def write(keys)
    FileUtils.mkdir_p File.dirname(path)
    File.open(path, "w") do |file|
      file.puts keys_to_yaml(Translate::File.deep_stringify_keys(keys))
    end
  end

  def read
    File.exists?(path) ? YAML::load(IO.read(path)) : {}
  end

  # Stringifying keys for prettier YAML
  def self.deep_stringify_keys(hash)
    hash.inject({}) { |result, (key, value)|
      value = deep_stringify_keys(value) if value.is_a? Hash
      result[(key.to_s rescue key) || key] = value
      result
    }
  end

  private
  def keys_to_yaml(keys)
    # Using ya2yaml, if available, for UTF8 support
    keys.respond_to?(:ya2yaml) ? keys.ya2yaml(:escape_as_utf8 => true) : keys.to_yaml
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
refinerycms-i18n-0.9.8.12 lib/translate/file.rb
refinerycms-i18n-0.9.8.11 lib/translate/file.rb
refinerycms-0.9.7.15 vendor/plugins/translate/lib/translate/file.rb
refinerycms-0.9.7.14 vendor/plugins/translate/lib/translate/file.rb
refinerycms-i18n-0.9.8.8 lib/translate/file.rb
refinerycms-i18n-0.9.8.7 lib/translate/file.rb
refinerycms-i18n-0.9.8.6 lib/translate/file.rb
refinerycms-i18n-0.9.8.5 lib/translate/file.rb
refinerycms-i18n-0.9.8.4 lib/translate/file.rb
refinerycms-i18n-0.9.8.3 lib/translate/file.rb
refinerycms-i18n-0.9.8.2 lib/translate/file.rb
refinerycms-i18n-0.9.8.1 lib/translate/file.rb
refinerycms-i18n-0.9.8 lib/translate/file.rb
refinerycms-0.9.7.13 vendor/plugins/translate/lib/translate/file.rb
refinerycms-0.9.7.11 vendor/plugins/translate/lib/translate/file.rb
refinerycms-0.9.7.10 vendor/plugins/translate/lib/translate/file.rb
refinerycms-0.9.7.9 vendor/plugins/translate/lib/translate/file.rb
refinerycms-0.9.7.8 vendor/plugins/translate/lib/translate/file.rb
refinerycms-0.9.7.7 vendor/plugins/translate/lib/translate/file.rb
refinerycms-0.9.7.6 vendor/plugins/translate/lib/translate/file.rb