CakePHP Locale pt_BR and DateFormat

Hi !
I am trying to localize my Cake 3.3 app to portuguese “Brasil”.
I’ve downloaded this and saved in C:\Apache24\htdocs\cake3\src\Locale\pt_BR
I’ve changed the defaultLocale in C:\Apache24\htdocs\cake3\config\app.php to ‘defaultLocale’ => env(‘APP_DEFAULT_LOCALE’, ‘pt_BR’)

But Cake is still in english and I have tried to change the date format in C:\Apache24\htdocs\cake3\config\bootstrap.php with:

// Configure a custom datetime format parser format.	
Type::build('date')
 ->useLocaleParser()
 ->setLocaleFormat('dd/MM/yyyy');
Type::build('datetime')
 ->useLocaleParser()
 ->setLocaleFormat('dd/MM/yyyy HH:mm:ss');
Type::build('timestamp')
 ->useLocaleParser()
 ->setLocaleFormat('dd/MM/yyyy HH:mm:ss');

But input date is still showing 2016 - 08 - 18

What can I do?
Thanks

I use this way:

config/bootstrap.php

date_default_timezone_set('America/Sao_Paulo');
ini_set('intl.default_locale', 'pt_BR');
\Cake\I18n\Time::setToStringFormat([IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT]);

Inflector::rules('irregular', [
     'solicitacao' => 'solicitacoes'
]);

AppController.php

Type::build('date')
    ->useLocaleParser()
    ->setLocaleFormat('dd/MM/yyyy');

Type::build('datetime')
    ->useLocaleParser()
    ->setLocaleFormat('dd/MM/yyyy HH:mm');

Type::build('timestamp')
    ->useLocaleParser()
    ->setLocaleFormat('dd/MM/yyyy HH:mm');

Type::build('decimal')
    ->useLocaleParser();

Type::build('float')
    ->useLocaleParser();

Time::setToStringFormat('dd/MM/YYYY HH:mm');
1 Like

AppController initialize() or beforeRender() ?

ok i figuredout its better use all in the bootstrap:

/**
 * Locale Formats
 */
\Cake\I18n\Time::setToStringFormat([IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT]);
\Cake\I18n\Time::setToStringFormat('dd/MM/YYYY HH:mm');

\Cake\Database\Type::build('date')
    ->useLocaleParser()
    ->setLocaleFormat('dd/MM/yyyy');

\Cake\Database\Type::build('datetime')
    ->useLocaleParser()
    ->setLocaleFormat('dd/MM/yyyy HH:mm');

\Cake\Database\Type::build('timestamp')
    ->useLocaleParser()
    ->setLocaleFormat('dd/MM/yyyy HH:mm');

\Cake\Database\Type::build('decimal')
    ->useLocaleParser();

\Cake\Database\Type::build('float')
    ->useLocaleParser();

Still not working for me :frowning:, I don’t know why…

add.ctp:

<div class="entrega form large-9 medium-8 columns content">
    <?= $this->Form->create($entrega) ?>
    <fieldset>
        <legend><?= __('Add Entrega') ?></legend>
        <?php
            echo $this->Form->input('servidor_id', ['options' => $servidor]);
            echo $this->Form->input('curso_id', ['options' => $curso]);
            echo $this->Form->input('dataEntrega');
        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

Add ‘type’ => ‘text’ at dataEntrega input, when the date is loaded to the value it comes locale formated dd/mm/yyyy, and when you save the date its formated to the database format. In addition you can use a js calendar plugin for a nice datepick

1 Like

You probably are missing the format for Date.

In bootstrap.php, after the lines:

Type::build('time')
    ->useImmutable();
Type::build('date')
    ->useImmutable();
Type::build('datetime')
    ->useImmutable();

Add this:

/**
 * Default formats
 */
\Cake\I18n\Time::setToStringFormat('dd-MM-yyyy HH:mm:ss');
\Cake\I18n\Date::setToStringFormat('dd-MM-yyyy');
\Cake\I18n\FrozenTime::setToStringFormat('dd-MM-yyyy HH:mm:ss');
\Cake\I18n\FrozenDate::setToStringFormat('dd-MM-yyyy');
1 Like

Did not work :frowning:

I’ve uploaded the entire code to http://www.4shared.com/rar/_DzVriozce/cake3.html

1 Like

Oh, to change the date input format you can:

Modify a single input template:

$this->Form->input('dataEntrega', [
    'templates' => [
        'dateWidget' => '{{day}}{{month}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}'
    ]
]);

Modify date template for all inputs in the app:

Create the file ‘config/my_custom_templates.php’ with the following code:

<?php return [ 'dateWidget' => '{{day}}{{month}}{{year}}{{hour}}{{minute}}{{second}}{{meridian}}', ];

In ‘src/View/AppView.php’ load form Helper like:

$this->loadHelper(‘Form’, [
‘templates’ => ‘my_custom_templates’
]);

1 Like

It worked ! Thanks !
But do you know why the month’s name of Locale\pt_BR are not working?

You can find Cakephp core translations in this Plugin CakePHP localized library

To use the plugin translation files, link or copy them into their expected location:
src/Locale/{locale-code}/cake.po.

1 Like

for me, its better way …
// in bootstrap
date_default_timezone_set(‘America/Sao_Paulo’);
setlocale(LC_ALL, ‘pt_BR’, ‘pt_BR.utf-8’, ‘pt_BR.utf-8’, ‘portuguese’);

Type::build(‘time’)->useImmutable();
Type::build(‘date’)->useImmutable()->useLocaleParser();
Type::build(‘datetime’)->useImmutable()->useLocaleParser();
Type::build(‘timestamp’)->useImmutable();

\Cake\I18n\Time::setToStringFormat(‘dd/MM/yyyy HH:mm:ss’);
\Cake\I18n\Date::setToStringFormat(‘dd/MM/yyyy’);
\Cake\I18n\FrozenTime::setToStringFormat(‘dd/MM/yyyy HH:mm:ss’);
\Cake\I18n\FrozenDate::setToStringFormat(‘dd/MM/yyyy’);

Type::build(‘decimal’)->useLocaleParser();
Type::build(‘float’)->useLocaleParser();

1 Like

Foi o único que serviu pra mim.

Obrigado

1 Like

My final solution was this on bootstrap:

/*
 * Enable immutable time objects in the ORM.
 *
 * You can enable default locale format parsing by adding calls
 * to `useLocaleParser()`. This enables the automatic conversion of
 * locale specific date formats. For details see
 * @link http://book.cakephp.org/3.0/en/core-libraries/internationalization-and-localization.html#parsing-localized-datetime-data
 */
/*Type::build('time')
    ->useImmutable();
Type::build('date')
    ->useImmutable();
Type::build('datetime')
    ->useImmutable();
Type::build('timestamp')
    ->useImmutable();*/
	
date_default_timezone_set('America/Sao_Paulo');
setlocale(LC_ALL, 'pt_BR', 'pt_BR.utf-8', 'pt_BR.utf-8', 'portuguese');

Type::build('time')->useImmutable();
Type::build('date')->useImmutable()->useLocaleParser();
Type::build('datetime')->useImmutable()->useLocaleParser();
Type::build('timestamp')->useImmutable();

\Cake\I18n\Time::setToStringFormat('dd/MM/yyyy HH:mm:ss');
\Cake\I18n\Date::setToStringFormat('dd/MM/yyyy');
\Cake\I18n\FrozenTime::setToStringFormat('dd/MM/yyyy HH:mm:ss');
\Cake\I18n\FrozenDate::setToStringFormat('dd/MM/yyyy');

\Cake\I18n\I18n::locale('pt-BR'); // new !

Type::build('decimal')->useLocaleParser();
Type::build('float')->useLocaleParser();
2 Likes