Convert full-width Japanese text to half-width (zen-kaku to han-kaku)

I use a combination of the Ruby built-in NKF and String#tr

require 'nkf'
dbl = "BOXカタカナ"
dbl = NKF.nkf('-X -w', dbl).tr('0-9a-zA-Z', '0-9a-zA-Z')
# dbl now is "BOXカタカナ"

This has the added benefit of transposing half-width katakana to full-with katakana as well.


Well, it's not pretty and it only works for Romaji (could be extended to deal with other characters) but it worked for me:

title = "BOX"
englishReplacements = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
japaneseReplacements = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

converted = title.tr(japaneseReplacements, englishReplacements) 
# title is now "BOX"