()
| 104 | } |
| 105 | |
| 106 | async function writeReport() { |
| 107 | const report = {}; |
| 108 | const defaultTranslationsFile = translationsPath(defaultLocale); |
| 109 | const defaultTranslations = readJsonSync(defaultTranslationsFile); |
| 110 | for (const locale of allLocales) { |
| 111 | const translationsFile = translationsPath(locale); |
| 112 | const translations = readJsonSync(translationsFile); |
| 113 | const localeReport = (report[locale] = { |
| 114 | translated: [], |
| 115 | untranslated: [], |
| 116 | }); |
| 117 | for (const [id, message] of Object.entries(defaultTranslations)) { |
| 118 | if (translations[id]) { |
| 119 | localeReport.translated.push([id, message]); |
| 120 | } else if (!/\{[a-z]+\}/.test(message)) { |
| 121 | localeReport.untranslated.push([id, message]); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | const languageName = new Intl.DisplayNames("en", { type: "language" }); |
| 126 | const countWords = (count, [id, message]) => |
| 127 | count + |
| 128 | message |
| 129 | .replaceAll(/\{[a-zA-Z]}+/g, "") |
| 130 | .replaceAll(/<\/?[a-z]+>/g, "") |
| 131 | .match(/\p{L}+/gu).length; |
| 132 | const lines = []; |
| 133 | for (const [locale, { translated, untranslated }] of Object.entries(report)) { |
| 134 | lines.push(`# ${languageName.of(locale)}`); |
| 135 | lines.push(``); |
| 136 | lines.push( |
| 137 | `Translated: ` + |
| 138 | `${translated.length} messages, ` + |
| 139 | `${translated.reduce(countWords, 0)} words`, |
| 140 | ); |
| 141 | lines.push(``); |
| 142 | lines.push( |
| 143 | `Untranslated: ` + |
| 144 | `${untranslated.length} messages, ` + |
| 145 | `${untranslated.reduce(countWords, 0)} words`, |
| 146 | ); |
| 147 | if (untranslated.length > 0) { |
| 148 | untranslated.sort( |
| 149 | (a, b) => a[1].length - b[1].length || a[1].localeCompare(b[1]), |
| 150 | ); |
| 151 | const head = untranslated.slice(0, 20); |
| 152 | lines.push(``); |
| 153 | for (const [id, message] of head) { |
| 154 | const prefix = message.substring(0, 60); |
| 155 | if (message.length > prefix.length) { |
| 156 | lines.push(`* *${prefix}...*`); |
| 157 | } else { |
| 158 | lines.push(`* *${prefix}*`); |
| 159 | } |
| 160 | } |
| 161 | if (untranslated.length > head.length) { |
| 162 | lines.push(`* ...`); |
| 163 | } |
no test coverage detected