Parrot Time Magazine

The Thinking of Speaking
Issue #9 May / June 2014
GlobTech
Using Locale

Using Locale

by Erik Zidowecki
May / June 2014 |  asd

One of the annoying issues when dealing in a global environment like the internet is trying to present people in multiple countries with data in their regional format. There are different ways of displaying time (AM/PM vs 24 hour), dates (month / day / year or day / month / year), numbers (what the roles of periods and commas are) and language for days and months.

The last of these can become the most difficult on a multilingual website. If you are trying to display a calendar or a date that contains the names of the days and months rather than just numbers, you are faced with the need to create those terms in many languages.

For example, if the date is "Tuesday, May 20th", how would you display that for someone who speaks Italian (given that you know to give the page to them in Italian). You could store all the days of the week for Italian (gennaio, febbraio, marzo, aprile, maggio, giugno, luglio, agosto, settembre, ottobre, novembre, dicembre) and the months (lunedì, martedì, mercoledì, giovedì, venerdì, sabato, domenica) so they could be displayed properly. You would then have to do this for every other language you want the site to be in.

This can become more frustrating if you are printing a small calendar and need the shorter versions of these words, or just want to use the condensed forms normally. For example, in English, we would use "Tue, Mar 30th". It isn't always just using the first few letters, so we can't simply truncate our words.

There is a solution when using PHP to deliver this information. Rather than storing all these words and versions in arrays, then figure out how to put them in properly when we call the strftime() function (which returns the date and time in a number of formats). We can use the PHP "setlocale" function.

Essentially, you set the locale to affect a specific kind of information that PHP might retrieve for you, like the date or time, and the language you want it in. The language is passed using a particular encoding scheme, usually a two character code for a language, an underscore, and a two character code for the country it is spoken in.

The country part is necessary because there might be different formats used for the same language. For example, English could be encoded using en_US, en_GB, or en_CA, with those being for the United States, Great Britain, and Canada, respectively. Similarly, German could use de_DE, de_CH, de_AT, or de_LU (Germany, Switzerland, Austria and Luxembourg). The extension ".utf8" should be used to accommodate for any non-latin based characters.

You can determine what will be affected by the language selection by using these constants:

LC_ALL for all of the below
LC_COLLATE for string comparison
LC_CTYPE for character classification and conversion, like strtoupper()
LC_MONETARY for localeconv()
LC_NUMERIC for decimal separator (also localeconv())
LC_TIME for date and time formatting with strftime()

So, if for example you wish to set the date to output in Swiss German, you would use
setlocale(LC_TIME, de_CH.utf8)

Then
print strftime('%A, %B %e, %Y',time());

Gives us
Dienstag, Mai 20, 2014

Changing the codes, with it_IT.utf8 we get martedì, maggio 20, 2014
and with ja_JP.utf8, we get 火曜日, 5月 20, 2014

You can find a list of many of these language codes here:
http://red-route.org/code/php-international-language-and-locale-codes-demonstration

And there you are! A quick way to properly display dates for many languages. You can experiment with the other settings, like string conversions and currency on your own. Have fun!

GlobTech - Using Locale
Writer: Erik Zidowecki
Images:
Petey: world map

All images are Copyright - CC BY-SA (Creative Commons Share Alike) by their respective owners, except for Petey, which is Public Domain (PD) or unless otherwise noted.

Searching for language resources?
Find entertaining and educational books for learning a language at Scriveremo Publishing. Just click the link below to find learning books for more than 30 languages!
Parleremo Languages Word Search Puzzles Italian - Volume 1



Also in this issue




Others like this

Comments

comments powered by Disqus