Skip to content

Commit

Permalink
Adds locale to the locale kebab-case warnings (#3951)
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <obulat@gmail.com>
  • Loading branch information
obulat committed Mar 24, 2024
1 parent 1dac25c commit fe8b2bf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
23 changes: 21 additions & 2 deletions frontend/src/locales/scripts/bulk-download.js
Expand Up @@ -31,6 +31,9 @@ const fetchBulkJed1x = async () => {
* Extract all JSON file from the given ZIP file. Their names are sanitised to
* be in the format `<locale_code>.json`.
*
* TODO: Remove deprecated keys handling once all po keys are updated.
* https://github.com/WordPress/openverse/issues/2438
*
* @param zipPath {string} - the path to the ZIP file to extract
* @return {Promise<unknown[]>} - the outcome of writing all ZIP files
*/
Expand All @@ -47,9 +50,25 @@ const extractZip = async (zipPath) => {
.replace(".jed.json", "")
return [localeName, vueI18nObj]
})
return await Promise.all(
localeJsonMap.map((args) => writeLocaleFile(...args))
const deprecatedKeys = { count: 0, keys: {} }

const result = await Promise.all(
localeJsonMap.map((args) => writeLocaleFile(...args, deprecatedKeys))
)
const issue = "https://github.com/WordPress/openverse/issues/2438"
if (deprecatedKeys.count > 0) {
let warning = `${deprecatedKeys.count} deprecated kebab-case keys replaced in locale files. `
warning += `To see the keys, run \`just frontend/run i18n:get-translations --verbose\`. For more details, see ${issue}.`
if (process.argv.includes("--verbose")) {
warning += `\n${JSON.stringify(deprecatedKeys.keys, null, 2)}`
}
console.warn(warning)
} else {
console.log(
`No deprecated kebab-case keys found in locale files. 🎉 Please close issue ${issue}.`
)
}
return result
}

/**
Expand Down
47 changes: 30 additions & 17 deletions frontend/src/locales/scripts/utils.js
Expand Up @@ -40,36 +40,43 @@ exports.setToValue = function setValue(obj, path, value) {
o[a[0]] = value
}

function replacer(_, match) {
// Replace ###<text>### from `po` files with {<text>} in `vue`.
// Additionally, the old kebab-cased keys that can still be in the
// translations are replaced with camelCased keys the app expects.
// TODO: Remove `camel` and warning once all translation strings are updated.
// https://github.com/WordPress/openverse/issues/2438
if (match.includes("-")) {
console.warn("Found kebab-cased key in translation strings:", match)
}
return `{${kebabToCamel(match)}}`
}

/**
* Replace ###<text>### with {<text>}.
*
* @param json {any} - the JSON object to replace placeholders in
* @param {any} json - the JSON object to replace placeholders in
* @param {string} locale - the locale of the JSON object
* @param {object} deprecatedKeys - object to store deprecated kebab-cased keys and number of replacements.
* @return {any} the sanitised JSON object
*/
const replacePlaceholders = (json) => {
const replacePlaceholders = (json, locale, deprecatedKeys) => {
if (json === null) {
return null
}

/**
* Replaces ###<text>### from `po` files with {<text>} in `vue`.
* Additionally, the old kebab-cased keys that can still be in the
* translations are replaced with camelCased keys the app expects.
*/
function replacer(_, match) {
if (match.includes("-")) {
deprecatedKeys.count++
deprecatedKeys.keys[locale] = [
...(deprecatedKeys.keys[locale] ?? []),
match,
]
}
return `{${kebabToCamel(match)}}`
}

if (typeof json === "string") {
return json.replace(/###([a-zA-Z-]*)###/g, replacer)
}
let currentJson = { ...json }

for (const row of Object.entries(currentJson)) {
let [key, value] = row
currentJson[key] = replacePlaceholders(value)
currentJson[key] = replacePlaceholders(value, locale, deprecatedKeys)
}
return currentJson
}
Expand All @@ -80,9 +87,15 @@ exports.replacePlaceholders = replacePlaceholders
* Write translation strings to a file in the locale directory
* @param {string} locale
* @param {any} rawTranslations
* @param {object} deprecatedKeys - object to store deprecated kebab-cased keys and number of replacements.
*/
exports.writeLocaleFile = (locale, rawTranslations) => {
const translations = replacePlaceholders(rawTranslations)
exports.writeLocaleFile = (locale, rawTranslations, deprecatedKeys) => {
const translations = replacePlaceholders(
rawTranslations,
locale,
deprecatedKeys
)

return writeFile(
process.cwd() + `/src/locales/${locale}.json`,
JSON.stringify(translations, null, 2) + os.EOL
Expand Down

0 comments on commit fe8b2bf

Please sign in to comment.