WanaKana: Japanese Text Conversion and Input

5.3.1 · active · verified Sun Apr 19

WanaKana is a JavaScript utility library designed for detecting and transliterating Japanese text between Hiragana, Katakana, Romaji, and handling Kanji. It provides a comprehensive set of functions for checking the type of script (e.g., `isJapanese`, `isKana`, `isRomaji`), converting between them (e.g., `toKana`, `toHiragana`, `toRomaji`), and includes DOM helpers for real-time input conversion (`bind`, `unbind`). The library is currently at version 5.3.1 and maintains an active release cadence, with minor updates and bug fixes occurring every few months. Its key differentiators include robust input method editor (IME) simulation capabilities for HTML text fields, comprehensive handling of various kana and romaji mapping rules, and support for custom mappings. WanaKana ships with TypeScript definitions, making it highly compatible with modern TypeScript-driven web development workflows.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic Romaji to Hiragana/Katakana conversion, checks for Japanese text, and illustrates how to bind WanaKana to a DOM input element for real-time conversion.

import { toKana, toHiragana, toRomaji, isJapanese, bind, unbind } from 'wanakana';

// Convert Romaji to Hiragana and Katakana
const romajiInput = 'konnichiwa sekai';
const hiragana = toHiragana(romajiInput); // 'こんにちわせかい'
const katakana = toKatakana(romajiInput); // 'コンニチワセカイ'

console.log(`Romaji: "${romajiInput}"`);
console.log(`  -> Hiragana: "${hiragana}"`);
console.log(`  -> Katakana: "${katakana}"`);

// Check if a string contains Japanese characters
const japaneseText = '日本語を勉強します';
const containsJapanese = isJapanese(japaneseText); // true
console.log(`"${japaneseText}" contains Japanese: ${containsJapanese}`);

// Demonstrating DOM binding (browser environment required)
// This part will only execute if 'document' is defined.
if (typeof document !== 'undefined') {
  const inputElement = document.createElement('input');
  inputElement.type = 'text';
  inputElement.id = 'wanakana-demo-input';
  document.body.appendChild(inputElement);

  console.log('Appended a demo input element to the document body.');

  // Bind wanakana for real-time Romaji to Kana conversion
  bind(inputElement, { IMEMode: true });

  console.log('WanaKana is bound to the input element. Type romaji to convert to kana.');

  // Simulate user input (programmatically)
  inputElement.value = 'nihongo';
  inputElement.dispatchEvent(new Event('input', { bubbles: true }));

  // In a real browser, inputElement.value would now be 'にほんご'
  // For console, we'll log what it *should* be:
  console.log(`Simulated input 'nihongo'. Expected input value after conversion: 'にほんご'. Current value (may not reflect live conversion in Node): ${inputElement.value}`);

  // Clean up
  unbind(inputElement);
  document.body.removeChild(inputElement);
  console.log('WanaKana unbound and input element removed.');
} else {
  console.log('Skipping DOM binding example as `document` is not defined (likely a Node.js environment).');
}

view raw JSON →