javascriptreactjsmongodbtimeago

Timeago.js use locale configs


I'm using timeago.js to display the information stored on the DB as '...hours ago'. My problem is that I need to translate this to pt_BR, I saw on the docs that they have this option and tried implement it on my ReactJS code but I didn't got anything, it still is in English.

import { format } from 'timeago.js'

export default function Post({post}) {

...

<span className="postDate">{format(post.createdAt, 'pt_BR')}</span>

...

Just to know, I'm using MongoDB.

Where is my error?


Solution

  • I figured out how to do it:

    import * as timeago from 'timeago.js'
    import TimeAgo from 'timeago-react'
    import pt_BR from 'timeago.js/lib/lang/pt_BR'
    
    timeago.register('pt_BR', pt_BR)
    
    <span className="postDate">
    <TimeAgo datetime={post.createdAt} locale='pt_BR' />
    </span>