In my environment (App Engine Standard PHP 5.5) I have a problem with the language of the dates. I need to show them in Spanish, but I can not make it work.
In my local environment it is translated without problems, but in production it shows the dates in English.
In my php.ini I already put this:
date.timezone = "America / Argentina / Cordoba"
extension = "intl.so"
And in the code where I need to show the dates in Spanish (Argentina) I tried this:
setlocale(LC_TIME, 'es_AR.UTF-8', 'es_AR', 'es-AR');
echo strftime("%d de %B", strtotime($event_date));
and in this way also:
setlocale(LC_TIME, 'es_ES.UTF-8', 'es_ES', 'es-ES');
echo strftime("%d de %B", strtotime($event_date));
Am I doing something the wrong way?
Thank you very much
Related
I am using this simple script to test on my host which runs on php version 5.6.
The script gives the output "Wednesday" instead off "Woensdag"(dutch)
Why is the day of the week still in English and not in Dutch?
Is this because the server is not correct configured?
<?php
/* Set locale to Dutch */
date_default_timezone_set("Europe/Amsterdam");
setlocale(LC_ALL, 'nl_NL');
echo date("l"); // output: Wednesday instead off "Woensdag" (dutch)
?>
Unfortunately date is not multilingual. If you want to format a language in another language you need to set locale (as you did in your example) and use strftime
Formatting options for strftime do result in "Woensdag" (or "mittwoch in German, etc):
setlocale(LC_TIME, 'en_EN');
echo strftime('%A', time()); // for a Wednesday will output: Wednesday
setlocale(LC_TIME, 'nl_NL');
echo strftime('%A', time()); // for a Wednesday will output: woensdag
Unless you want al lot of things to be influenced by setlocale you might want to specify that you're doing this for time only (as in the example above). Read on setlocale for more info: http://php.net/manual/en/function.setlocale.php
Keep in mind you need to have the locales available on your machine for this to work. Check with locale -a on linux. setlocale has a return value. If it is false something went wrong and you're most likely missing the specified locale.
I am currently developing a a Website that will be hosted on Microsoft Azure Websites. The client (as well as I) live in Austria and thus we would like to have the Austrian (German) locale for time and date - there is a small difference in the dates as we say "Jänner" instead of "Januar" for "January" and "Januar" sounds somewhat strange to an austrian. The standard snippet for returning a german date would be like so:
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_DE.utf8','de_DE', 'de_DE#euro', 'german');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
The answer to this question is acutlly fairly simple. To have austrian or swiss date formats, you can change the locales to the corresponding languages.
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_AT.utf8','de_AT', 'de_AT#euro', 'german_austria');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
for austrian
or
function ger_date($date){
date_default_timezone_set("Europe/Vienna");
setlocale (LC_ALL, 'de_CH.utf8','de_CH', 'de_CH#euro', 'german_switzerland');
$longdate = strftime('%d. %B %Y', strtotime($date));
return $longdate;
}
for swiss german. (note that there is not much difference in the regional format between german and switzerland)
But please note, that german_austria outputs Jänner, which sometimes can render as J�nner as the last locale ('german_austria', 'german' or 'german_switzerland') is not an UTF8 Locale and 'german-austria.utf8' is not available on my host.
If your system has no UTF-8 compatible locale installed and you can not install a nother one, you can simply wrap the strftime function into the utf8_encode() function from PHP and everything should work like a charm.
When using the PHP to return the timezone abbreviation with either date('T') or strftime('%Z') the result does not translate to the localized version of these abbreviations.
In french I have been told the following at the correct translations:
EDT == HAE
EST == HNE
I have tried the following code example:
<?php
setlocale(LC_ALL, 'fr_FR');
echo strftime('%Z');
echo date('T');
?>
All attempts produce EST/EDT rather than the translated versions.
I get the same on Mac and Linux.
I inspected the Gettext MO file for French, like this:
/usr/local/bin/msgunfmt /usr/share/locale/fr/LC_MESSAGES/texinfo.mo
This shows all the translations for things like months and days.
So you should get translations if you do:
echo strftime("%A %e %B %Y\n");
// gives "Vendredi 27 juin 2014"
But the translation file does not appear to contain translations for EST, or EDT - or in fact any others I looked for.
So I guess the answer is that these strings are simply not translated in the standard locale packages.
I've run locale -a on my server and can see that it's got Arabic locale settings installed:
ar_AE
ar_AE.iso88596
ar_AE.utf8
However, if I set the locale via:
$locale = array('ar_AE', 'ar_AE.iso88596', 'ar_AE.utf8', 'ar');
setlocale(LC_TIME, $locale);
and output it:
strftime('%A %d %B', $current_date)
The displayed date is in English, not Arabic.
Arabic is the only language this isn't working for: the site I'm working on is in 15 languages and all the others display a translated date.
What's going wrong?
This worked for me with no problems at all.
setlocale(LC_ALL, 'ar_AE.utf8');
If this does not work, then there is another code in your PHP file that interferes with the language.
In a template I display the day and month of a specific date :
<div class="jour"><?php echo date('d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo date('M',strtotime($content->getCreatedAt())) ?></div>
This works fine, problem is the month name is in English. Where do I specify that I want the month names in another locale, French for instance ?
Symfony has a format_date helper among the Date helpers that is i18n-aware. The formats are unfortunately badly documented, see this link for a hint on them.
default_culture only applies for the symfony internationalisation framework, not for native PHP functions. If you want to change this setting project wide, I would do so in config/ProjectConfiguration.class.php, using setlocale, and then use strftime rather than date:
// config/ProjectConfigration.class.php
setlocale(LC_TIME, 'fr_FR');
// *Success.php
<div class="jour"><?php echo strftime('%d',strtotime($content->getCreatedAt())) ?></div>
<div class="mois"><?php echo strftime('%b',strtotime($content->getCreatedAt())) ?></div>
Note that this requires locale settings to be enabled on your machine. To check, do var_dump(setlocale(LC_ALL, 'fr_FR')); If the result is false, you cannot use setlocale to do this and probably need to write the translation code yourself. Furthermore, you will need to have the correct locale installed on your system. To check what locales are installed, do locale -a at the command line.
Sorry for butting in so late in the day, but I would like to add my own thoughts here. The best international date format that I have come up with is "%e %b %Y", e.g. 9 Mar 2012. I find this much more readable than the ISO format "%Y-%m-%d", e.g. 2012-03-09. According to the docs, the %x format should be locale sensitive, but it does not work for me, at least not on the iPhone. This may be because Safari is not passing the locale in the HTML headers, I do not know.
It is sometimes useful to use an array with different possible values to setlocale().
Especially to support different environments (windows, linux, ...)
setlocale(LC_TIME, array('fr', 'fr_FR', 'fr_FR.utf8', 'french', 'french_FRANCE', 'french_FRANCE.utf8'));
echo strftime("%A %d %B", strtotime(date("Y-m-d")));
As the documentation states:
If locale is an array or followed by additional parameters then each array element or parameter is tried to be set as new locale until success. This is useful if a locale is known under different names on different systems or for providing a fallback for a possibly not available locale.