Tuesday, October 5, 2010

Find unused localized messages

I am working for company where refactoring is foul term. Died code is never removed etc. Fortunately modern IDEs (e.g. Eclipse) have enough search capabilities. Nevertheless problem of unused localized messages is very serious. There is no compiler that shows you “unused” warning. Many unused sentences are stored in *.prooperties files and must be handled, translated to new languages etc.
I wrote very simple UNIX shell script that finds such messages. I
f you have a bad luck (like me) to work on Windows, do not worry. You can always run it using Cygwin (as I do).
I work in Java, so script searches for *.java and *.jsp files. But it can be easily modified.

#!/bin/sh

###########################################################
# This script searches source code for unesed strings and prints them.
# It assumes that strings are stored properties files and code is in *.java or *.jsp files.
###########################################################
if [ "$#" -ne 2 ]; then
echo Usage: $0 RESOURCE_FILES_PATTERN SRC_CODE_ROOT_DIRECTORY
echo e.g. $0 “MyProject/resources/Messages*.properties” “MyProject/src”
exit 0;
fi
texts=$1
src=$2
for line in `(ls $texts | while read file; do cat $file; done;) | sed ‘s/#.*//’ | sed ‘s/=.*//’ | sed ‘s/ //’ | egrep -v “^$” | sort -u`; do
found=false
for file in `find $src -name *.java -or -name *.jsp`; do
grep “\”$line\”" $file >/dev/null
if [ "$?" == "0" ]; then
found=true
continue
fi
done;
if [ $found == false ]; then
echo $line is not found
fi
done;

This post was previously published at alexr.blog.com

1 comment:

  1. Hi Alex! To better localize strings, I would suggest trying an online localization tool like https://poeditor.com/. It has an intuitive and friendly design, useful features such as automatic translation, API, GitHub integration, making the localization process smoother for users. Maybe it is useful.

    ReplyDelete