Welcome Guest, Not a member yet? Register   Sign In
Add date format to Language
#1

(This post was last modified: 12-04-2021, 04:46 AM by Danilo.)

CodeIgniter 4.1.5

If I set:
PHP Code:
app\Config\App.php
$defaultLocale 
'it';  

With this code:

PHP Code:
use CodeIgniter\I18n\Time;
$miaData Time::create(2021731235930); 
echo 
"<br>" $miaData->toFormattedDateString(); 

OUTPUT:
Quote:lug 31, 2021


This is terrifying to see.
The month has been translated into Italian, but the format is in English (month day year).
But in Italian the date is written: day month year.



This is my suggestion.

system\Language\en\Time.php
ADD
PHP Code:
//Format strings by date and time
'dateTimeFormat'        => 'MM/dd/yyyy HH:mm:ss',
'dateFormat'            => 'MM/dd/yyyy',
'timeFormat'            => 'HH:mm:ss',
'formattedDateFormat'  => 'MMM d, yyyy'


system\Language\it\Time.php
ADD
PHP Code:
//Format strings by date and time
'dateTimeFormat'        => 'dd/MM/yyyy HH:mm:ss',
'dateFormat'            => 'dd/MM/yyyy',
'timeFormat'            => 'HH:mm:ss',
'formattedDateFormat'  => 'd MMM yyyy'


system\I18n\Time.php
override this methods:

PHP Code:
public function toDateTimeString(?bool $toLocale true)
{
    $stringFormat = ($toLocale) ? lang('Time.dateTimeFormat') : 'yyyy-MM-dd HH:mm:ss';
    return $this->toLocalizedString($stringFormat);    
}

public function 
toDateString(?bool $toLocale true)
{
    $stringFormat = ($toLocale) ? lang('Time.dateFormat') : 'yyyy-MM-dd';
    return $this->toLocalizedString($stringFormat);    
}

public function 
toFormattedDateString(?bool $toLocale true)
{
    $stringFormat = ($toLocale) ? lang('Time.formattedDateFormat') : 'MMM d, yyyy';
    return $this->toLocalizedString($stringFormat);    
}

public function 
toTimeString(?bool $toLocale true)
{
    $stringFormat = ($toLocale) ? lang('Time.timeFormat') : 'HH:mm:ss';
    return $this->toLocalizedString($stringFormat);    



AND REPLACE: ->toDateTimeString()
TO: ->toDateTimeString(false)
(4 replacements)



NOW

PHP Code:
echo "<br>" $miaData->toFormattedDateString();
echo 
"<br>" $miaData->toDateTimeString();
echo 
"<br>" $miaData->toDateTimeString(false); 


$defaultLocale = 'it';     
Quote:31 lug 2021
31/07/2021 23:59:30
2021-07-31 23:59:30

$defaultLocale = 'en';     
Quote:Jul 31, 2021
07/31/2021 23:59:30
2021-07-31 23:59:30


EDIT:
The user can easily overwrite the format, modifying the file MYAPP/app/Language/it/Time.php
Reply




Theme © iAndrew 2016 - Forum software by © MyBB