-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.lua
executable file
·204 lines (174 loc) · 7.66 KB
/
core.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
-- "Romaji Translator" addon by tomill
local addon = LibStub("AceAddon-3.0"):NewAddon("Romaji Translator")
function addon:OnInitialize()
local defaults = {
profile = {
whisper = false,
guild = true,
party = false,
instance = false,
say = false,
bnet = false,
community = false,
}
}
local options = {
type = "group",
args = {}
}
local setter = function(info, val) addon.db.profile[ info[#info] ] = val end
local getter = function(info) return addon.db.profile[ info[#info] ] end
for k, v in pairs(defaults.profile) do
local label = k:gsub("^%l", string.upper)
options.args[k] = {
type = "toggle",
name = "Enable on [" .. label .. "] chat message.",
desc = "check suru to, [" .. label .. "] chat de ON desu.",
width = "full",
set = setter,
get = getter,
}
end
self.enabled = true
self.db = LibStub("AceDB-3.0"):New("Romaji2KanaDB", defaults)
LibStub("AceConfig-3.0"):RegisterOptionsTable(self.name, options)
LibStub("AceConfigDialog-3.0"):AddToBlizOptions(self.name)
end
SLASH_ROMAJI2KANA1 = "/romakana"
SLASH_ROMAJI2KANA2 = "/romakana"
SlashCmdList["ROMAJI2KANA"] = function (opt)
opt = string.lower(opt)
if opt == "on" then
addon.enabled = true
DEFAULT_CHAT_FRAME:AddMessage(addon.name .. ": enabled", 1, 1, 1)
elseif opt == "off" then
addon.enabled = false
DEFAULT_CHAT_FRAME:AddMessage(addon.name .. ": temporary disabled", 1, 1, 1)
else
DEFAULT_CHAT_FRAME:AddMessage(addon.name .. ": help", 1, 1, 1)
DEFAULT_CHAT_FRAME:AddMessage("/romakana off - disable in this login session", 1, 1, 1)
DEFAULT_CHAT_FRAME:AddMessage("/romakana on - enable in this login session", 1, 1, 1)
end
end
local kanamap = {
["bya"] = "びゃ", ["byo"] = "びょ", ["byu"] = "びゅ",
["cha"] = "ちゃ", ["chi"] = "ち", ["cho"] = "ちょ", ["chu"] = "ちゅ",
["dya"] = "ぢゃ",
["gya"] = "ぎゃ", ["gyo"] = "ぎょ", ["gyu"] = "ぎゅ",
["hya"] = "ひゃ", ["hyo"] = "ひょ", ["hyu"] = "ひゅ",
["kya"] = "きゃ", ["kyo"] = "きょ", ["kyu"] = "きゅ",
["mya"] = "みゃ", ["myo"] = "みょ", ["myu"] = "みゅ",
["nya"] = "にゃ", ["nyo"] = "にょ", ["nyu"] = "にゅ",
["rya"] = "りゃ", ["ryo"] = "りょ", ["ryu"] = "りゅ",
["sha"] = "しゃ",
["shi"] = "し", ["sho"] = "しょ", ["shu"] = "しゅ",
["sya"] = "しゃ", ["syo"] = "しょ", ["syu"] = "しゅ",
["tsu"] = "つ",
["tya"] = "ちゃ", ["tyo"] = "ちょ", ["tyu"] = "ちゅ",
["ba"] = "ば", ["be"] = "べ", ["bi"] = "び", ["bo"] = "ぼ", ["bu"] = "ぶ",
["da"] = "だ", ["de"] = "で", ["di"] = "でぃ", ["do"] = "ど", ["du"] = "どぅ",
["fu"] = "ふ",
["ga"] = "が", ["ge"] = "げ", ["gi"] = "ぎ", ["go"] = "ご", ["gu"] = "ぐ",
["ha"] = "は", ["he"] = "へ", ["hi"] = "ひ", ["ho"] = "ほ", ["hu"] = "ふ",
["ja"] = "じゃ", ["ji"] = "じ", ["jo"] = "じょ", ["ju"] = "じゅ",
["ka"] = "か", ["ke"] = "け", ["ki"] = "き", ["ko"] = "こ", ["ku"] = "く",
["ma"] = "ま", ["me"] = "め", ["mi"] = "み", ["mo"] = "も", ["mu"] = "む",
["na"] = "な", ["ne"] = "ね", ["ni"] = "に", ["no"] = "の", ["nu"] = "ぬ",
["pa"] = "ぱ", ["pe"] = "ぺ", ["pi"] = "ぴ", ["po"] = "ぽ", ["pu"] = "ぷ",
["ra"] = "ら", ["re"] = "れ", ["ri"] = "り", ["ro"] = "ろ", ["ru"] = "る",
["sa"] = "さ", ["se"] = "せ", ["si"] = "し", ["so"] = "そ", ["su"] = "す",
["ta"] = "た", ["te"] = "て", ["ti"] = "ち", ["to"] = "と", ["tu"] = "つ",
["wa"] = "わ", ["wo"] = "を",
["ya"] = "や", ["yo"] = "よ", ["yu"] = "ゆ",
["za"] = "ざ", ["ze"] = "ぜ", ["zi"] = "じ", ["zo"] = "ぞ", ["zu"] = "ず",
["a"] = "あ", ["e"] = "え", ["i"] = "い", ["o"] = "お", ["u"] = "う",
}
local function convertWord(original)
local word = original
if string.match(word, "^[(<%[].*[)>%]]$") then
return original
end
if string.match(word, "^[(][^)]+[)]:") then
return "" -- incognito line
end
if string.match(word, "^%A*%u") then
return original
end
word = string.lower(word)
if ROMAJI2KANA_STOPWORDS[ string.gsub(word, "%A", "") ] then
return original
end
word = string.gsub(word, "(w+)$", function(w)
local www = ""
for i = 1, #w do
www = www .. "w"
end
return www
end)
word = string.gsub(word, "([bcdfghjkprstwyz])%1", "っ%1")
word = string.gsub(word, "([bcdghkmnprst][hsy][aeiou])", function(w) return kanamap[w] end)
word = string.gsub(word, "([bdfghjkmnprstwyz][aeiou])", function(w) return kanamap[w] end)
word = string.gsub(word, "([aeiou])", function(w) return kanamap[w] end)
word = string.gsub(word, "nn?", "ん")
if string.match(word, "%a") then
return original
end
return word
end
local function convertMessage(msg)
if not string.find(msg, "^[%a%d%s%p]+$") then
return msg -- already includes non ascii. maybe kana. just skip
end
local res
local count = { ["word"] = 0, ["kana"] = 0 }
for word in string.gmatch(msg, "%S+") do
local kana = convertWord(word)
count.word = count.word + 1
if kana ~= word then
count.kana = count.kana + 1
end
if res then
res = res .. " " .. kana
else
res = kana
end
end
if count.kana == 0 then
return msg -- something failed
elseif ( count.kana / count.word ) <= 0.2 then
return msg -- something failed
else
return string.format("%s (%s)", msg, res)
end
end
local function hookMessage(self, event, msg, ...)
if not addon.enabled then
return
end
if (event == "CHAT_MSG_WHISPER" and addon.db.profile.whisper) or
(event == "CHAT_MSG_COMMUNITIES_CHANNEL" and addon.db.profile.community) or
(event == "CHAT_MSG_BN_WHISPER" and addon.db.profile.bnet) or
(event == "CHAT_MSG_SAY" and addon.db.profile.say) or
(event == "CHAT_MSG_GUILD" and addon.db.profile.guild) or
(event == "CHAT_MSG_PARTY" and addon.db.profile.party) or
(event == "CHAT_MSG_PARTY_LEADER" and addon.db.profile.party) or
(event == "CHAT_MSG_RAID" and addon.db.profile.instance) or
(event == "CHAT_MSG_RAID_LEADER" and addon.db.profile.instance) or
(event == "CHAT_MSG_INSTANCE_CHAT" and addon.db.profile.instance) or
(event == "CHAT_MSG_RAID_WARNING" and addon.db.profile.instance) or
(event == "CHAT_MSG_INSTANCE_CHAT_LEADER" and addon.db.profile.instance) then
return false, convertMessage(msg), ...
end
end
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_COMMUNITIES_CHANNEL", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_BN_WHISPER", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_GUILD", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY_LEADER", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID_LEADER", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID_WARNING", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_INSTANCE_CHAT", hookMessage)
ChatFrame_AddMessageEventFilter("CHAT_MSG_INSTANCE_CHAT_LEADER", hookMessage)