Last active
November 24, 2021 22:17
-
-
Save angelikatyborska/fecf65beded87d66e280dd331c6ab008 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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