ilovett

Disabling i18n in CakePHP

January 2nd, 2010

I ran into an unusual problem in CakePHP recently. A site I'm working on uses CakePHP's internationalization and localization features unnecessarily. The codebase is full of references to the __() function, but they're pointless because the site isn't actually multi-lingual. So I have this capability that I don't necessarily want right now, but getting rid of it isn't trivial and at some point I may have a need for it.

Just about everything comes with a performance cost, and this feature is no exception. Using webgrind, I could see the I18n class sucking up time and resources even though it was doing a whole lot of nothing. I wanted to correct that.

The solution was to create a stub class. If CakePHP sees that a class named I18n has not been loaded, it will attempt to load it. I could have prevented that from happening by editing the file, but that would complicate future upgrades. Loading a stub class which has the same interface but none of the bloat avoids that.

For lack of a better place, I put my stub class in app/vendors/i18n.php and loaded it from the AppController constructor. It contains a single function, translate(), which returns its first argument unmodified. Ordinarily that would be dumb, but in this case it's an optimization.

Webgrind still shows the same number of invocations for the I18n class, but they're taking much less time.