I have a cakephp 2.9 multilanguage app and using .po files for translating strings like __('example'). So far so good but I've been trying for a while now to translate this date from:
Fri, Aug 31st 2018, 16:03
to:
Vie, Ago 31 2018, 16:03
This is my code:
setlocale("LC_ALL", "es_ES.UTF-8");
App::uses('CakeTime', 'Utility');
echo CakeTime::nice();
I've downloaded the spanish files from the localized repo (https://github.com/cakephp/localized) and put them into app\Locale\spa\LC_TIME and still the date keeps showing in english.
Can someone help me? Maybe it's the wrong approach?
Well, didn't took me long after I posted the question to realize all the ways I have tried were ok, using caketime or strftime directly, the problem was with the setlocale:
Changed this:
setlocale("LC_ALL", "es_ES.UTF-8");
to this:
setlocale(LC_TIME, 'es_ES.utf8','esp');
So my two results are:
echo ucfirst(strftime('%A '.$number_of_the_day.' %B %Y'));
//Viernes 31 agosto 2018
echo __('Today is ').CakeTime::nice();
//Hoy es vie, ago 31st 2018, 16:29
It worked like a charm, however I'd very much like a table where all this language codes are explained so we can use the right syntax for each and not guess, just like I did.
Related
I am trying to output the current date with different languages. I have used the following code to echo the date 2022-01-01 in Swedish natural language:
<?php
setlocale(LC_ALL, 'sv_SE.UTF-8');
echo strftime("%A %e %B %Y", mktime(0, 0, 0, 1, 1, 2022)) . "<br>";
?>
When this code is run on my web server it shows the expected output in Swedish:
lördag 1 januari 2022
But when testing on my localhost it displays the date is written in English:
Saturday 1 January 2022
I am running Apache on my localhost. I guess that I need to configure Apache or PHP locally to make this work. But I am not sure how or where. Any suggestions?
I found an answer now. It seems that Microsoft uses other locale strings for this. For Swedish it shall be "sve_SWE" instead of "sv_SE".
Read more: MS Language strings, and MS Country/Region strings
So, for a Windows version of localhost it should be:
setlocale(LC_ALL, 'sve_SWE.UTF-8');
I'm a noob (I confess it) and can't manage to find a solution to my problem.
I'm setting up the date format on Drupal and it uses the PHP date format.
Right now it's "d F Y", so it appears as 07 Dicembre 2021 (in Italian), but in Italian months are written out in lowercase. Is there a way to transform Dicembre into dicembre? I couldn't find a proper way.
Thanks for your help!
Using strtolower() the output will be as you expected:
<?php echo strtolower(date("M"),2);?>
So to implement it only for Month, simply break the date format into 3 parts. Day , Month, Year.
Use drupal module:
https://www.drupal.org/project/simple_field_formatter
Go to:
/admin/structure/types/manage/[YOURTYPE]/display
For your date field, click the gearwheel on the right (format settings) and activate the checkbox for strtolower
Or create your own FieldFormatter: https://www.webwash.net/how-to-create-a-custom-field-formatter-in-drupal-8/
You should use the locale aware strftime, which formats according to the current locale.
setlocale(LC_TIME, "it_IT");
echo strftime("%d %B %Y"); // 07 dicembre 2021
Got a bit of an odd question. After creating a PHP script for a friend who lives in Germany, he has decided he needs the time/date in German format, an example being:
Montag, 16.März 2014
Is there a date/time function that can do this in PHP?
Any help much appreciated, Thanks!
Use strftime() and set the locale to German
Example
<?php
setlocale(LC_ALL, "de_DE.utf8");
echo strftime("%A, %e.%B %Y");
?>
Which would output: Donnerstag, 7.August 2014
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 use CakeTime::niceshort() and it works very well. I set correct locale values and set translate (po) files and I can see localized month, day names successfully.
But one thing is missing. When I use niceshort():
echo $this->Time->niceShort(1387120620);
I get this:
Ara 15th 2013, 15:17
But this isn't valid for Turkish dates. I need this:
Ara 15 2013, 15:17
It is possible with these:
$this->Time->format($time,"%b %e %Y, %H:%M")
But I don't want to lose niceShort's features. It gives short information if timestamp is close to now:
https://github.com/cakephp/cakephp/blob/2.5/lib/Cake/Utility/CakeTime.php#L391
Is it possible to remove "th" and "nd" from dates without changing core lib ?
You should use setlocale before calling CakeTime::niceshort()
setlocale("LC_ALL", "tr_TR.UTF-8");
You need to make sure they are properly installed on the server.