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, strict: true) ⇒ 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.

  • strict (Boolean) (defaults to: true)

    Whether to parse in strict mode.

Returns:

Raises:



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

def self.parse(io, strict: true)
  items = []

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

  File.new(items)
end

.parse_file(path, strict: true) ⇒ 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.

  • strict (Boolean) (defaults to: true)

    Whether to parse in strict mode.

Returns:

Raises:



71
72
73
74
75
# File 'lib/dotstrings/file.rb', line 71

def self.parse_file(path, strict: true)
  ::File.open(path, 'r') do |file|
    parse(file, strict: strict)
  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:



106
107
108
109
# File 'lib/dotstrings/file.rb', line 106

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

#==(other) ⇒ Object



173
174
175
# File 'lib/dotstrings/file.rb', line 173

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:



94
95
96
# File 'lib/dotstrings/file.rb', line 94

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'))


116
117
118
# File 'lib/dotstrings/file.rb', line 116

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


163
164
165
# File 'lib/dotstrings/file.rb', line 163

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

#delete(key) ⇒ Object

Deletes an item by key.



122
123
124
# File 'lib/dotstrings/file.rb', line 122

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.') }


131
132
133
134
# File 'lib/dotstrings/file.rb', line 131

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.



144
145
146
147
# File 'lib/dotstrings/file.rb', line 144

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

#empty?Boolean

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

Returns:

  • (Boolean)


169
170
171
# File 'lib/dotstrings/file.rb', line 169

def empty?
  @items.empty?
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/dotstrings/file.rb', line 177

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

#keysObject

Returns all keys in the file.



79
80
81
# File 'lib/dotstrings/file.rb', line 79

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

#lengthObject

Returns the number of items in the file.



151
152
153
# File 'lib/dotstrings/file.rb', line 151

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.



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

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