For the past few days I have been looking for a proper i18n (internationalisation) and l10n (localisation) method for PHP. PHP has quite a few locale aware functions such as strftime and sprintf. In combination with gettext this can work quite well. The major downside is that you need to install all the locales that you want to support on your server. Want to serve your websites in Russian? Then you need to generate a Russian locale system-wide. That won’t work when you’re on a shared hosting account somewhere.
PHP6 is promising to solve that problem with the intl functions. Even better, the intl functions are also available as a PECL extension and works on PHP 5.2.4 and newer. Debian Etch currently packages PHP 5.2.0 which is too old, but Lenny—Debian’s upcoming version—is up to PHP 5.2.6. There is no package (yet) for php5-intl in Debian Lenny but building and installing the extension yourself is really easy. Here’s a short tutorial.
First we need to install a couple of packages that allow us to build PECL extensions on Debian, and we need to install the build and runtime dependencies of the intl package (libicu38, libicu-dev and xsltproc).
- # apt-get install php5-dev dh-make-php fakeroot libicu38 libicu-dev xsltproc
The magic is in the dh-make-php package. It can download and configure a PECL extension for us, ready for building a Debian package. The following command tells dh-make-pecl to download and configure the intl extension.
- $ dh-make-pecl --depends libicu38 --build-depends libicu-dev --only 5 intl
This downloads the intl extension, configures it and adds libicu38 and libicu-dev as dependencies. The last option tells dh-make-pecl that we only want to build this extension for PHP5 and not for both PHP4 and PHP5 as is the default. Now we can build a binary Debian package.
- $ cd php-intl-1.0.0/
- $ dpkg-buildpackage -rfakeroot
Now the binary package will be built. If you have GPG configured then at the end you may get prompted (twice) for your GPG passphrase so that the files can be signed with your GPG signature. You should now have a .deb and various other files in the directory above.
- $ cd ..
- $ ls
- intl-1.0.0.tgz php-intl_1.0.0-1.dsc
- php5-intl_1.0.0-1_i386.deb php-intl_1.0.0-1_i386.changes
- php-intl-1.0.0 php-intl_1.0.0.orig.tar.gz
Now you can install the extension. If you have Apache running then you need to restart it. Note: You can't restart Apache gracefully with the apache2ctl graceful command. A graceful restart of Apache does not restart PHP so the extension will not be loaded. You need to restart Apache through the init.d script.
- # dpkg -i php5-intl_1.0.0-1_i386.deb
- Selecting previously deselected package php5-intl.
- (Reading database ... 177537 files and directories currently installed.)
- Unpacking php5-intl (from php5-intl_1.0.0-1_i386.deb) ...
- Setting up php5-intl (1.0.0-1) ...
- # /etc/init.d/apache2 restart
- Restarting web server: apache2 ... waiting .
That’s all! Now you can go play with the new intl functions. Happy coding!