setlocale

Small little annoyance we found in the process of internationalizing the site for European locales.

setlocale(LC_ALL, 'es_ES.UTF8'); // Spanish from spain
$a = 1.2;
echo 'hi'.$a;

This prints hi1,2. Take out the first line and it prints hi1.2 as you might expect.

Not a big deal right? Well imagine if that is the index into a cache key of an object? Now when someone from Spain views the page, it thinks those objects aren’t in cache. Big mess ensues.

I noticed that I suggested object versioning to be integers in the documentation, but some of us were using floats and I wasn’t type-checking. Whoops!

Our solution is to only set the locale when rendering output from a template and then setting it back. It’s ugly and I’m a definite fan of using integers only for version numbers in my next life.

Possesives

In a unrelated note, the people at work had a long discussion of how to render possessives when internationalizing. The key in the U.S. is to stop being clever and follow the first rule of the Elements of Style. This way it’s just,

printf(_('%s’s pictures'), htmlspecialchars($display_name));

Hopefully, the long argument I had trying to convince someone to do this will be returned in less neutered quotation marks on the site. I’m not holding my breath.

2 thoughts on “setlocale

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.