Reputation: 41
I have to translate my website (coded in PHP) to another language. I have been using Drupal and its functions to handle this, but there is still some hardcoded strings spread out.
Do you know a script or a software that would help me locate them in the whole source code directory ? The best would be that it also allows me to change them.
Upvotes: 4
Views: 1980
Reputation: 95354
See Is there a semi-automated way to perform string extraction for i18n? for an automated way to handle this problem. While the linked-to answer is specific to Java, the same engine/scheme would work fine for PHP.
If that's too much, our Source Code Search Engine (SCSE) can identify these trivially, leaving you to decide what to do about each string.
The SCSE is a kind of super-grep that operates off the language structure rather than raw character text. Queries are stated in terms of language tokens rather than strings. The SCSE indexes all the tokens of teh many files of a source code base, and then uses that index to optimize searches. Because it uses langauge-accurate lexers, it understands the precise bounds of strings, comments, numbers, keywords, and whitespace. Queries are (language)whitesphace insensitive. So, to find all the places where a an identifier which has "foo" in the middle is assigned a constant, one can write a query:
I=*foo* '=' N
where the query say, "find I(dentifiers) with a wildcard constraint, followed by language token =, followed by any N(number).
This is all relvant for finding all string literals in your program with the trivial query:
S
which means means, well, find all literal S(trings) without any constraints regardless of type and syntax (PHP has many types of string literals).
The resulting hits can be displayed in a UI, but for OP's purpose, one can turn on logging and have the tool provide the hits and thier locations by precise file and line number.
The SCSE has scanners for PHP and many other languages.
Upvotes: 1
Reputation: 60276
A couple options come to mind:
If you are using an IDE, such as Eclipse, there is often a search feature that will allow you to search all of your source code. In Eclipse you can go to 'Search' -> 'File'.
If you aren't using an IDE with a search feature, but you have access to the command-line on a unix-based server, you can use recursive grep. For example:
grep -r "sometext" /path/to/code
If you want to do a recursive find-and-replace from the command line, you can use a combination of find and sed:
find /path/to/code -type f -print0 | xargs -0 sed -i 's/sometext/newtext/g'
Upvotes: 0