Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find duplicate keys in localizable.strings files automatically?

Tags:

xcode

When using localizable.strings with many entries in your XCode project you sooner or later may use a key more than once. Is it possible to let XCode find that case and issue a warning about it?

Apple's Resource Programming mentions the genstrings tool, but usually you don't use that yourself in XCode. So, how can I let XCode detect duplicate keys in such files without manually running genstrings?

Bounty note: to earn the bounty a solution must fully integrate with XCode if it uses external resources like scripts, that is, it must work with input files given in XCode, mark a build as fail in case of duplicates and must not trigger for false positives like empty lines or comments.

like image 876
Mike Lischke Avatar asked Jun 09 '14 16:06

Mike Lischke


1 Answers

cut -d' ' -f1 Localizable.strings | sort | uniq -c

type this command in the terminal and you get a list saying how often each key is used.

use -d instead of -c and you only get duplicates

the script:

#!/bin/bash

c=`expr $SCRIPT_INPUT_FILE_COUNT - 1`
for i in $(seq 0 $c)
    do

    var="SCRIPT_INPUT_FILE_$i"
    FILENAME=${!var}

    DUPES=`cut -d' ' -f1 "$FILENAME" | sort | uniq -d`

    while read -r line; do
        if [[ $line == "\""* ]] ;
        then
            echo "warning: $line used multiple times -"
        fi
    done <<< "$DUPES"
done

screenshot

like image 186
Daij-Djan Avatar answered Oct 12 '22 17:10

Daij-Djan