Oh … hello, Japanese !
Trust me, Japanese language while I feel like reading part is very easy, but for writing and grammar, they are another topics.
Japanese language have 3 kinds of characters: Hiragana, Katakana, Kanji.
No, that’s no all, on computers …. they have a thing called full-width, half-width characters
I’m not sure what is the reason behind for these variances, but I guess that ‘s what make Japan unique Also I admire people who know Japanese language, one of the reason that I can’t learn Japanese. I tried and failed “HARDDD” 3 timess….
While working with Japanese clients projects, sometime I got assigned to tasks such as:
- That input field only accept a type of character (alphabet, number, Hiragana, Katakana, full-width, half-width …)
- Convert all of the characters in the input into full-width or half-width Hiragana or Katakana
- Limit the number of character in a text field to only XXX chars
While these tasks are not difficult, quite easy to implement for English or Vietnamese languages, but Japanese is “not so easy, hah?”.
Lucky for us, there are a lot of developers who have this same problem, and where is the problem, there is a solution.
Check out wanakana.js (nà na na na …), a lightweight, easy to use JS lib which can help us deal with the above tasks easier.
Installation
# 1. Install it from NPM
npm install wanakana
# 2 Import into your project
import * as wanakana from 'wanakana';
// or
import { toKana, isRomaji } from 'wanakana';
Quick references
wanakana.isJapanese('泣き虫。!〜2¥zenkaku')
// => true
wanakana.isKana('あーア')
// => true
wanakana.isHiragana('すげー')
// => true
wanakana.isKatakana('ゲーム')
// => true
wanakana.isKanji('切腹')
// => true
wanakana.isKanji('勢い')
// => false
wanakana.isRomaji('Tōkyō and Ōsaka')
// => true
wanakana.toKana('ONAJI buttsuuji')
// => 'オナジ ぶっつうじ'
wanakana.toKana('座禅‘zazen’スタイル')
// => '座禅「ざぜん」スタイル'
wanakana.toKana('batsuge-mu')
// => 'ばつげーむ'
wanakana.toKana('wanakana', { customKanaMapping: { na: 'に', ka: 'bana' }) });
// => 'わにbanaに'
wanakana.toHiragana('toukyou, オオサカ')
// => 'とうきょう、 おおさか'
wanakana.toHiragana('only カナ', { passRomaji: true })
// => 'only かな'
wanakana.toHiragana('wi', { useObsoleteKana: true })
// => 'ゐ'
wanakana.toKatakana('toukyou, おおさか')
// => 'トウキョウ、 オオサカ'
wanakana.toKatakana('only かな', { passRomaji: true })
// => 'only カナ'
wanakana.toKatakana('wi', { useObsoleteKana: true })
// => 'ヰ'
wanakana.toRomaji('ひらがな カタカナ')
// => 'hiragana katakana'
wanakana.toRomaji('ひらがな カタカナ', { upcaseKatakana: true })
// => 'hiragana KATAKANA'
wanakana.toRomaji('つじぎり', { customRomajiMapping: { じ: 'zi', つ: 'tu', り: 'li' }) };
// => 'tuzigili'
Check out the project github, or their website for more examples
Hope this help !