class Danger::DangerDuplicateLocalizableStrings

Simple plugin that checks for duplicate entries in changed Localizable.strings files inside iOS and Mac projects.

@example Checks whether there are duplicate entries in Localizable.strings

check_localizable_duplicates

@tags localization, cocoa

Public Instance Methods

check_localizable_duplicates() click to toggle source

Checks whether there are any duplicated entries in all Localizable.strings files and prints out any found duplicates.

@return [void]

# File lib/duplicate_localizable_strings/plugin.rb, line 78
def check_localizable_duplicates
  entries = localizable_duplicate_entries
  print_duplicate_entries entries unless entries.empty?
end
localizable_duplicate_entries() click to toggle source

Returns an array of all detected duplicate entries. An entry is represented by a has with file path under ‘file’ key and the Localizable.strings key under ‘key’ key.

@return [Array of duplicate Localizable.strings entries]

# File lib/duplicate_localizable_strings/plugin.rb, line 20
def localizable_duplicate_entries
  localizable_files = (git.modified_files + git.added_files) - git.deleted_files
  localizable_files.select! { |line| line.end_with?('.strings') }

  duplicate_entries = []

  localizable_files.each do |file|
    lines = File.readlines(file)

    # Grab just the keys, translations might be different
    keys = lines.map { |e| e.split('=').first }
    # Filter newlines and comments
    keys = keys.select do |e|
      e != "\n" && !e.start_with?('/*') && !e.start_with?('//')
    end

    # Grab keys that appear more than once
    duplicate_keys = keys.select { |e| keys.rindex(e) != keys.index(e) }
    # And make sure we have one entry per duplicate key
    duplicate_keys = duplicate_keys.uniq

    duplicate_keys.each do |key|
      duplicate_entries << { 'file' => file, 'key' => key }
    end
  end

  duplicate_entries
end
print_duplicate_entries(duplicate_entries) click to toggle source

Prints passed duplicated entries. @param [Hash] duplicate_entries

A hash of `[file => keys]` entries to print.

@return [void]