note 111311 modified in function.gettext by danbrown

From: Date: Tue, 05 Mar 2013 16:14:23 +0000
Subject: note 111311 modified in function.gettext by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-193938@lists.php.net to get a copy of this message
Just a quick note on locale names, since I just spent 2 days wondering why gettext did not translate anything: I configured the interesting locales on a debian 6 system, where I basically enabled the utf8 variants like ca_ES.utf8, es_ES.utf8, ... with dpkg-reconfigure locales Then, I tried setting the locale from the browsers accepted languages by <?php $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); ?> which yields for example "es_ES". Well, I am not interested in ISO-8859-1 output, so es_ES was not installed since it apparently is based on ISO-8859-1 and the recommendation is to install UTF-8 based locales only for new systems. Therefore setLocale(LC_MESSAGES, $locale) did not set a valid system locale in this case and so gettext still used system default locale. It did not help in any way that I kept the spanish messages in a generic language folder $localedir/es/LC_MESSAGES for all spanish variants. A quick workaround was to do the following: <?php $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); $lang = Locale::getPrimaryLanguage($locale); $region = Locale::getRegion($locale); $locale = $lang . '_' . $region . '.utf8'; Locale::setDefault($locale); setlocale(LC_ALL, $locale); setlocale(LC_MESSAGES, $locale); ?> Of course, checks for empty values have to be added. --was-- Just a quick note on locale names, since I just spent 2 days wondering why gettext did not translate anything: I configured the interesting locales on a debian 6 system, where I basically enabled the utf8 variants like ca_ES.utf8, es_ES.utf8, ... with dpkg-reconfigure locales Then, I tried setting the locale from the browsers accepted languages by $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); which yields for example "es_ES". Well, I am not interested in ISO-8859-1 output, so es_ES was not installed since it apparently is based on ISO-8859-1 and the recommendation is to install UTF-8 based locales only for new systems. Therefore setLocale(LC_MESSAGES, $locale) did not set a valid system locale in this case and so gettext still used system default locale. It did not help in any way that I kept the spanish messages in a generic language folder $localedir/es/LC_MESSAGES for all spanish variants. A quick workaround was to do the following: $locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']); $lang = Locale::getPrimaryLanguage($locale); $region = Locale::getRegion($locale); $locale = $lang . '_' . $region . '.utf8'; Locale::setDefault($locale); setlocale(LC_ALL, $locale); setlocale(LC_MESSAGES, $locale); Of course, checks for empty values have to be added. http://php.net/manual/en/function.gettext.php

« previous php.notes (#193938) next »