Class: DotStrings::File

Inherits:
Object
  • Object
show all
Defined in:
lib/dotstrings/file.rb

Overview

Represents a .strings file.

It provides methods to parse .strings, as well as methods for accessing and manipulating localized string items.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = []) ⇒ File

Returns a new instance of File.



18
19
20
# File 'lib/dotstrings/file.rb', line 18

def initialize(items = [])
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

All items in the file.



16
17
18
# File 'lib/dotstrings/file.rb', line 16

def items
  @items
end

Class Method Details

.parse(io) ⇒ DotStrings::File

Parses a file from the given IO object.

Examples:

io = Zlib::GzipReader.open('path/to/en.lproj/Localizable.strings.gz')
file = DotStrings::File.parse(io)

Parameters:

  • io (IO)

    The IO object to parse.

Returns:

Raises:



50
51
52
53
54
55
56
57
58
# File 'lib/dotstrings/file.rb', line 50

def self.parse(io)
  items = []

  parser = Parser.new
  parser.on_item { |item| items << item }
  parser << normalize_encoding(io.read)

  File.new(items)
end

.parse_file(path) ⇒ DotStrings::File

Parses the file at the given path.

Examples:

file = DotStrings::File.parse_file('path/to/en.lproj/Localizable.strings')

Parameters:

  • path (String)

    The path to the file to parse.

Returns:

Raises:



69
70
71
72
73
# File 'lib/dotstrings/file.rb', line 69

def self.parse_file(path)
  ::File.open(path, 'r') do |file|
    parse(file)
  end
end

Instance Method Details

#<<(item) ⇒ DotStrings::Item

Appends an item to the file.

Examples:

file << DotStrings::Item.new(key: 'button.title', value: 'Submit')

Parameters:

Returns:



104
105
106
107
# File 'lib/dotstrings/file.rb', line 104

def <<(item)
  @items << item
  self
end

#==(other) ⇒ Object



171
172
173
# File 'lib/dotstrings/file.rb', line 171

def ==(other)
  eql?(other)
end

#[](key) ⇒ DotStrings::Item

Returns an item by key, if it exists, otherwise nil.

Examples:

item = file['button.title']
unless item.nil?
  puts item.value # => 'Submit'
end

Parameters:

  • key (String)

    The key of the item to return.

Returns:



92
93
94
# File 'lib/dotstrings/file.rb', line 92

def [](key)
  @items.find { |item| item.key == key }
end

#append(item) ⇒ Object

Appends an item to the file.

Examples:

file.append(DotStrings::Item.new(key: 'button.title', value: 'Submit'))


114
115
116
# File 'lib/dotstrings/file.rb', line 114

def append(item)
  self << item
end

#count(&block) ⇒ Object

Returns the number of items in the file.

If a block is given, it will count the number of items for which the block returns true.

Examples:

file.count # => 10
file.count { |item| item.key.start_with?('button.') } # => 3


161
162
163
# File 'lib/dotstrings/file.rb', line 161

def count(&block)
  @items.count(&block)
end

#delete(key) ⇒ Object

Deletes an item by key.



120
121
122
# File 'lib/dotstrings/file.rb', line 120

def delete(key)
  @items.delete_if { |item| item.key == key }
end

#delete_if(&block) ⇒ Object

Deletes all items for which the block returns true.

Examples:

file.delete_if { |item| item.key.start_with?('button.') }


129
130
131
132
# File 'lib/dotstrings/file.rb', line 129

def delete_if(&block)
  @items.delete_if(&block)
  self
end

#each(&block) ⇒ Object

Calls the given block once for each item in the file.

Examples:

file.each do |item|
  puts "#{item.key} > #{item.value}"
end

Parameters:

  • block (Proc)

    The block to call.



142
143
144
145
# File 'lib/dotstrings/file.rb', line 142

def each(&block)
  @items.each(&block)
  self
end

#empty?Boolean

Returns true if the file doen’t contain any items.

Returns:

  • (Boolean)


167
168
169
# File 'lib/dotstrings/file.rb', line 167

def empty?
  @items.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/dotstrings/file.rb', line 175

def eql?(other)
  other.is_a?(self.class) && @items.eql?(other.items)
end

#keysObject

Returns all keys in the file.



77
78
79
# File 'lib/dotstrings/file.rb', line 77

def keys
  @items.map(&:key)
end

#lengthObject

Returns the number of items in the file.



149
150
151
# File 'lib/dotstrings/file.rb', line 149

def length
  @items.length
end

#sort(&block) ⇒ Object

Returns a new File with the items sorted using the given comparator block.

If no block is given, the items will be sorted by key.



26
27
28
29
# File 'lib/dotstrings/file.rb', line 26

def sort(&block)
  new_file = dup
  new_file.sort!(&block)
end

#sort!(&block) ⇒ Object

Sort the items using the given block.

If no block is given, the items will be sorted by key.



35
36
37
38
# File 'lib/dotstrings/file.rb', line 35

def sort!(&block)
  @items.sort!(&block || ->(a, b) { a.key <=> b.key })
  self
end

#to_s(escape_single_quotes: false, comments: true) ⇒ Object

Serializes the file to a string.

Parameters:

  • escape_single_quotes (Boolean) (defaults to: false)

    whether to escape single quotes.

  • comments (Boolean) (defaults to: true)

    whether to include comments.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/dotstrings/file.rb', line 184

def to_s(escape_single_quotes: false, comments: true)
  result = []

  @items.each do |item|
    result << item.to_s(
      escape_single_quotes: escape_single_quotes,
      include_comment: comments
    )

    result << ''
  end

  result.join("\n")
end