มอดูลนี้ใช้สำหรับถอดอักษรคะนะ (ฮิระงะนะและคะตะกะนะ) ในภาษาญี่ปุ่นเป็นอักษรไทย


local export = {}
local gsub = mw.ustring.gsub
local u = mw.ustring.char

local all_kana = '([あいうえお-もやゆよ-ろわ-ゔアイウエオ-モヤユヨ-ロワ-ヴヷ-ヺ][ぁぃぅぇぉゃゅょァィゥェォャュョ]?)'

local kana = {
	['あ'] = {'¤อะ'}, ['い'] = {'อิ'}, ['う'] = {'อุ'}, ['え'] = {'เอะ'}, ['お'] = {'โอะ'},
	['か'] = {'¤คะ','¤กะ'}, ['き'] = {'คิ','กิ'}, ['く'] = {'คุ','กุ'}, ['け'] = {'เคะ','เกะ'}, ['こ'] = {'โคะ','โกะ'},
	['さ'] = {'¤ซะ'}, ['し'] = {'ชิ'}, ['す'] = {'ซุ'}, ['せ'] = {'เซะ'}, ['そ'] = {'โซะ'},
	['た'] = {'¤ทะ','¤ตะ'}, ['ち'] = {'ชฺิ'}, ['つ'] = {'สึ'}, ['て'] = {'เทะ','เตะ'}, ['と'] = {'โทะ','โตะ'},
	['な'] = {'¤นะ'}, ['に'] = {'นิ'}, ['ぬ'] = {'นุ'}, ['ね'] = {'เนะ'}, ['の'] = {'โนะ'},
	['は'] = {'¤ฮะ'}, ['ひ'] = {'ฮิ'}, ['ふ'] = {'ฟุ'}, ['へ'] = {'เฮะ'}, ['ほ'] = {'โฮะ'},
	['ま'] = {'¤มะ'}, ['み'] = {'มิ'}, ['む'] = {'มุ'}, ['め'] = {'เมะ'}, ['も'] = {'โมะ'},
	['や'] = {'¤ยะ'}, ['ゆ'] = {'ยุ'}, ['よ'] = {'โยะ'},
	['ら'] = {'¤ระ'}, ['り'] = {'ริ'}, ['る'] = {'รุ'}, ['れ'] = {'เระ'}, ['ろ'] = {'โระ'},
	['わ'] = {'¤วะ'}, ['ゐ'] = {'วิ'}, ['ゑ'] = {'เวะ'}, ['を'] = {'โวะ'},
	['ん'] = {'ง~'}, ['っ'] = {'ต~'},

	['ゔ'] = {'วฺ'..'ุ'},
	['が'] = {'¤กะ','¤งะ'}, ['ぎ'] = {'กิ','งิ'}, ['ぐ'] = {'กุ','งุ'}, ['げ'] = {'เกะ','เงะ'}, ['ご'] = {'โกะ','โงะ'},
	['ざ'] = {'¤ซฺะ'}, ['じ'] = {'จิ'}, ['ず'] = {'ซฺ'..'ุ'}, ['ぜ'] = {'เซฺะ'}, ['ぞ'] = {'โซฺะ'},
	['だ'] = {'¤ดะ'}, ['ぢ'] = {'จฺิ'}, ['づ'] = {'สฺึ'}, ['で'] = {'เดะ'}, ['ど'] = {'โดะ'},
	['ば'] = {'¤บะ'}, ['び'] = {'บิ'}, ['ぶ'] = {'บุ'}, ['べ'] = {'เบะ'}, ['ぼ'] = {'โบะ'},
	['ぱ'] = {'¤พะ','¤ปะ'}, ['ぴ'] = {'พิ','ปิ'}, ['ぷ'] = {'พุ','ปุ'}, ['ぺ'] = {'เพะ','เปะ'}, ['ぽ'] = {'โพะ','โปะ'},

	['うぁ'] = {'¤วะ'}, ['うぃ'] = {'วิ'}, ['うぅ'] = {'วุ'}, ['うぇ'] = {'เวะ'}, ['うぉ'] = {'โวะ'},
	['きゃ'] = {'เคียะ','เกียะ'}, ['きゅ'] = {'คิว','กิว'}, ['きょ'] = {'เคียว','เกียว'},
	['しゃ'] = {'¤ชะ'}, ['しゅ'] = {'ชุ'}, ['しぇ'] = {'เชะ'}, ['しょ'] = {'โชะ'},
	['ちゃ'] = {'¤ชฺะ'}, ['ちゅ'] = {'ชฺ'..'ุ'}, ['ちぇ'] = {'เชฺะ'}, ['ちょ'] = {'โชฺะ'},
	['つぁ'] = {'¤สะ'}, ['つぃ'] = {'สิ'}, ['つぇ'] = {'เสะ'}, ['つぉ'] = {'โสะ'},
	['てぃ'] = {'ทิ','ติ'},
	['とぅ'] = {'ทุ','ตุ'},
	['にゃ'] = {'เนียะ'}, ['にゅ'] = {'นิว'}, ['にょ'] = {'เนียว'},
	['ひゃ'] = {'เฮียะ'}, ['ひゅ'] = {'ฮิว'}, ['ひょ'] = {'เฮียว'},
	['ふぁ'] = {'¤ฟะ'}, ['ふぃ'] = {'ฟิ'}, ['ふぇ'] = {'เฟะ'}, ['ふぉ'] = {'โฟะ'},
	['みゃ'] = {'เมียะ'}, ['みゅ'] = {'มิว'}, ['みょ'] = {'เมียว'},
	['りゃ'] = {'เรียะ'}, ['りゅ'] = {'ริว'}, ['りょ'] = {'เรียว'},

	['ゔぁ'] = {'¤วฺะ'}, ['ゔぃ'] = {'วฺิ'}, ['ゔぇ'] = {'เวฺะ'}, ['ゔぉ'] = {'โวฺะ'},
	['ぎゃ'] = {'เกียะ','เงียะ'}, ['ぎゅ'] = {'กิว','งิว'}, ['ぎょ'] = {'เกียว','เงียว'},
	['じゃ'] = {'¤จะ'}, ['じゅ'] = {'จุ'}, ['じぇ'] = {'เจะ'}, ['じょ'] = {'โจะ'},
	['ぢゃ'] = {'¤จฺะ'}, ['ぢゅ'] = {'จฺ'..'ุ'}, ['ぢぇ'] = {'เจฺะ'}, ['ぢょ'] = {'โจฺะ'},
	['でぃ'] = {'ดิ'},
	['どぅ'] = {'ดุ'},
	['びゃ'] = {'เบียะ'}, ['びゅ'] = {'บิว'}, ['びょ'] = {'เบียว'},
	['ぴゃ'] = {'เพียะ','เปียะ'}, ['ぴゅ'] = {'พิว','ปิว'}, ['ぴょ'] = {'เพียว','เปียว'},

	['ア'] = {'¤อะ'}, ['イ'] = {'อิ'}, ['ウ'] = {'อุ'}, ['エ'] = {'เอะ'}, ['オ'] = {'โอะ'},
	['カ'] = {'¤คะ','¤กะ'}, ['キ'] = {'คิ','กิ'}, ['ク'] = {'คุ','กุ'}, ['ケ'] = {'เคะ','เกะ'}, ['コ'] = {'โคะ','โกะ'},
	['サ'] = {'¤ซะ'}, ['シ'] = {'ชิ'}, ['ス'] = {'ซุ'}, ['セ'] = {'เซะ'}, ['ソ'] = {'โซะ'},
	['タ'] = {'¤ทะ','¤ตะ'}, ['チ'] = {'ชฺิ'}, ['ツ'] = {'สึ'}, ['テ'] = {'เทะ','เตะ'}, ['ト'] = {'โทะ','โตะ'},
	['ナ'] = {'¤นะ'}, ['ニ'] = {'นิ'}, ['ヌ'] = {'นุ'}, ['ネ'] = {'เนะ'}, ['ノ'] = {'โนะ'},
	['ハ'] = {'¤ฮะ'}, ['ヒ'] = {'ฮิ'}, ['フ'] = {'ฟุ'}, ['ヘ'] = {'เฮะ'}, ['ホ'] = {'โฮะ'},
	['マ'] = {'¤มะ'}, ['ミ'] = {'มิ'}, ['ム'] = {'มุ'}, ['メ'] = {'เมะ'}, ['モ'] = {'โมะ'},
	['ヤ'] = {'¤ยะ'}, ['ユ'] = {'ยุ'}, ['ヨ'] = {'โยะ'},
	['ラ'] = {'¤ระ'}, ['リ'] = {'ริ'}, ['ル'] = {'รุ'}, ['レ'] = {'เระ'}, ['ロ'] = {'โระ'},
	['ワ'] = {'¤วะ'}, ['ヰ'] = {'วิ'}, ['ヱ'] = {'เวะ'}, ['ヲ'] = {'โวะ'},
	['ン'] = {'ง~'}, ['ッ'] = {'ต~'},

	['ヴ'] = {'วฺ'..'ุ'}, 
	['ガ'] = {'¤กะ','¤งะ'}, ['ギ'] = {'กิ','งิ'}, ['グ'] = {'กุ','งุ'}, ['ゲ'] = {'เกะ','เงะ'}, ['ゴ'] = {'โกะ','โงะ'},
	['ザ'] = {'¤ซฺะ'}, ['ジ'] = {'จิ'}, ['ズ'] = {'ซฺ'..'ุ'}, ['ゼ'] = {'เซฺะ'}, ['ゾ'] = {'โซฺะ'},
	['ダ'] = {'¤ดะ'}, ['ヂ'] = {'จฺิ'}, ['ヅ'] = {'สฺึ'}, ['デ'] = {'เดะ'}, ['ド'] = {'โดะ'},
	['バ'] = {'¤บะ'}, ['ビ'] = {'บิ'}, ['ブ'] = {'บุ'}, ['ベ'] = {'เบะ'}, ['ボ'] = {'โบะ'},
	['パ'] = {'¤พะ','¤ปะ'}, ['ピ'] = {'พิ','ปิ'}, ['プ'] = {'พุ','ปุ'}, ['ペ'] = {'เพะ','เปะ'}, ['ポ'] = {'โพะ','โปะ'},
	['ヷ'] = {'¤วฺะ'}, ['ヸ'] = {'วฺิ'}, ['ヹ'] = {'เวฺะ'}, ['ヺ'] = {'โวฺะ'},

	['ウァ'] = {'¤วะ'}, ['ウィ'] = {'วิ'}, ['ウゥ'] = {'วุ'}, ['ウェ'] = {'เวะ'}, ['ウォ'] = {'โวะ'},
	['キャ'] = {'เคียะ','เกียะ'}, ['キュ'] = {'คิว','กิว'}, ['キョ'] = {'เคียว','เกียว'},
	['シャ'] = {'¤ชะ'}, ['シュ'] = {'ชุ'}, ['シェ'] = {'เชะ'}, ['ショ'] = {'โชะ'},
	['チャ'] = {'¤ชฺะ'}, ['チュ'] = {'ชฺ'..'ุ'}, ['チェ'] = {'เชฺะ'}, ['チョ'] = {'โชฺะ'},
	['ツァ'] = {'¤สะ'}, ['ツィ'] = {'สิ'}, ['ツェ'] = {'เสะ'}, ['ツォ'] = {'โสะ'},
	['ティ'] = {'ทิ','ติ'},
	['トゥ'] = {'ทุ','ตุ'},
	['ニャ'] = {'เนียะ'}, ['ニュ'] = {'นิว'}, ['ニョ'] = {'เนียว'},
	['ヒャ'] = {'เฮียะ'}, ['ヒュ'] = {'ฮิว'}, ['ヒョ'] = {'เฮียว'},
	['ファ'] = {'¤ฟะ'}, ['フィ'] = {'ฟิ'}, ['フェ'] = {'เฟะ'}, ['フォ'] = {'โฟะ'},
	['ミャ'] = {'เมียะ'}, ['ミュ'] = {'มิว'}, ['ミョ'] = {'เมียว'},
	['リャ'] = {'เรียะ'}, ['リュ'] = {'ริว'}, ['リョ'] = {'เรียว'},

	['ヴァ'] = {'¤วฺะ'}, ['ヴィ'] = {'วฺิ'}, ['ヴェ'] = {'เวฺะ'}, ['ヴォ'] = {'โวฺะ'},
	['ギャ'] = {'เกียะ','เงียะ'}, ['ギュ'] = {'กิว','งิว'}, ['ギョ'] = {'เกียว','เงียว'},
	['ジャ'] = {'¤จะ'}, ['ジュ'] = {'จุ'}, ['ジェ'] = {'เจะ'}, ['ジョ'] = {'โจะ'},
	['ヂャ'] = {'¤จฺะ'}, ['ヂュ'] = {'จฺ'..'ุ'}, ['ヂェ'] = {'เจฺะ'}, ['ヂョ'] = {'โจฺะ'},
	['ディ'] = {'ดิ'},
	['ドゥ'] = {'ดุ'},
	['ビャ'] = {'เบียะ'}, ['ビュ'] = {'บิว'}, ['ビョ'] = {'เบียว'},
	['ピャ'] = {'เพียะ','เปียะ'}, ['ピュ'] = {'พิว','ปิว'}, ['ピョ'] = {'เพียว','เปียว'},
}

local punc = {
	[' '] = ' ', ['、'] = ', ', ['。'] = '. ', ['゠'] = '-',
	['「'] = ' ‘', ['」'] = '’ ', ['『'] = ' “', ['』'] = '” ',
	[','] = ', ', ['・'] = '·',
	[':'] = ':', ['!'] = '!', ['?'] = '?',
}

local adjust1 = {
	{'¤ฮะ$', '¤วะ'},
	{'¤ฮะ([%s%p])', '¤วะ%1'},
}

local adjust2 = {
	{'¤(.ฺ?)ะ¤อะ', '¤%1า'},
	{'(.ฺ?)ิอิ', '%1ี'},
	{'(.ฺ?)ุอุ', '%1ู'},
	{'(.ฺ?)ึอุ', '%1ือ'},
	{'เ(.ฺ?)ะเอะ', 'เ%1'},
	{'เ(.ฺ?)ะอิ', 'เ%1'},
	{'โ(.ฺ?)ะโอะ', 'โ%1'},
	{'โ(.ฺ?)ะอุ', 'โ%1'},
	{'เ(.ฺ?)ียะ¤อะ', 'เ%1ีย'},
	{'(.ฺ?)ิวอุ', '%1ีว'},
	{'เ(.ฺ?)ียวโอะ', 'เ%1ียว'}, -- not changed
	{'เ(.ฺ?)ียวอุ', 'เ%1ียว'}, -- not changed

	{'¤(.ฺ?)ะー', '¤%1า'},
	{'(.ฺ?)ิー', '%1ี'},
	{'(.ฺ?)ุー', '%1ู'},
	{'(.ฺ?)ึー', '%1ือ'},
	{'เ(.ฺ?)ะー', 'เ%1'},
	{'โ(.ฺ?)ะー', 'โ%1'},
	{'เ(.ฺ?)ียะー', 'เ%1ีย'},
	{'(.ฺ?)ิวー', '%1ีว'},
	{'เ(.ฺ?)ียวー', 'เ%1ียว'}, -- not changed

	{'¤(.ฺ?)ะ(.~)', '%1ั%2'},
	{'เ(.ฺ?)ะ(.~)', 'เ%1็%2'},
	{'โ(.ฺ?)ะ(.~)', '%1%2'},

	{'ง~([¤เโ]?)([บปพม])', 'ม~%1%2'},
	{'ง~([¤เโ]?)([จชซดตทนยรส])', 'น~%1%2'},

	{'ต~([¤เโ]?)([กง])', 'ก~%1%2'},
	{'ต~([¤เโ]?)(บ)', 'บ~%1%2'},
	{'ต~([¤เโ]?)(ป)', 'ป~%1%2'},
	{'ต~([¤เโ]?)(ด)', 'ด~%1%2'},
	{'ต~([¤เโ]?)(ซ)', 'ส~%1%2'},
	{'ต~([¤เโ]?)(ช)', 'ส~%1%2'},
	{'ส~([¤เโ]?)(ชฺ)', 'ต~%1%2'}, -- chaining

	{'[¤~^]', ''},
}

function export.tr(text, lang, sc, debug_mode)

	if type(text) == 'table' then -- called directly from a template
		text = text.args[1]
	end

	-- first kana
	text = gsub(text, '^' .. all_kana, function(k)
		return kana[k][1]
	end)
	text = gsub(text, '([%s%p])' .. all_kana, function(sp, k)
		return sp .. kana[k][1]
	end)

	-- following kana
	text = gsub(text, all_kana, function(k)
		return kana[k][2] or kana[k][1]
	end)

	text = gsub(text, '.', punc)

	if text ~= '¤ฮะ' then -- if it is not the only word ha
		for _,v in ipairs(adjust1) do
			text = gsub(text, v[1], v[2])
		end
	end
	for _,v in ipairs(adjust2) do
		text = gsub(text, v[1], v[2])
	end

	return text

end

return export