Skip to content

Instantly share code, notes, and snippets.

@davidglezz
Last active March 24, 2025 17:09
Show Gist options
  • Save davidglezz/e98a91fef84bf2b5361c3ea5d8d58304 to your computer and use it in GitHub Desktop.
Save davidglezz/e98a91fef84bf2b5361c3ea5d8d58304 to your computer and use it in GitHub Desktop.
Replace v-t="x" attribute with $t(x) function in vue files
const fs = require('node:fs')
const path = require('node:path')
const glob = require('glob')
// Find all Vue files
const vueFiles = glob.sync(path.join(__dirname, '../**/*.vue'))
vueFiles.forEach(filePath => {
let content = fs.readFileSync(filePath, 'utf8')
// Regex to match self-closing tags with v-t attribute
const regex = /<([a-zA-Z0-9-]+)([^>]*?)v-t="([^"]+)"([^>]*?)(\/>)/gi
// Replace with the new syntax
const newContent = content.replace(
regex,
(_match, tag, beforeAttrs, value, afterAttrs) =>
`<${tag}${beforeAttrs}${afterAttrs}>{{ $t(${value}) }}</${tag}>`,
)
// Only write back if changes were made
if (content !== newContent) {
console.log(`Updating: ${filePath}`)
fs.writeFileSync(filePath, newContent, 'utf8')
}
})
console.log('Replacement complete!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment