Skip to content

Commit

Permalink
extension support langMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
biuuu committed Sep 3, 2018
1 parent f6a8054 commit fbe6ada
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 484 deletions.
15 changes: 15 additions & 0 deletions extension/modules/langMsg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import getLangMsgData from '../store/lang-msg'

export default async function transLangMsg(data, pathname) {
if (!data.option || !data.option.langMsg) return data

const msgs = data.option.langMsg
const langMsgMap = await getLangMsgData()
for (let key of Object.keys(msgs)) {
const msg = langMsgMap.get(`${key}${msgs[key].msg}`)
if (msg && msg.trans) {
msgs[key].msg = msg.trans
}
}
return data
}
66 changes: 66 additions & 0 deletions extension/store/lang-msg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import fetchData from '../fetch'
import { getLocalData, setLocalData } from './local-data'
import parseCsv from '../utils/parseCsv'
import sortKeywords from '../utils/sortKeywords'

const langMsgMap = new Map()
let loaded = false

const nameWithScenario = (list, key = 'name') => {
const newList = []
const keys = []
list.forEach(item => {
const existIdx = keys.indexOf(item[key])
if (existIdx !== -1) {
const obj = newList[existIdx]
if (item.scenario) {
obj[item.scenario] = item
obj.scenarios.push(item.scenario)
} else {
obj.trans = item.trans
obj.noun = !!item.noun
}
} else {
const obj = { [key]: item[key], scenarios: [] }
if (item.scenario) {
obj[item.scenario] = item
obj.scenarios.push(item.scenario)
} else {
obj.trans = item.trans
obj.noun = !!item.noun
}
newList.push(obj)
keys.push(item[key])
}
})
return newList
}

const getLangMsgData = async () => {
if (!loaded) {
let langMsg = getLocalData('langMsg')
if (!langMsg) {
langMsg = await fetchData('/blhxfy/data/lang-msg.csv')
setLocalData('langMsg', langMsg)
}
const list = parseCsv(langMsg)
list.forEach(item => {
if (item.id && item.id.trim()) {
item.en && langMsgMap.set(`${item.id}${item.en}`, {
trans: item.trans,
en: item.en,
jp: item.jp
})
item.jp && langMsgMap.set(`${item.id}${item.jp}`, {
trans: item.trans,
en: item.en,
jp: item.jp
})
}
})
loaded = true
}
return langMsgMap
}

export default getLangMsgData
14 changes: 3 additions & 11 deletions extension/store/name-npc.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,8 @@ const nameWithScenario = (list, key = 'name') => {

const getNameData = async () => {
if (!loaded) {
let nameEn = getLocalData('nameEn')
let nameJp = getLocalData('nameJp')
if (!nameEn) {
nameEn = await fetchData('/blhxfy/data/npc-name-en.csv')
setLocalData('nameEn', nameEn)
}
if (!nameJp) {
nameJp = await fetchData('/blhxfy/data/npc-name-jp.csv')
setLocalData('nameJp', nameJp)
}
const nameEn = await fetchData('/blhxfy/data/npc-name-en.csv')
const nameJp = await fetchData('/blhxfy/data/npc-name-jp.csv')
const listEn = nameWithScenario(parseCsv(nameEn))
const listJp = nameWithScenario(parseCsv(nameJp))
sortKeywords(listEn).forEach(item => {
Expand All @@ -62,4 +54,4 @@ const getNameData = async () => {
return { enNameMap, jpNameMap }
}

export default getNameData
export default getNameData
8 changes: 6 additions & 2 deletions extension/translate.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import URI from 'urijs'
import transScenario from './modules/scenario'
import transLangMsg from './modules/langMsg'
import getUserName from './store/name-user'

const apiHosts = ['game.granbluefantasy.jp', 'gbf.game.mbga.jp']
Expand All @@ -18,8 +19,11 @@ export default async function translate(state) {
if (apiHosts.indexOf(hostname) === -1) return
if (pathname.includes('scenario')) {
data = await transScenario(data, pathname)
} else if (pathname.includes('/profile/content/index/')) {
getUserName(data)
} else if (pathname.includes('/content/')) {
if (pathname.includes('/profile/content/index/')) {
getUserName(data)
}
data = await transLangMsg(data, pathname)
} else {
return
}
Expand Down
6 changes: 3 additions & 3 deletions extension/utils/parseCsv.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import CSV from 'papaparse'
import CSV from 'comma-separated-values'

const parseCsv = (str) => {
try {
return CSV.parse(str.replace(/^\ufeff/, ''), { header: true }).data
return CSV.parse(str.replace(/^\ufeff/, ''), { header: true })
} catch (err) {
console.error(err)
return {}
}
}

export default parseCsv
export default parseCsv
3 changes: 3 additions & 0 deletions extension/version.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"ver": "0.4.0"
}
3 changes: 1 addition & 2 deletions packCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const CONFIG = require('./config')
const zip = require('gulp-zip')
const rollup = require('rollup')
const babel = require('rollup-plugin-babel')
const nodeBti = require('rollup-plugin-node-builtins')
const cmjs = require('rollup-plugin-commonjs')
const resolve = require('rollup-plugin-node-resolve')
const through = require('through2')
Expand Down Expand Up @@ -133,7 +132,7 @@ gulp.task('publish', ['md5'], function (done) {
})
})

const extensionVer = '0.3'
const extensionVer = require('./extension/version.json').ver
const extensionBanner = `// ==UserScript==
// @name 碧蓝幻想翻译
// @namespace https://github.com/biuuu/BLHXFY
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "blhxfy",
"version": "0.6.5",
"version": "0.6.6",
"description": "碧蓝幻想微风机翻团",
"main": "main.js",
"scripts": {
Expand All @@ -21,6 +21,7 @@
"anyproxy": "^4.0.5",
"axios": "^0.17.1",
"chokidar": "^2.0.4",
"comma-separated-values": "^3.6.4",
"core-js": "^2.5.7",
"decompress-zip": "^0.3.1",
"del": "^3.0.0",
Expand All @@ -41,7 +42,6 @@
"rollup": "^0.64.1",
"rollup-plugin-babel": "^4.0.0-beta.8",
"rollup-plugin-commonjs": "^9.1.5",
"rollup-plugin-node-builtins": "^2.1.2",
"rollup-plugin-node-resolve": "^3.3.0",
"st": "^1.2.2",
"urijs": "^1.19.0",
Expand Down
Loading

0 comments on commit fbe6ada

Please sign in to comment.