Search code examples
phpgettext

Converting echo calls to gettext calls


I'm in charge of a project that needs to be *i18n*ed. The only problem is that the original developer didn't think of this when writing the code so all the strings are just "hard-coded".

It's quite a mess, I've been looking the code and found all these cases:

  • Strings in echo calls:

    echo "I'm a string";

  • Strings in echo calls and mixed with HTML:

    echo "<div><span href=''>I'm a string</span></div>";

  • Strings in non-PHP (plain HTML) portions of the code.

  • Strings in echo calls with PHP variables inside:

    echo "There are $number items in this list";

My question is: do you know of any program that could automate a bit the tedious work of converting all these into gettext _() calls?

The project is quite big and I'm thinking of writing it myself (I'm sure I'll face this problem again in the future) but I don't want to reinvent the wheel.

Thanks a lot!


Solution

  • You could use regular expressions and sed to replace at least some of those echo messages. But keep in mind that you can break more than you wanted to fix when you use automatic text replacement.