Skip to content

Commit

Permalink
Build translations from Markdown files
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-roesch committed Jul 16, 2020
1 parent cad5124 commit 0333da0
Show file tree
Hide file tree
Showing 65 changed files with 246 additions and 98 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
node_modules
/dist
/build

# local env files
.env.local
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
FROM node:10 as build-stage
COPY ./ /app
WORKDIR /app
RUN yarn install && yarn run build
RUN yarn install && yarn run dumpLangFiles && cp /app/build/lang/* /app/src/lang && yarn run build

FROM nginx:stable-alpine
RUN mkdir /app
Expand Down
87 changes: 87 additions & 0 deletions bin/compile-lang-jsons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/* eslint-disable no-console */
const fs = require('fs')

const destPath = './build/translations'

fs.mkdirSync(destPath, { recursive: true })

const langs = fs.readdirSync('./translations', { withFileTypes: true })
langs.forEach((entry) => {
if (!entry.isDirectory()) {
return
}

handleLang(entry.name)
})

function handleLang (lang) {
console.log(`Compiling language JSON for locale '${lang}'`)

const path = `./src/lang/${lang}.json`

if (!fs.existsSync(path)) {
console.error(`Could not find language JSON for locale '${lang}'`)
process.exit(1)
}

const messages = JSON.parse(fs.readFileSync(path, 'utf8'))

messages.events = buildLangEntries(lang, 'events', parseEventFile)
messages.locations = buildLangEntries(lang, 'locations', parseLocationFile)

const langDestPath = `${destPath}/${lang}.json`

fs.writeFileSync(langDestPath, JSON.stringify(messages, undefined, 4))

console.log(`Wrote language JSON for locale '${lang}' to '${langDestPath}'`)
}

function buildLangEntries (lang, type, parser) {
const dirPath = `./lang/${lang}/${type}`

if (!fs.existsSync(dirPath)) {
return undefined
}

const result = {}

console.log(`Compiling translations of ${type} for locale '${lang}'...`)

fs.readdirSync(dirPath, { withFileTypes: true }).filter(e => e.isFile() && e.name.endsWith('.md')).forEach((entry) => {
const entryId = entry.name.substring(0, entry.name.lastIndexOf('.md'))

result[entryId] = parser(lang, entryId, fs.readFileSync(`${dirPath}/${entry.name}`, 'utf8'))
})

return result
}

function parseEventFile (lang, id, content) {
if (!content.startsWith('#')) {
console.error(`Translation of event ${id} for locale '${lang}' does not start with name as Markdown heading`)
process.exit(1)
}

const firstNewLine = content.indexOf('\n')

const name = content.substring(1, firstNewLine).trim()
const detailsHeadingPos = content.indexOf('\n## Details')
const blurb = content.substring(firstNewLine, detailsHeadingPos !== -1 ? detailsHeadingPos : undefined).trim()
const details = detailsHeadingPos !== -1 ? content.substring(content.indexOf('\n', detailsHeadingPos + 1)).trim() : undefined

return { name, blurb, details }
}

function parseLocationFile (lang, id, content) {
if (!content.startsWith('#')) {
console.error(`Translation of location ${id} for locale '${lang}' does not start with name as Markdown heading`)
process.exit(1)
}

const firstNewLine = content.indexOf('\n')

const name = content.substring(1, firstNewLine).trim()
const details = content.substring(firstNewLine).trim()

return { name, details }
}
34 changes: 34 additions & 0 deletions bin/dump-lang-files.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable no-console */
const fs = require('fs')

const events = JSON.parse(fs.readFileSync('./src/store/events.json', 'utf8'))
events.forEach((event) => {
const path = `./translations/en/events/${event.id}.md`

if (fs.existsSync(path)) {
return
}

let data = `# ${event.id}\nBlurb\n`

if (event.details) {
data += '\n## Details\nDetails\n'
}

fs.writeFileSync(path, data)

console.log(`Created new translation file for event ${event.id}`)
})

const locations = JSON.parse(fs.readFileSync('./src/store/locations.json', 'utf8'))
Object.values(locations).forEach((location) => {
const path = `./translations/en/locations/${location.id}.md`

if (fs.existsSync(path)) {
return
}

fs.writeFileSync(path, `# ${location.id}\nDetails\n`)

console.log(`Created new translation file for location ${location.id}`)
})
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
"lint": "vue-cli-service lint",
"dumpLangFiles": "node ./bin/dump-lang-files.js",
"compileLangJsons": "node ./bin/compile-lang-jsons.js"
},
"dependencies": {
"@johmun/vue-tags-input": "^2.1.0",
Expand Down
5 changes: 0 additions & 5 deletions src/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,5 @@
"ed": "Die Tänzerin am Abgrund",
"ob": "Der Ruf der Klingen & Die Splitter der Macht",
"row": "Rhythm of War"
},
"events": {
"shattering": {
"name": "Shattering of Adonalsium"
}
}
}
91 changes: 0 additions & 91 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,96 +33,5 @@
"ed": "Edgedancer",
"ob": "Oathbringer",
"row": "Rhythm of War"
},
"events": {
"shattering": {
"name": "Shattering of Adonalsium",
"blurb": "Adonalsium was shattered into 16 Shards at this event"
},
"ashyn-exodus": {
"name": "Exodus from Ashyn"
}
},
"locations": {
"alethkar": {
"name": "Alethkar",
"details": "Alethkar is the largest and most prominent of the five Vorin nations on Roshar. People from Alethkar are known as Alethi, and their royal colors are blue and gold. Prior to the True Desolation, they were engaged in the War of Reckoning against the listeners. At the present, vast majority of their territory is under control of the Voidbringers."
},
"jah-keved": {
"name": "Jah Keved"
},
"herdaz": {
"name": "Herdaz"
},
"kharbranth": {
"name": "Kharbranth",
"details": "Kharbranth, City of Bells, is an independent city-state on Roshar, ruled by King Taravangian. It is one of the five Vorin nations and home to the Palanaeum, the largest library on Roshar. Kharbranth is known for the quality of its hospitals, and people come from all over the world to study medicine from Kharbranth's surgeons. The Kharbranthian royal colors are burnt orange and white, and have become synonymous around Roshar as the colors of surgeons, medicine, and healing."
},
"thaylenah": {
"name": "Thaylenah"
},
"azir": {
"name": "Azir"
},
"shinovar": {
"name": "Shinovar"
},
"aimia": {
"name": "Aimia"
},
"iri": {
"name": "Iri"
},
"rira": {
"name": "Rira"
},
"reshi-isles": {
"name": "Reshi Isles"
},
"urithiru": {
"name": "Urithiru"
},
"shattered-plains": {
"name": "The Shattered Plains"
},
"tashikk": {
"name": "Tashikk"
},
"emul": {
"name": "Emul"
},
"tukar": {
"name": "Tukar"
},
"marat": {
"name": "Marat"
},
"liafor": {
"name": "Liafor"
},
"babatharnam": {
"name": "Babatharnam"
},
"marabethia": {
"name": "Marabethia"
},
"purelake": {
"name": "Purelake"
},
"kholinar": {
"name": "Kholinar"
},
"rathalas": {
"name": "Rathalas"
},
"new-natanatan": {
"name": "New Natanatan"
},
"thaylen-city": {
"name": "Thaylen City"
},
"valley": {
"name": "The Valley"
}
}
}
6 changes: 6 additions & 0 deletions translations/en/events/adolin-birth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# adolin-birth

Blurb

## Details
Adolin was born in 1120, like the good lad that he is
2 changes: 2 additions & 0 deletions translations/en/events/ashyn-exodus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ashyn-exodus
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/conquest-of-alethkar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# conquest-of-alethkar
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/dalinar-birth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# dalinar-birth
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/dalinar-nightwatcher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# dalinar-nightwatcher
Blurb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# dalinar-opens-honors-perpendicularity
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/dalinar-wins-oathbringer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# dalinar-wins-oathbringer
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/discovery-of-parshendi.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# discovery-of-parshendi
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/end-of-hierocracy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# end-of-hierocracy
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/evi-death.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# evi-death
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/false-desolation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# false-desolation
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/founding-knights-radiant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# founding-knights-radiant
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/gavilar-assassination.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gavilar-assassination
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/gavilar-coronation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# gavilar-coronation
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/kaladin-birth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# kaladin-birth
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/kaladin-joins-army.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# kaladin-joins-army
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/kaladin-kills-helaran.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# kaladin-kills-helaran
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/last-desolation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# last-desolation
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/lin-davar-murder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# lin-davar-murder
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/roshone-arrival.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# roshone-arrival
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/scouring-of-amia.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# scouring-of-amia
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/shallan-arrives-in-kharbranth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# shallan-arrives-in-kharbranth
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/shallan-birth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# shallan-birth
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/shallan-kills-father.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# shallan-kills-father
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/shallan-kills-mother.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# shallan-kills-mother
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/shattering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# shattering
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/surgeon-training-begins.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# surgeon-training-begins
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/szeth-named-truthless.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# szeth-named-truthless
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/taravangian-acquires-oathstone.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# taravangian-acquires-oathstone
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/taravangian-nightwatcher.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# taravangian-nightwatcher
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/the-gang-is-in-shadesmar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# the-gang-is-in-shadesmar
Blurb
2 changes: 2 additions & 0 deletions translations/en/events/tien-death.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# tien-death
Blurb
2 changes: 2 additions & 0 deletions translations/en/locations/aimia.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Aimia
Details
2 changes: 2 additions & 0 deletions translations/en/locations/alethkar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Alethkar
Details
2 changes: 2 additions & 0 deletions translations/en/locations/azir.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Azir
Details
2 changes: 2 additions & 0 deletions translations/en/locations/babatharnam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Babatharnam
Details
2 changes: 2 additions & 0 deletions translations/en/locations/emul.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Emul
Details
2 changes: 2 additions & 0 deletions translations/en/locations/herdaz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Herdaz
Details
2 changes: 2 additions & 0 deletions translations/en/locations/iri.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Iri
Details
2 changes: 2 additions & 0 deletions translations/en/locations/jah-keved.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Jah Keved
Details
2 changes: 2 additions & 0 deletions translations/en/locations/kharbranth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Kharbranth
Details
2 changes: 2 additions & 0 deletions translations/en/locations/kholinar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Kholinar
Details
2 changes: 2 additions & 0 deletions translations/en/locations/liafor.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Liafor
Details
2 changes: 2 additions & 0 deletions translations/en/locations/marabethia.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Marabethia
Details
2 changes: 2 additions & 0 deletions translations/en/locations/marat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Marat
Details
Loading

0 comments on commit 0333da0

Please sign in to comment.