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

trans-google-translate

v1.0.2

Published

A headless React hook and customizable UI components for integrating the Google Translate widget.

Readme

trans-google-translate

A highly customizable, zero-dependency React package that seamlessly wraps Google Translate. It offers a headless React Hook for fully custom selector layouts, alongside high-quality pre-styled components (a Brutalist floating button and a clean dropdown).

Designed for production environments, Next.js (SSR), and Vite.


Features

  • 🌐 Worldwide Compatibility: Supports 85+ global languages out of the box.
  • 🪶 Zero Styling Dependencies: Default styles are applied via inline JS objects. Works instantly in any project (Tailwind, Vanilla CSS, CSS Modules, Styled Components, etc.) without post-build CSS configuration.
  • ⚙️ Headless Core: A powerful React Hook (useGoogleTranslate) manages singleton script injection, reactive synchronization across multiple components, cookie purging, and MutationObservers to clean up native Google Translate iframe banners.
  • 🎨 Fully Custom Render Props: Complete control over the HTML structure and styles via renderButton, renderDropdown, and custom flags (flagUrlTemplate).
  • Reload-less Transitions: Syncs translation updates directly through Google's native dropdown element without a page reload if initialized.
  • 📦 Dual Output: Ship with both CommonJS (CJS) and ES Modules (ESM) outputs via tsup.

Installation

npm install trans-google-translate
# or
yarn add trans-google-translate
# or
pnpm add trans-google-translate

Usage

1. Default Floating Brutalist Selector

Drop it anywhere in your root layout. It loads the Google Translate script automatically, registers the observer, and provides a floating button in the bottom right corner.

import { GoogleTranslate } from "trans-google-translate";

export default function App() {
  return (
    <div>
      {/* Your application content */}
      <GoogleTranslate />
    </div>
  );
}

2. Standard Dropdown Selector

If you want a normal inline dropdown selector:

import { GoogleTranslateDropdown } from "trans-google-translate";

export default function Header() {
  return (
    <header>
      <Logo />
      <GoogleTranslateDropdown />
    </header>
  );
}

3. Headless Selector (Fully Custom)

Use the hook useGoogleTranslate to construct your own custom UI. Multiple hooks on the page will automatically sync their active language states.

import { useGoogleTranslate } from "trans-google-translate";

export function CustomSelector() {
  const { currentLanguage, changeLanguage, languages } = useGoogleTranslate({
    defaultLanguage: "fr", // Set default source language
  });

  return (
    <div className="custom-selector">
      <p>Active: {currentLanguage}</p>
      <div className="grid grid-cols-4 gap-2">
        {languages.map((lang) => (
          <button
            key={lang.value}
            onClick={() => changeLanguage(lang.value)}
            className={currentLanguage === lang.value ? "active" : ""}
          >
            {lang.label}
          </button>
        ))}
      </div>
    </div>
  );
}

API Reference

useGoogleTranslate(options?: UseGoogleTranslateOptions)

Options

| Option | Type | Default | Description | | :--- | :--- | :--- | :--- | | defaultLanguage | string | "en" | The source language of your website. | | languages | LanguageOption[] | Comprehensive list | Languages to display in selectors. | | onLanguageChange | (lang: string) => void | undefined | Callback fired when the active language changes. | | showTooltip | boolean | false | Show original Google Translate hover tooltips. | | cookieDomain | string | window.location.hostname | Domain for the googtrans cookie. | | storageKey | string | "google_translate_lang" | LocalStorage key to cache the translation state. |

Return Value (UseGoogleTranslateResult)

| Value | Type | Description | | :--- | :--- | :--- | | currentLanguage | string \| null | The current active language code (e.g., "fr"). | | isDefault | boolean | true if current language matches defaultLanguage. | | isInitialized | boolean | true if the Google Translate SDK has initialized. | | isLoading | boolean | true if the SDK script is currently loading. | | current | LanguageOption \| undefined | The active language option object. | | currentFlag | string \| null | Flag CDN image URL for the active language. | | changeLanguage | (lang: string) => void | Updates active language and performs translation. | | languages | LanguageOption[] | List of languages configured for the selector. |


<GoogleTranslate /> Props

| Prop | Type | Description | | :--- | :--- | :--- | | customLanguages | LanguageOption[] | Override the default global list. | | className | string | Custom CSS class for the root wrapper element. | | style | CSSProperties | Inline styles for the root wrapper element. | | buttonClassName | string | Custom CSS class for the trigger button. | | buttonStyle | CSSProperties | Inline styles for the trigger button. | | dropdownClassName | string | Custom CSS class for the dropdown menu container. | | dropdownStyle | CSSProperties | Inline styles for the dropdown menu container. | | options | UseGoogleTranslateOptions | Configuration options passed to useGoogleTranslate. | | flagUrlTemplate | (flagCode: string) => string | Custom function to generate flag image URLs. | | renderButton | (selected: LanguageOption \| undefined, isOpen: boolean, toggle: () => void) => ReactNode | Customize the trigger button. | | renderDropdown | (languages: LanguageOption[], currentLanguage: string \| null, selectLanguage: (lang: string) => void, isOpen: boolean) => ReactNode | Customize the dropdown list. |


<GoogleTranslateDropdown /> Props

| Prop | Type | Description | | :--- | :--- | :--- | | customLanguages | LanguageOption[] | Override the default global list. | | className | string | Custom CSS class for the select wrapper. | | style | CSSProperties | Inline styles for the select wrapper. | | selectClassName | string | Custom CSS class for the native select element. | | selectStyle | CSSProperties | Inline styles for the native select element. | | options | UseGoogleTranslateOptions | Configuration options passed to useGoogleTranslate. |


License

MIT © 2026