Skip to content

Commit

Permalink
Require English descriptions in locale files (#10260)
Browse files Browse the repository at this point in the history
The `verify-locale-strings.js` script now validates that the
descriptions from the `en` locale are also present in all other
locales.

These descriptions are intended to help with translation, and are not
meant to be translated. This check will ensure that translators don't
accidentally translate these. It also ensures they're present alongside
each translated message, which might be helpful for understanding
context.
  • Loading branch information
Gudahtt authored Feb 3, 2021
1 parent b98cef1 commit 181bd7b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 10 additions & 0 deletions development/lib/locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,17 @@ function compareLocalesForMissingItems({ base, subject }) {
return Object.keys(base).filter((key) => !subject[key])
}

function compareLocalesForMissingDescriptions({ englishLocale, targetLocale }) {
return Object.keys(englishLocale).filter(
(key) =>
targetLocale[key] !== undefined &&
englishLocale[key].description !== undefined &&
englishLocale[key].description !== targetLocale[key].description,
)
}

module.exports = {
compareLocalesForMissingDescriptions,
compareLocalesForMissingItems,
getLocale,
getLocalePath,
Expand Down
26 changes: 24 additions & 2 deletions development/verify-locale-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
//
// This script will validate that locales have no unused messages. It will check
// the English locale against string literals found under `ui/`, and it will check
// other locales by comparing them to the English locale.
// other locales by comparing them to the English locale. It will also validate
// that non-English locales have all descriptions present in the English locale.
//
// A report will be printed to the console detailing any unused messages.
//
Expand All @@ -27,6 +28,7 @@ const log = require('loglevel')
const matchAll = require('string.prototype.matchall').getPolyfill()
const localeIndex = require('../app/_locales/index.json')
const {
compareLocalesForMissingDescriptions,
compareLocalesForMissingItems,
getLocale,
getLocalePath,
Expand Down Expand Up @@ -129,12 +131,32 @@ async function verifyLocale(code) {
})
}

if (extraItems.length > 0) {
const missingDescriptions = compareLocalesForMissingDescriptions({
englishLocale,
targetLocale,
})

if (missingDescriptions.length) {
console.log(
`**${code}**: ${missingDescriptions.length} missing descriptions`,
)
log.info('Messages with missing descriptions:')
missingDescriptions.forEach(function (key) {
log.info(` - [ ] ${key}`)
})
}

if (extraItems.length > 0 || missingDescriptions.length > 0) {
if (fix) {
const newLocale = { ...targetLocale }
for (const item of extraItems) {
delete newLocale[item]
}
for (const message of Object.keys(englishLocale)) {
if (englishLocale[message].description && targetLocale[message]) {
targetLocale[message].description = englishLocale[message].description
}
}
await writeLocale(code, newLocale)
}
return true
Expand Down

0 comments on commit 181bd7b

Please sign in to comment.