Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@angelikatyborska
Last active November 24, 2021 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelikatyborska/fecf65beded87d66e280dd331c6ab008 to your computer and use it in GitHub Desktop.
Save angelikatyborska/fecf65beded87d66e280dd331c6ab008 to your computer and use it in GitHub Desktop.
const { spawnSync } = require('child_process')
const fs = require('fs')
const path = require('path')
const os = require('os')
const glob = require('glob')
// this loader "imports" a .pot file by compiling all of the .po files under the same domain
// into a JSON with translations that can be used by vue-gettext
//
// E.g.:
// import translations from '../../../../priv/gettext/domain_name.pot'
// Vue.use(GetTextPlugin, {translations})
module.exports = {
default: function () {
// resource is a full path to the imported file, e.g. /home/user/code/my_app/priv/gettext/messages.pot
const resource = this.resourcePath
const domain = resource.match(/\/(?<domain>[^/]*)\.pot$/).groups.domain
// create temp dir because gettext-compile must write to a file
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'my_app_'))
const tempFilePath = `${tempDir}/${domain}.json`
const files = glob.sync(`../priv/gettext/**/LC_MESSAGES/${domain}.po`)
const process = spawnSync('yarn', ['gettext-compile', '--output', tempFilePath, ...files])
if (process.status === 0) {
const result = fs.readFileSync(tempFilePath)
return `module.exports = ${result}`
} else {
throw process.stderr.toString()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment