Sha256: d1a4e7e49e91c2318be70e2ff02927f34d60d37e40b343ffb9c4e71d85c09811

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'rchardet19'

require_relative 'file'
require_relative 'keyset'
require_relative 'strings_lexer'
require_relative 'strings_parser'

module Babelyoda
	class Strings < Keyset
	  attr_reader :filename
	  attr_reader :language
	  
	  def initialize(filename, language)
	    super(Babelyoda::Keyset.keyset_name(filename))
	    @filename, @language = filename, language
	  end

	  def read!
      raise ArgumentError.new("File not found: #{filename}") unless File.exist?(@filename)
      read
    end
    
    def read
      localized_filename = File.localized(@filename, @language)
      if File.exist?(localized_filename)
        File.open(localized_filename, read_mode) do |f|
          lexer = StringsLexer.new
          parser = StringsParser.new(lexer, @language)
          parser.parse(f.read) do |localization_key|
            merge_key!(localization_key)
          end        
        end
      end
      self
    end      

    def save!
      FileUtils.mkdir_p(File.dirname(filename))
      File.open(filename, "wb") do |f|
        keys.each_pair do |id, key|
          next unless key.values[language]
          f << "/* #{key.context} */\n" if key.context
          f << "\"#{id}\" = \"#{key.values[language].text}\";\n"
          f << "\n"
        end
      end
    end
    
    def self.save_keyset(keyset, filename, language)
      strings = self.new(filename, language)
      strings.merge!(keyset)
      strings.save!
    end
    
  private
            
    def read_mode
      cd = CharDet.detect(File.read(@filename))
      encoding_str = Encoding.aliases[cd.encoding] || cd.encoding
      encoding_str = 'UTF-8' if encoding_str == 'utf-8'
      encoding_str = 'UTF-8' if encoding_str == 'ascii'
      if (encoding_str != "UTF-8")
        "rb:#{encoding_str}:UTF-8"
      else
        "r"
      end
    end
          
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
babelyoda-2.0.2 lib/babelyoda/strings.rb