Sha256: 76d456b20e95426e583a14d4b37241c9cf341ed4c258c08040cc92f6d05b4162

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'yaml'
require 'psych'

module MissingTranslation
  class TranslationFile
    attr_accessor :pathname, :content, :group, :language, :translations

    def initialize(pathname = nil)
      return unless pathname

      @pathname = pathname
      @file = File.open(pathname)
      if @file
        @content = (YAML.load(@file.read) || {})
      end
    end

    def to_json
      return nil unless @content

      @content.to_hash
    end

    def group
      return @group if @group

      groups = pathname.split('/').last.gsub('.yml', '').split('.')
      return '' unless groups.size > 1

      @group = groups.first
    end

    def language
      return @language if @language

      @language = pathname.split('/').last.gsub('.yml', '').split('.').last
    end

    def write(directory)
      return nil unless translations

      result = Psych.parse_stream translations.to_yaml
      result.grep(Psych::Nodes::Scalar).each do |node|
        node.plain  = false
        node.quoted = true
        node.style = Psych::Nodes::Scalar::DOUBLE_QUOTED
      end
      result.grep(Psych::Nodes::Mapping).each do |node|
        node.children.each_slice(2) do |k, _|
          k.plain  = true
          k.quoted = false
          k.style  = Psych::Nodes::Scalar::ANY
        end
      end
      filename = [group, language, "yml"].compact.reject{|s| s.size <= 0}.join('.')

      p "Writing file #{directory}/#{filename}..."
      File.write("#{directory}/#{filename}", result.yaml)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
missing_translation-0.2.0 lib/missing_translation/translation_file.rb
missing_translation-0.1.0 lib/missing_translation/translation_file.rb