npm.io
1.0.2 • Published 2d ago

wiktionary-sound

Licence
MIT
Version
1.0.2
Deps
0
Size
13 kB
Vulns
0
Weekly
0

wiktionary-sound

Retrieve pronunciation audio files from Wiktionary and Wikimedia Commons.

wiktionary-sound searches Wiktionary entries, finds available pronunciation recordings, resolves their Wikimedia URLs, extracts metadata (language, speaker, accent), and selects the best pronunciation automatically.

Useful for:

  • Language learning applications
  • Dictionary apps
  • Flashcards
  • Pronunciation tools
  • React / Next.js applications
  • Educational software

Features

  • Supports Wiktionary languages
  • Finds real pronunciation recordings
  • Extracts speaker information when available
  • Detects accents from filenames
  • Automatically selects the best pronunciation
  • Built-in caching to reduce repeated requests
  • Works with Node.js and modern bundlers
  • Includes TypeScript declarations

Installation

Using npm:

npm install wiktionary-sound

Using pnpm:

pnpm add wiktionary-sound

Using yarn:

yarn add wiktionary-sound

Quick Start

JavaScript

import { getSound } from "wiktionary-sound";

const result = await getSound("bonjour", "fr");

console.log(result.best);

Example output:

{
  "title": "Fichier:Fr-bonjour.ogg",
  "url": "https://upload.wikimedia.org/...",
  "extension": "ogg",
  "language": "fr",
  "speaker": null,
  "accent": null
}

The best result is the recommended pronunciation file.


API Reference

getSound(word, language, options)

Searches Wiktionary for pronunciation files.

Parameters

Parameter Type Default Description
word string required Word to search
language string "en" Wiktionary language edition
options object {} Search options

Example:

const result = await getSound("chaussure", "fr");

Options

Language

The second argument controls the Wiktionary language edition.

Examples:

await getSound("hello", "en");

await getSound("bonjour", "fr");

await getSound("hola", "es");

Selection preferences

You can control pronunciation selection:

const result = await getSound("hello", "en", {
  preferLanguage: "en",
  preferAccent: "us",
});

Available preferences:

Option Description
preferLanguage Prefer recordings from a language code
preferAccent Prefer a specific accent
preferNative Prefer native pronunciation recordings

Response Format

getSound() returns:

{
  (word, sourceLanguage, files, best);
}

word

The searched word.

Example:

"bonjour";

sourceLanguage

The Wiktionary language used.

Example:

"fr";

files

All available pronunciation recordings.

Example:

[
  {
    "title": "Fichier:Fr-bonjour.ogg",
    "url": "https://upload.wikimedia.org/...",
    "extension": "ogg",
    "language": "fr",
    "speaker": null,
    "accent": null
  }
]

best

The automatically selected pronunciation.

Example:

{
  "title": "Fichier:Fr-chaussure.ogg",
  "url": "https://upload.wikimedia.org/...",
  "extension": "ogg",
  "language": "fr",
  "speaker": null,
  "accent": null
}

Selecting Pronunciations

The package uses a scoring system to choose the best audio file.

Higher priority:

  1. Exact requested language match
  2. Native language recordings
  3. Official Wiktionary pronunciation files
  4. Preferred accent match
  5. Supported audio formats

Example:

For:

getSound("hello", "en");

Possible results:

En-us-hello.ogg
En-uk-hello.ogg
LL-Q1860-English recordings

The selector chooses the highest scoring result.


TypeScript

The package includes TypeScript definitions.

Example:

import { getSound, SoundResult } from "wiktionary-sound";

const result: SoundResult = await getSound("bonjour", "fr");

console.log(result.best.url);

React Example

The package can be used inside React applications.

Example:

import { useEffect, useState } from "react";
import { getSound } from "wiktionary-sound";

export default function Pronunciation() {
  const [audio, setAudio] = useState(null);

  useEffect(() => {
    getSound("bonjour", "fr").then((result) => {
      setAudio(result.best.url);
    });
  }, []);

  return audio && <audio controls src={audio} />;
}

Data Source

This package uses:

  • Wiktionary API
  • Wikimedia Commons audio files

All pronunciation recordings are provided by Wikimedia contributors.


Limitations

  • Not every word has pronunciation audio.
  • Some languages have fewer recordings.
  • Audio quality depends on available Wikimedia files.
  • Results depend on Wiktionary content.

License

MIT License

Copyright 2026 saugatproductions

Keywords