Sha256: 52a709a061774d0fe166bf0e4e2bdc7010e2fccd0bb93aae920c4873a971c130

Contents?: true

Size: 1.9 KB

Versions: 37

Compression:

Stored size: 1.9 KB

Contents

class TranslationSupport
  class << self
    # Retrieve US word set
    def get_translation_keys(language_root, suffix = nil)
      (dummy_comments, words) = read_file("#{language_root}/en#{suffix}.yml", 'en')
      words
    end

    # Retrieve comments, translation data in hash form
    def read_file(filename, basename)
      (comments, data) = IO.read(filename).split(/\n#{basename}:\s*\n/) # Add error checking for failed file read?
      [comments, create_hash(data, basename)]
    end

    # Creates hash of translation data
    def create_hash(data, _basename)
      words = Hash.new
      return words if !data

      parent = Array.new
      previous_key = 'base'
      data.split("\n").each do |w|
        next if w.strip.blank?

        (key, value) = w.split(':', 2)
        value ||= ''
        shift = (key =~ /\w/) / 2 - parent.size # Determine level of current key in comparison to parent array
        key = key.sub(/^\s+/, '')
        parent << previous_key if shift > 0 # If key is child of previous key, add previous key as parent
        (shift * -1).times { parent.pop } if shift < 0 # If key is not related to previous key, remove parent keys
        previous_key = key # Track key in case next key is child of this key
        words[parent.join(':') + ':' + key] = value unless key.blank?
      end
      words
    end

    def open_available_tags(filename)
      data = YAML::load(File.open(filename.to_s))
      data.to_s
    end

    # Writes to file from translation data hash structure
    def write_file(filename, basename, comments, words)
      File.open(filename, 'w') do |log|
        log.puts(comments + "\n" + basename + ": \n")
        words.sort.each do |k, v|
          keys = k.split(':')
          (keys.size - 1).times { keys[keys.size - 1] = '  ' + keys[keys.size - 1] } # Add indentation for children keys
          log.puts(keys[keys.size - 1] + ':' + v + "\n")
        end
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
trusty-cms-5.3.5 lib/translation_support.rb
trusty-cms-5.3.4 lib/translation_support.rb
trusty-cms-5.3.3 lib/translation_support.rb
trusty-cms-5.3.2 lib/translation_support.rb
trusty-cms-5.3.1 lib/translation_support.rb
trusty-cms-5.3 lib/translation_support.rb
trusty-cms-5.2.3 lib/translation_support.rb
trusty-cms-5.2.2 lib/translation_support.rb
trusty-cms-5.2.1 lib/translation_support.rb
trusty-cms-5.2.0 lib/translation_support.rb
trusty-cms-5.1.0 lib/translation_support.rb
trusty-cms-5.0.9 lib/translation_support.rb
trusty-cms-5.0.7 lib/translation_support.rb
trusty-cms-5.0.6 lib/translation_support.rb
trusty-cms-5.0.5 lib/translation_support.rb
trusty-cms-5.0.4 lib/translation_support.rb
trusty-cms-5.0.3 lib/translation_support.rb
trusty-cms-5.0.2 lib/translation_support.rb
trusty-cms-5.0.1 lib/translation_support.rb
trusty-cms-4.3.5 lib/translation_support.rb