Skip to content

Commit

Permalink
update chat-preset trans
Browse files Browse the repository at this point in the history
  • Loading branch information
biuuu committed Oct 16, 2018
1 parent 639f6db commit 9d4f63c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
14 changes: 12 additions & 2 deletions extension/modules/chat-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@ import getChatData from '../store/chat-preset'

export default async function transChat(data) {
if (!data.chat) return data
const chatMap = await getChatData()
const { chatMap, nChatMap } = await getChatData()
for (let key in data.chat) {
let item = data.chat[key]
for (let ck in item) {
let id = item[ck].chat_id
if (chatMap.has(id)) {
item[ck].text = chatMap.get(id)
let hasSpecialTrans = false
if (nChatMap.has(id)) {
const { text, trans } = nChatMap.get(id)
if (item[ck].text === text) {
item[ck].text = trans
hasSpecialTrans = true
}
}
if (!hasSpecialTrans) {
item[ck].text = chatMap.get(id)
}
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions extension/store/chat-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import parseCsv from '../utils/parseCsv'
import { getLocalData, setLocalData } from './local-data'

const chatMap = new Map()
const nChatMap = new Map()
let loaded = false

const trim = (str) => {
Expand All @@ -20,15 +21,24 @@ const getChatData = async () => {
const list = parseCsv(csv)
list.forEach(item => {
const id = trim(item.id)
const text = trim(item.text)
const trans = trim(item.trans)
if (id && trans) {
chatMap.set(id, trans)
if (/\d+-n/.test(id)) {
const rgs = id.match(/(\d+)-n/)
const _id = rgs[1]
nChatMap.set(_id, {
text, trans
})
} else {
chatMap.set(id, trans)
}
}
})
loaded = true
}

return chatMap
return { chatMap, nChatMap }
}

export default getChatData
2 changes: 1 addition & 1 deletion extension/version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"ver": "0.12.3"
"ver": "0.12.4"
}

0 comments on commit 9d4f63c

Please sign in to comment.