Class: Danger::DangerDuplicateLocalizableStrings

Inherits:
Plugin
  • Object
show all
Defined in:
lib/duplicate_localizable_strings/plugin.rb

Overview

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

Examples:

Checks whether there are duplicate entries in Localizable.strings


check_localizable_duplicates

Instance Method Summary collapse

Instance Method Details

#check_localizable_duplicatesvoid

This method returns an undefined value.

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



78
79
80
81
# 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_entriesArray of duplicate Localizable.strings entries

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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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

This method returns an undefined value.

Prints passed duplicated entries.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/duplicate_localizable_strings/plugin.rb', line 56

def print_duplicate_entries(duplicate_entries)
  message = "#### Found duplicate entries in Localizable.strings files \n\n"

  message << "| File | Key |\n"
  message << "| ---- | --- |\n"

  duplicate_entries.each do |entry|
    file = entry['file']
    key = entry['key']

    message << "| #{file} | #{key} | \n"
  end

  markdown message
end