Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it feasible to rely on setlocale, and rely on locales being installed?

I was trying to generate a localized date string with strftime, the placeholder I use is %x. The language/locale is setlocale(LC_ALL, array('jp','japanese')), however neither locale was available so it generated a string with improper characters. I then installed the ja_JP.utf8 locale and specified that as the first element in the array and the date formatting issue I had was resolved.

My question is, should I always rely on locales being installed? I'm aware of how to install them on boxes I have root access to, but what if I don't have access?

I believe Zend_Locale and Zend_Translate do not rely on setlocale at all but somehow do it internally, which gives me the impression that it isn't practically feasible for enterprise level applications.

I know I could probably use Zend_Locale and Zend_Translate in my application but it also needs to support PHP4, at least for another year which is why I can't solely rely upon those.

like image 488
meder omuraliev Avatar asked Dec 21 '09 19:12

meder omuraliev


People also ask

What does set locale mean?

The setlocale() function is used to set or query the program's current locale. If locale is not NULL, the program's current locale is modified according to the arguments. The argument category determines which parts of the program's current locale should be modified.

What is Setlocale in C?

The setlocale function installs the specified system locale or its portion as the new C locale. The modifications remain in effect and influences the execution of all locale-sensitive C library functions until the next call to setlocale .

What is locale PHP?

A "Locale" is an identifier used to get language, culture, or regionally-specific behavior from an API. PHP locales are organized and identified the same way that the CLDR locales used by ICU (and many vendors of Unix-like operating systems, the Mac, Java, and so forth) use.


2 Answers

If my examination of setlocale() is correct, the answer is: No and no. The range of installed locales varies, as does their name, and the availability of a certain locale ultimately cannot be predicted with total certainty.

like image 119
Pekka Avatar answered Oct 21 '22 05:10

Pekka


You can check the return value of setlocale and at least check that it was installed. Otherwise you will have silent failures:

setlocale(LC_ALL, 'en_US') or die('Locale not installed');
like image 28
Ariel Avatar answered Oct 21 '22 03:10

Ariel