|
| 1 | +import fs from "fs" |
| 2 | +import path from "path" |
| 3 | + |
| 4 | +import i18n from "../../../../i18n.config.json" |
| 5 | +import dirs from "../../../data/crowdin/translation-buckets-dirs.json" |
| 6 | +import { CROWDIN_API_MAX_LIMIT } from "../../../lib/constants" |
| 7 | +import crowdinClient from "../api-client/crowdinClient" |
| 8 | + |
| 9 | +type SummaryItem = [code: string, bucket: string, needsReview: number] |
| 10 | + |
| 11 | +/** |
| 12 | + * Generates a report of words needing review for each content bucket in all languages. |
| 13 | + * Report in CSV format with columns: Language, Bucket Name, Words needing review. |
| 14 | + * To run: |
| 15 | + * - Ensure CROWDIN_API_KEY is set in the .env file (.env.local will not work) |
| 16 | + * 1. https://crowdin.com/settings#api-key |
| 17 | + * 2. Click: "New token" |
| 18 | + * 3. Give token a name |
| 19 | + * 4. Select "Translation Status" under "Projects" for scope |
| 20 | + * 5. Click: "Create" and authenticate |
| 21 | + * 6. Copy the token to the .env file |
| 22 | + * - Can be run with `yarn crowdin-needs-review` |
| 23 | + * - Results are saved to src/data/crowdin/bucketsAwaitingReviewReport.csv |
| 24 | + * - Report is git ignored, and should not be committed |
| 25 | + */ |
| 26 | +async function main() { |
| 27 | + const projectId = Number(process.env.CROWDIN_PROJECT_ID) || 363359 |
| 28 | + |
| 29 | + const reportSummary = [] as SummaryItem[] |
| 30 | + |
| 31 | + const directories = dirs.sort((a, b) => a.name.localeCompare(b.name)) |
| 32 | + |
| 33 | + // Loop through list of content buckets (dirs) |
| 34 | + for (const dir of directories) { |
| 35 | + console.log(`Processing: ${dir.name}...`) |
| 36 | + |
| 37 | + // Get translation progress for bucket (dir.id) in all languages |
| 38 | + const { data } = |
| 39 | + await crowdinClient.translationStatusApi.getDirectoryProgress( |
| 40 | + projectId, |
| 41 | + dir.id, |
| 42 | + { limit: CROWDIN_API_MAX_LIMIT } |
| 43 | + ) |
| 44 | + |
| 45 | + // Loop through supported languages |
| 46 | + i18n.forEach(({ crowdinCode }) => { |
| 47 | + const match = data.find( |
| 48 | + ({ data: { languageId } }) => languageId === crowdinCode |
| 49 | + ) |
| 50 | + if (!match) return |
| 51 | + const { words, translationProgress } = match.data |
| 52 | + if (translationProgress < 100) return |
| 53 | + const needsReview = words.translated - words.approved |
| 54 | + if (needsReview === 0) return |
| 55 | + // If match, 100% translation progress, and not full reviewed, add to summary |
| 56 | + reportSummary.push([crowdinCode, dir.name, needsReview]) |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + // Sort first by language code, then by bucket name |
| 61 | + const sorted = reportSummary.sort((a, b) => |
| 62 | + a[0] === b[0] ? a[1].localeCompare(b[1]) : a[0].localeCompare(b[0]) |
| 63 | + ) |
| 64 | + // Transform to çsv string |
| 65 | + const csvArray = sorted.map((item) => item.join(",")) |
| 66 | + // Insert header names at beginning of csv array |
| 67 | + csvArray.unshift("Language,Bucket Name,Words needing review") |
| 68 | + const csv = csvArray.join("\n") |
| 69 | + |
| 70 | + // Write csv to file to fs |
| 71 | + const csvPath = path.resolve( |
| 72 | + process.cwd(), |
| 73 | + "src/data/crowdin/bucketsAwaitingReviewReport.csv" |
| 74 | + ) |
| 75 | + fs.writeFileSync(csvPath, csv) |
| 76 | + |
| 77 | + // Log summary |
| 78 | + console.log("\nReport summary:\n") |
| 79 | + console.log(csv) |
| 80 | + console.log(`\n✅ Report saved to ${csvPath}`) |
| 81 | +} |
| 82 | + |
| 83 | +main() |
| 84 | + |
| 85 | +export default main |
0 commit comments