npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gabortorma/nuxt-dayjs-i18n

v5.2.18

Published

Extend dayjs-nuxt plugin with automatic i18n locale switching.

Downloads

135

Readme

Nuxt Dayjs i18n

npm version npm downloads License Nuxt code style

Extend dayjs-nuxt plugin with automatic i18n locale switching, computed plugins and provide format function.

Instructions

  • It automatically call dayjs.locale('en') when i18n.setLocale('en') is called.
  • Always use i18n.setLocale('en') function instead of setting i18n.locale.value = 'en', because value setting not fire the i18n:beforeLocaleSwitch and i18n:localeSwitched nuxt hooks.
  • Don't use dayjs.locale function, because it doesn't called back the i18n.setLocale.
  • You can change locales locally with dayjs().locale('en') function.

Quick Setup

  1. Add nuxt-dayjs-i18n dependency to your project
# Using pnpm
pnpm add -D @gabortorma/nuxt-dayjs-i18n

# Using yarn
yarn add --dev @gabortorma/nuxt-dayjs-i18n

# Using npm
npm install --save-dev @gabortorma/nuxt-dayjs-i18n
  1. Add nuxt-dayjs-i18n to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@gabortorma/nuxt-dayjs-i18n', '@nuxtjs/i18n', 'nuxt-dayjs'],
})

Configuration

You can specify the plugins, set ato provide format function

export default defineNuxtConfig({
  i18n: {
    locales: [{
      code: 'en-gb',
      language: 'en-GB',
      file: 'en-gb.ts',
      name: 'English',
    }, {
      // use same code with dayjs locale file names:
      // https://github.com/iamkun/dayjs/tree/dev/src/locale
      code: 'hu',
      language: 'hu-HU',
      file: 'hu.ts',
      name: 'Magyar',
    }],
  },
  dayjs: {
    // locales: ['en-gb', 'hu'],
    // locales not needed, it automatically comes from i18n

    // defaultLocale: 'en'
    // !! don't use defaultLocale, it comes from i18n
  },
  dayjsI18n: {
    computedPlugins: true, // you can specify in array: ['localiztedFormat', 'relativeTime', 'localeData']
    provideFormat: true, // provide $df for Vue
  }
})

Debug options removed

Use CONSOLA_LEVEL environment variable instead.

Basic usage

You can check dayjs-nuxt

Usage in computed value

Original dayjs function lost reactivity in computed value and does not change when the locale is changed. These three computed plugin provides a solution for this.

Localized format

const formatInComputed = computed(() => dayjs(new Date()).format('L LTS'))
// still reactive, format result value changes when locale is changed by i18n.setLocale

Relative time

computedFrom

const fromInComputed = computed(() => dayjs(new Date()).from('2023-01-01'))
// still reactive, from result value changes when locale is changed by i18n.setLocale

computedFromNow

const fromNowInComputed = computed(() => dayjs(new Date()).fromNow())
// still reactive, fromNow result value changes when locale is changed by i18n.setLocale

computedTo

const toInComputed = computed(() => dayjs(new Date()).to('2023-01-01'))
// still reactive, to result value changes when locale is changed by i18n.setLocale

computedToNow

const toNowInComputed = computed(() => dayjs(new Date()).toNow())
// still reactive, toNow result value changes when locale is changed by i18n.setLocale

Locale data

const monthsInComputed = computed(() => dayjs.localeData().months())
// still reactive, months result value changes when locale is changed by i18n.setLocale

dayjs.localeData().xxxx()
// still reactive, result value of any function of localeData changes when locale is changed by i18n.setLocale

You can check more examples in Playground

Provided format

You can use the provided format function anywhere.

<template>
  <div>
    {{ $df('2023-09-07 15:00:02', 'L LTS') }}
    // en-US output: 09/07/2023 3:00:02 PM
  </div>
</template>

Development

# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Release new version
npm run release