Kana Translator

By Serpentsounds on May 29, 2014

If you ever encounter Japanese text during your IRCing, you might be able to make use of this. Unfortunately, it will only help you out with kana, not kanji, sorry!

When you hover over text that contains Japanese kana, a small tooltip-like window will appear showing you the original text and the romanization. To make the window disappear, simply hover over any text that doesn't contain kana.

While the window is open, you can use the up and down arrows to slide the position to wherever is least obtrusive for you. This position will be remembered the next time the window appears.

To disable the functionality entirely, there is a popup menu with an on/off toggle.

/* 

Benjamin "Serpentsounds" Berry
May 29, 2014

Kana "Translation" Script
This script offers a non-obtrusive tooltip-like window which romanizes any Japanese kana the user may mouse over.

To make the window disappear, simply hover over text not containing any kana.

To change the position of the window, use the up and down arrows while it is open.

To disable the script, use the toggle found in the right click menu.

*/

menu * {
  -
  $iif($group(#KanaTranslator).status == on,$style(1)) Kana Translator:toggleKanaTranslator
  -
}

#KanaTranslator on
;Activated when mousing over text
on ^*:HOTLINK:*:*:{
  ;Kana detected in string
  if ($regex($1-,/(*UTF8)[\x{3040}-\x{30FF}]/)) {
    ;Reinitialize offset if missing or first run
    if (!$var(KanaTranslation.YOffset)) set %KanaTranslation.YOffset 0

    ;KanaTranslation isn't present, make new window
    if (!$window(@KanaTranslation)) window -aBk0dfow0 +b @KanaTranslation $window(-2).x $calc($window(-2).y + %KanaTranslation.YOffset) $window(-2).w 38

    ;KanaTranslation is already up, clear window
    else dline @KanaTranslation 1-2

    ;Add original and translation
    aline @KanaTranslation $1-
    aline @KanaTranslation $kanaToRomaji($1-)
  }

  ;No kana
  else {
    ;Get rid of existing KanaTranslation window
    if ($window(@KanaTranslation)) window -c @KanaTranslation

    ;Disable pointer
    halt
  }
}
#KanaTranslation end

;User presses up key
on *:KEYDOWN:@KanaTranslation:38:{
  ;Don't allow movement above window
  if (%KanaTranslation.YOffset <= 0) return

  ;Update offset
  set %KanaTranslation.YOffset $calc(%KanaTranslation.YOffset - 1)

  ;Translate window
  window @KanaTranslation $window(@KanaTranslation).x $calc($window(@KanaTranslation).y - 1) $window(-2).w 38
}

;User presses down key
on *:KEYDOWN:@KanaTranslation:40:{
  ;Don't allow movement below window
  if (%KanaTranslation.YOffset >= $calc($window(-2).y + $window(-2).h)) return

  ;Update offset
  set %KanaTranslation.YOffset $calc(%KanaTranslation.YOffset + 1)

  ;Translate window
  window @KanaTranslation $window(@KanaTranslation).x $calc($window(@KanaTranslation).y + 1) $window(-2).w 38
}

;Main translation alias
alias kanaToRomaji {
  var %text $$1

  ;Adds ' for n followed by vowel (んあ vs. な)
  %text = $regsubex(%text,/(*UTF8)([\x{30F3}\x{3093}])([\x{30A1}-\x{30AA}\x{3041}-\x{304A}])/g,\1'\2)

  ;Replace extra katakana combinations with romaji
  var %x 1,%extraKatakana $extraKatakana,%extraRomaji $extraRomaji
  while ($gettok(%extraKatakana,%x,32)) {
    %text = $replace(%text,$v1,$gettok(%extraRomaji,%x,32))
    inc %x
  }

  ;Replace katakana and hiragana with romaji
  var %x 1,%katakana $katakana,%hiragana $hiragana,%romaji $romaji
  while ($gettok(%katakana,%x,32)) {
    %text = $replace(%text,$v1,$gettok(%romaji,%x,32),$gettok(%hiragana,%x,32),$gettok(%romaji,%x,32))
    inc %x
  }

  ;Double up long katakana vowels (aー to aa, etc)
  %text = $regsubex(%text,/(*UTF8)([aeiou])\x{30FC}/g,\1\1)

  ;Fill in sokuons (っk to kk, etc)
  %text = $regsubex(%text,/(*UTF8)(?:\x{30C3}|\x{3063})([a-z])/g,\1\1)

  return %text
}

;Popup alias to enable/disable function
alias toggleKanaTranslator {
  ;Enabled, so we disable
  if ($group(#KanaTranslator).status == on) {
    ;If the window is open, close it
    if ($window(@KanaTranslator)) window -c @KanaTranslator

    .disable #KanaTranslator
  }

  ;Other way around, reenable KanaTranslator
  else .enable #KanaTranslator
}

/*  
Lists of characters and character groups (digraphs)
Beware editing: proper romanization relies on the position of characters in the string
*/

alias katakana return キャ キュ キョ シャ シュ ショ チャ チュ チョ ニャ ニュ ニョ ヒャ ヒュ ヒョ ミャ ミュ ミョ リャ リュ リョ ギャ ギュ ギョ ジャ ジュ ジョ ヂャ ヂュ ヂョ ビャ ビュ ビョ ピャ ピュ ピョ ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ワ ヲ ガ ギ グ ゲ ゴ ザ ジ ズ ゼ ゾ ダ ヂ ヅ デ ド バ ビ ブ ベ ボ パ ピ プ ペ ポ ン ァ ィ ゥ ェ ォ

alias hiragana return きゃ きゅ きょ しゃ しゅ しょ ちゃ ちゅ ちょ にゃ にゅ にょ ひゃ ひゅ ひょ みゃ みゅ みょ りゃ りゅ りょ ぎゃ ぎゅ ぎょ じゃ じゅ じょ ぢゃ ぢゅ ぢょ びゃ びゅ びょ ぴゃ ぴゅ ぴょ あ い う え お か き く け こ さ し す せ そ た ち つ て と な に ぬ ね の は ひ ふ へ ほ ま み む め も や ゆ よ ら り る れ ろ わ を が ぎ ぐ げ ご ざ じ ず ぜ ぞ だ ぢ づ で ど ば び ぶ べ ぼ ぱ ぴ ぷ ぺ ぽ ん ぁ ぃ ぅ ぇ ぉ

alias extraKatakana return ヴァ ヴィ ヴ ヴェ ヴォ ウィ ウェ ウォ ファ フィ フェ フォ チェ ディ ドゥ ティ トゥ シェ

alias romaji return kya kyu kyo sha shu sho cha chu cho nya nyu nyo hya hyu hyo mya myu myo rya ryu ryo gya gyu gyo ja ju jo ja ju jo bya byu byo pya pyu pyo a i u e o ka ki ku ke ko sa shi su se so ta chi tsu te to na ni nu ne no ha hi fu he ho ma mi mu me mo ya yu yo ra ri ru re ro wa wo ga gi gu ge go za ji zu ze zo da ji zu de do ba bi bu be bo pa pi pu pe po n a i u e o

alias extraRomaji return va vi vu ve vo wi we wo fa fi fe fo che di dou ti tou she

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.