This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

dark.eye9
dark.eye9

Create a partial with code (Example for Spanish):

{% set meses = ["Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"] %}
{{ fecha|date('j') }} {{ meses[fecha|date('n')-1] }} {{ fecha|date('Y') }}

Call it from your pages like: {% partial 'fecha_esp' fecha=post.published_at %} It will print 2 Junio, 2014

To adapt it to other languages you only have to replace each month name in the array.

Hope it helps somebody ;)

tom.plessis238
tom.plessis238

I have a similar problem to localize date in french. After adding new locale on my ubutnu server, forcing locale in my plugin like that :

setlocale(LC_TIME, 'fr_FR');

And then using twig to diplay my localized date :

{{ wedding.date.formatLocalized('%A %d %B %Y')|lower }}

it does not work, date is in english... What did i missed ?

sulaz1x
sulaz1x

Anyone solved this ? I can't get date printed in an other language than English, how to switch ?

planetadeleste
planetadeleste

I have the same problem.

+1 to give us a solution or fix (if is a bug)

Thanks

Tschallacka
Tschallacka

Well.. the rendering happens in a totally different flow than the plugin itself I think, which would cause the locale context to be set in your namespace, but not in the namespace of the octobercms that does the actual parsing and outputting.

This stackoverflow answer might help though

http://stackoverflow.com/questions/17346366/symfony-2-setlocale-lc-all-de-de

Or this answer thats for laravel5

https://laracasts.com/discuss/channels/general-discussion/where-to-setlocale-in-laravel-5-on-multilingual-multidomain-app

Last updated

planetadeleste
planetadeleste

Thanks @Tschallacka for the references. I found the solution un stackoverflow post.

// In Components 
public function __construct($cmsObject = null, $properties = []){
    Carbon::setLocale( Lang::getLocale() );
    parent::__construct($cmsObject, $properties);
}

{# In Twig  #}
<h5>{{ now.diffForHumans(post.created_at) }}</h5>

And works fine. Thanks

Last updated

planetadeleste
planetadeleste

New update on localization dates. Lang::getLocale() return the locale code in ISO-3166-1, but setlocale() is in POSIX and them, use Locale format, like en_EN.UTF-8. To have dates in locale language, use this code in component.

public function __construct($cmsObject = null, $properties = []){
    $localeCode = Lang::getLocale();
    Carbon::setLocale( $localeCode );
    setlocale( LC_TIME, $localeCode . '_' . strtoupper($localeCode) . '.UTF-8' );
    parent::__construct($cmsObject, $properties);
}

I don't know if this is the best solution, work for me.

Thanks

alwin
alwin

Adding this to your Plugin.php file will do (in most cases) the job:

Add this at the top of the Plugin.php file:

use Carbon\Carbon;
use Lang;

Add this to the body of the Plugin class:

    /**
     * Boot method, called right before the request route.
     */
    public function boot()
    {
        $localeCode = Lang::getLocale();
        setlocale(LC_TIME, $localeCode . '_' . strtoupper($localeCode) . '.UTF-8');
    }

    /**
     * Registers CMS markup tags introduced by this plugin.
     */
    public function registerMarkupTags()
    {
        return [
            'filters' => [
                'strftime' => function(Carbon $time, $format) {
                    return strftime($format, $time->getTimestamp());
                }
            ]
        ];
    }

In the Twig template:

{{ post.published_at|strftime('%A %e %B %Y') }}
Vojta Svoboda
Vojta Svoboda

Try to use Twig extensions plugin with strftime filter.

Last updated

ricardoesc.65314053
Asinox
Asinox

Super!, i was looking for this!

alessio
alessio

I found a workaround with javascript:

<script>document.write(new Date({{ post.published_at|date('Y') }}, {{ post.published_at|date('n') -1 }}, {{ post.published_at|date('j') }} ).toLocaleDateString());</script>

NOTE: Date() uses months from 0-11, so remember to subtract 1

The output is a date formatted in the user's browser locale.

Date.prototype.toLocaleDateString() syntax, options and examples here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

Hope this helps

Last updated

klas.wirholm
klas.wirholm

alwin said:

Adding this to your Plugin.php file will do (in most cases) the job:

Add this at the top of the Plugin.php file:

use Carbon\Carbon;
use Lang;

Add this to the body of the Plugin class:

   /**
    * Boot method, called right before the request route.
    */
   public function boot()
   {
       $localeCode = Lang::getLocale();
       setlocale(LC_TIME, $localeCode . '_' . strtoupper($localeCode) . '.UTF-8');
   }

   /**
    * Registers CMS markup tags introduced by this plugin.
    */
   public function registerMarkupTags()
   {
       return [
           'filters' => [
               'strftime' => function(Carbon $time, $format) {
                   return strftime($format, $time->getTimestamp());
               }
           ]
       ];
   }

In the Twig template:

{{ post.published_at|strftime('%A %e %B %Y') }}

That code us actually not a good general recommendation, as the utf language code (first part before underscore) not always is the same as the country (first part before underscore).

Also no nee to redefine strftime(). Even date() change language format when you set core php value by setlocale().

Easiest is to just add setlocale(LCTIME, "sv'SE.UTF-8"); on config/app.php

If you want to use dynamical translation and change of language by session for frontend, you need yo use the following syntax, and have twig extension installed: {{ date|localizeddate('full', 'none', defaultLocale, null, 'dd MMMM, yyyy') }}

The problem is that most plugin are not configured for that intl carbon format, so you need to change a lot of twig format in your own partials.

My bee there is an other solution, would bee glad to hear it, though.

1-13 of 13

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.