OverviewFilesCommitsIssuesDocsReleasesPeers

ollama_semantic_function_calling / ollamatools-new / keet / node_modules / @holepunchto / keet-translations / find-strings.js

102 lines · javascript ref:
import { execSync } from 'child_process'
import fs from 'fs'
import minimist from 'minimist'
import { translations } from './index.js'
const sources = translations.en.translations

// Get Arguments
//
const {
  _: { 0: path },
  json,
  save
} = minimist(process.argv.slice(2))

if (!path) {
  console.log('> Missing path\n')
  usage()
  process.exit(1)
}

// Find strings
//
try {
  const cmd = `grep -Eo 'i18n\`[^\`]+\`' -r ${path} | awk -F'\`' '{print $2}' | sort`
  const stdout = execSync(cmd).toString()

  // Remove duplicates
  const stringsDeduped = [...new Set(stdout.split('\n'))].filter(text => !!text)

  // Parse parameters
  const strings = stringsDeduped.map(text => {
    let count = 0
    return text.replaceAll(/\$\{[^}]+\}/g, () => '${' + `${count++}` + '}')
  })

  if (json) {
    log(arrayToJson(strings), false)
  } else {
    log('')
    log('+----------------------+')
    log('| Missing translations |')
    log('+----------------------+')
    log('')

    let nMissing = 0
    for (const string of strings) {
      if (!(string in sources)) {
        log(string)
        nMissing++
      }
    }

    log('')
    log('+------------------------+')
    log('| Redundant translations |')
    log('+------------------------+')
    log('')

    let nExtra = 0
    for (const source of Object.keys(sources)) {
      if (!strings.find(string => string === source)) {
        log(source)
        nExtra++
      }
    }

    log('')
    log(`> Missing ${nMissing} translations`)
    log(`> Extra ${nExtra} translations\n`)
  }
} catch (e) {
  console.error(e)
}

function arrayToJson (texts) {
  let json = '{\n'
  for (const [index, text] of texts.entries()) {
    json += `  "${text}": "${text}"`
    if (index < texts.length - 1) {
      json += ',\n'
    }
  }
  json += '\n}'

  // Validate JSON
  JSON.parse(json)

  return json
}

function log (text, append = true) {
  if (save) {
    fs.writeFileSync(save, `${text}\n`, { flag: append ? 'a' : 'w' })
  } else {
    console.log(text)
  }
}

function usage () {
  console.log('usage: find-strings path [--json] [--save output]')
}