DotStrings

A parser for Apple strings files (.strings) written in Ruby. Some of the features of DotStrings include:

  • A fast and memory-efficient streaming parser.
  • Support for multiline (/* ... */) comments as well as single-line comments (// ...).
  • An API for creating strings files programmatically.
  • Handles Unicode and escaped characters.
  • Helpful error messages: know which line and column fail to parse and why.

Installing

You can install DotStrings manually by running:

$ gem install dotstrings

Or by adding the following entry to your Gemfile, then running $ bundle install.

gem "dotstrings"

Usage

You can load .strings files using the DotString.parse() utility method. This method returns a DotStrings::File object or raises an exception if the file is invalid.

file = DotStrings.parse_file('en-US/Localizable.strings')
file.items.each do |item|
  puts item.comment
  puts item.key
  puts item.value
end

Examples

Listing keys

puts file.keys
# => ["key 1", "key 2", ...]

Accessing items by key

puts file['key 1'].value
# => "value 1"

Deleting items by key

file.delete('key 1')

Appending items

file << DotStrings::Item(
  comment: 'Title for the cancel button',
  key: 'button.cancel.title',
  value: 'Cancel'
)

Saving a file

File.write('en-US/Localizable.strings', file.to_s)