Class: Babelyoda::Strings

Inherits:
Keyset
  • Object
show all
Defined in:
lib/babelyoda/strings.rb

Instance Attribute Summary collapse

Attributes inherited from Keyset

#keys, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Keyset

#drop_empty!, #empty?, #ensure_languages!, keyset_name, #merge!, #merge_key!, parse_xml, #to_s, #to_xml

Constructor Details

#initialize(filename, language) ⇒ Strings

Returns a new instance of Strings.



13
14
15
16
# File 'lib/babelyoda/strings.rb', line 13

def initialize(filename, language)
  super(Babelyoda::Keyset.keyset_name(filename))
  @filename, @language = filename, language
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



10
11
12
# File 'lib/babelyoda/strings.rb', line 10

def filename
  @filename
end

#languageObject (readonly)

Returns the value of attribute language.



11
12
13
# File 'lib/babelyoda/strings.rb', line 11

def language
  @language
end

Class Method Details

.save_keyset(keyset, filename, language) ⇒ Object



48
49
50
51
52
# File 'lib/babelyoda/strings.rb', line 48

def self.save_keyset(keyset, filename, language)
  strings = self.new(filename, language)
  strings.merge!(keyset)
  strings.save!
end

Instance Method Details

#readObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/babelyoda/strings.rb', line 23

def read
  if File.exist?(@filename)
    File.open(@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

#read!Object

Raises:

  • (ArgumentError)


18
19
20
21
# File 'lib/babelyoda/strings.rb', line 18

def read!
  raise ArgumentError.new("File not found: #{filename}") unless File.exist?(@filename)
  read
end

#save!Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/babelyoda/strings.rb', line 36

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