DEV Community

Paul-Etienne Coisne
Paul-Etienne Coisne

Posted on

4 3

Painless i18n for Moment.js

You serve a multi-lingual JS app, use Moment.js and need to internationalize how times are displayed? Why bother with an expansive new NPM package where you could do it yourself in just a few lines of code?

First create a file that'll store the various formats you need, as well as the function that'll pass them to your custom Moment.js localize function:

// First find a way to determine the user's locale
const getLocale = () => window.locale || 'fr'

// Define as many formats as you wish.
const formats = {
  'ddd DD': {
    fr: 'ddd DD',
    de: 'ddd Do',
    en: 'ddd Do',
    'en-US': 'ddd Do',
    it: 'ddd DD',
  },
  'D MMM YYYY à HH:mm': {
    fr: 'D MMM YYYY à HH:mm',
    de: 'Do MMM YYYY [um] H:mm',
    en: 'D MMM YYYY [at] H:mm',
    'en-US': 'MMM Do YYYY [at] H:mm',
    it: 'D MMM YYYY [alle] HH:mm',
  },
}

// Whether you want the function to fail or not if the name doesn't exist is up to you. 
const i18nFormat = (name, locale = getLocale()) => {
  if (typeof formats[name] !== 'object') {
    console.log(`Unknown format definition for "${name}"`)

    return name
  }

  if (!formats[name][locale]) {
    console.log(
      false,
      `Missing format definition for "${name}" in locale "${locale}"`
    )

    return name
  }

  return formats[name][locale]
}

export default i18nFormat
Enter fullscreen mode Exit fullscreen mode

Now you can create a file defining your localize function...

import moment from 'moment'

moment.fn.localize = function localize(inputString) {
  return this.format(i18nFormats(inputString))
}
Enter fullscreen mode Exit fullscreen mode

...and import it at the root of your project. You're hot to trot!

import React from 'react'
import './setupMoment'
import moment from 'moment'

const App = () => (
  <div className="App">
    <h1>Internationalize Moment.js the easy way</h1>
    <h2>'dddd': {moment().localize('dddd')}</h2>
    <h2>'D MMM YYYY à HH:mm': {moment().localize('D MMM YYYY à HH:mm')}</h2>
  </div>
)

export default App
Enter fullscreen mode Exit fullscreen mode

Functional demo on Codesandbox

Billboard image

Monitor more than uptime.

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay