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 🙏

© 2026 – Pkg Stats / Ryan Hefner

vite-plugin-drupal-t

v1.0.4

Published

A Vite plugin that automatically extracts Drupal.t() and Drupal.formatPlural() translation calls for seamless internationalization

Readme

Drupal translations extract plugin for Vite

A Vite plugin that automatically extracts Drupal.t() and Drupal.formatPlural() translation calls from your JavaScript/TypeScript source code into a separate file. This enables Drupal's translation system to discover and process strings from your Vite-built frontend applications, making internationalization seamless across your entire stack.

The plugin scans your codebase during the build process, collects all translation calls, and outputs them as JavaScript comments in a translations.js file. Once included in your Drupal library, these strings become available for translation through Drupal's standard translation interface.

Installation

npm install vite-plugin-drupal-t --save-dev

Usage

Add to vite.config.ts file:

import { defineConfig } from 'vite'
import extractDrupalT from 'vite-plugin-drupal-t'

export default defineConfig({
  plugins: [
    extractDrupalT({
      include: ['**/*.js', '**/*.ts', '**/*.vue'],
    }),
  ],
})

Or if you need it only during build:

import { defineConfig } from 'vite'
import extractDrupalT from 'vite-plugin-drupal-t'

export default defineConfig({
  build: {
    rollupOptions: {
      plugins: [
        extractDrupalT({
          include: ['**/*.js', '**/*.ts', '**/*.vue'],
        }),
      ],
    },
  },
})

Output

The plugin creates a translations.js file in your build output directory (e.g., dist/translations.js). This file contains all translation strings found in the codebase as JavaScript comments:

// Drupal.t('Welcome to the site')
// Drupal.t('Hello @name', {'@name': userName})
// Drupal.formatPlural(5, '1 item', '@count items', {})

The translations are commented out, so they won't execute at runtime, but Drupal's string extraction system can still find and process them.

Drupal Setup

For Drupal to discover and extract these translation strings, you must include the translations.js file in a Drupal library definition.

Step 1: Add to your library definition

In your theme or module's *.libraries.yml file (e.g., mytheme.libraries.yml):

global:
  js:
    dist/translations.js: {}
    dist/main.js: {}
  css:
    theme:
      dist/style.css: {}

Or create a dedicated library just for translations:

translations:
  js:
    dist/translations.js: {}

main:
  js:
    dist/main.js: {}
  dependencies:
    - mytheme/translations

Step 2: Attach the library

Attach the library in your theme or module:

In your theme's .info.yml file:

libraries:
  - mytheme/global

Or programmatically in a hook:

/**
 * Implements hook_page_attachments().
 */
function mytheme_page_attachments(array &$attachments) {
  $attachments['#attached']['library'][] = 'mytheme/global';
}

Step 3: Extract translations

Once the library is attached, Drupal's translation system will scan the translations.js file during:

  • Interface translation updates (Configuration → Regional and language → User interface translation)
  • Locale module scanning when enabled
  • Drush locale:check and locale:update commands

The commented translation calls will be extracted and made available for translation in Drupal's translation interface.

Features

  • ✅ Extracts Drupal.t() calls
  • ✅ Extracts Drupal.formatPlural() calls
  • ✅ Supports single quotes, double quotes, and template literals
  • ✅ Supports multiline strings
  • ✅ Supports strings with parentheses
  • ✅ Supports parameters and context options
  • ✅ Automatically deduplicates identical translations
  • ✅ Sorts output alphabetically

Options

include

Type: string | string[] Default: undefined

A pattern or array of patterns to include files for extraction.

exclude

Type: string | string[] Default: undefined

A pattern or array of patterns to exclude files from extraction.