A client recently decided to display his website to multiple audiences divided by language segments. The client's application was an e-commerce application, using gettext with php and some simple linux commands we were able to create a dictionary of terms and translate them using php-gettext.
From the beginning, we needed to wrap items that we planned to translate inside of the get text function, for simplicity, the function call was similar to this:
echo _("some text that you would like to translate");
After wrapping all the dynamic pieces of the site, we then utilized the following command to search for php files within our installation
find . -iname "*.php" > files.tmp
Next we parsed the files.tmp file looking for the data within the php files that we indicated above. We're seeking to find all occurances of a function call beginning with _ and grab the text content with in the call.
xgettext --language=PHP --indent --keyword=_ --keyword=_e --keyword=_ngettext:1,2 -s -n --from-code=UTF8 -f files.tmp
This creates a messages.po dictionary file in your current working directory with all of your function calls, and their translations.
Finally, we compiled the messages.po file, after editing and assigning the language translations we desired, using the following command.
msgfmt -o messages.mo messages.po
Next we began the data structure which gettext requires to parse compiled data files.
Created directory locale at the document root.
Inside of locale we created our language directory, in this case es_US.
Inside of es_US we created the gettext required LC_MESSAGES directory.
Finally we moved messages.mo and messages.po from our document root to /locale/es_US/LC_MESSAGES and set the appropriate read permissions
Language Localization with PHP
Next we created an include file which would dynamically detect the users desired language, in this case Spanish
Then we realized all we needed to do was add some session support and we were done.
gettext is a simple way to provide for a dictionary based language translator with php and Linux.
If you would like us to help you set up this type of service on your website, just contact us.
Sidenote: You must replace "CHARSET" with "UTF-8" after doing the extract. dxgettext does this automatically, but xgettext doesn't.