Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Use jed1x json format to correctly handle pluralization (#753)
Olga Bulat53ca0f8dd41a
diff --git a/src/locales/en.json b/src/locales/en.jsonindex 8f1184a30..f2ba8c0f5 100644--- a/src/locales/en.json+++ b/src/locales/en.json@@ -61,7 +61,7 @@"loading": "Loading...","filter-button": {"simple": "Filters",- "with-count": "{count} Filter | {count} Filters"+ "with-count": "{count} Filter|{count} Filters"}},"footer": {@@ -405,7 +405,7 @@"browse-page": {"all-no-results": "No results","all-result-count": "{localeCount} result|{localeCount} results",- "all-result-count-more": "Over {localeCount} results",+ "all-result-count-more": "Over {localeCount} result|Over {localeCount} results","load": "Load more results","fetching-error": "Error fetching {type}:","search-rating": {@@ -595,7 +595,7 @@},"waveform": {"label": "Audio seek bar",- "current-time": "{time} seconds"+ "current-time": "{time} second|{time} seconds"},"audio-thumbnail": {"alt": "Cover art for \"{title}\" by {creator}"diff --git a/src/locales/po-files/openverse.pot b/src/locales/po-files/openverse.potindex ea4009255..be4660474 100644--- a/src/locales/po-files/openverse.pot+++ b/src/locales/po-files/openverse.pot@@ -4,7 +4,7 @@ msgid ""msgstr """Project-Id-Version: Openverse \n""Report-Msgid-Bugs-To: https://github.com/wordpress/openverse/issues \n"-"POT-Creation-Date: 2022-01-26T10:18:21+00:00\n"+"POT-Creation-Date: 2022-01-28T12:35:42+00:00\n""MIME-Version: 1.0\n""Content-Type: text/plain; charset=UTF-8\n""Content-Transfer-Encoding: 8bit\n"@@ -88,7 +88,7 @@ msgctxt "migration-notice.read"msgid "Read more"msgstr ""-#: src/components/DropdownButton.vue:82+#: src/components/DropdownButton.vue:83msgctxt "dropdown-button.aria.arrow-label"msgid "Dropdown"msgstr ""@@ -242,10 +242,12 @@ msgid "Audio seek bar"msgstr ""#. Do not translate words between ### ###.-#: src/components/VAudioTrack/VWaveform.vue:554+#: src/components/VAudioTrack/VWaveform.vue:570msgctxt "waveform.current-time"-msgid "###time### seconds"-msgstr ""+msgid "###time### second"+msgid_plural "###time### seconds"+msgstr[0] ""+msgstr[1] ""msgctxt "browsers.chrome"msgid "Chrome"@@ -362,12 +364,12 @@ msgid "Back to search results"msgstr ""#. Do not translate words between ### ###.-#: src/components/ImageDetails/VPhotoDetails.vue:57+#: src/components/ImageDetails/VPhotoDetails.vue:63msgctxt "photo-details.creator"msgid "by ###name###"msgstr ""-#: src/components/ImageDetails/VPhotoDetails.vue:144+#: src/components/ImageDetails/VPhotoDetails.vue:150msgctxt "photo-details.weblink"msgid "Go to image's website"msgstr ""@@ -392,7 +394,7 @@ msgctxt "photo-details.aria.dmca"msgid "dmca form"msgstr ""-#: src/components/ImageDetails/VPhotoDetails.vue:52+#: src/components/ImageDetails/VPhotoDetails.vue:58msgctxt "photo-details.aria.details"msgid "image details"msgstr ""@@ -414,7 +416,7 @@ msgid "###provider### form"msgstr ""#. Do not translate words between ### ###.-#: src/components/ImageDetails/VPhotoDetails.vue:67+#: src/components/ImageDetails/VPhotoDetails.vue:73msgctxt "photo-details.aria.creator-url"msgid "author ###creator###"msgstr ""@@ -427,7 +429,7 @@ msgctxt "photo-details.aria.attribution.tool"msgid "read more about the tool"msgstr ""-#: src/components/ImageDetails/VPhotoDetails.vue:38+#: src/components/ImageDetails/VPhotoDetails.vue:34msgctxt "photo-details.content-report.title"msgid "Report this content"msgstr ""@@ -536,7 +538,7 @@ msgctxt "photo-details.survey.answer"msgid "by answering a few questions."msgstr ""-#: src/components/ImageDetails/VPhotoDetails.vue:100+#: src/components/ImageDetails/VPhotoDetails.vue:106msgctxt "photo-details.information.title"msgid "Information"msgstr ""@@ -575,7 +577,7 @@ msgctxt "photo-details.information.pixels"msgid "pixels"msgstr ""-#: src/components/ImageDetails/VPhotoDetails.vue:89+#: src/components/ImageDetails/VPhotoDetails.vue:95msgctxt "photo-details.reuse.title"msgid "Reuse"msgstr ""@@ -729,7 +731,7 @@ msgstr[1] ""#. Do not translate words between ### ###.msgctxt "browse-page.all-result-count-more"-msgid "Over ###localeCount### results"+msgid "Over###localeCount### result"msgid_plural "Over ###localeCount### results"msgstr[0] ""msgstr[1] ""@@ -1984,8 +1986,10 @@ msgstr ""#. Do not translate words between ### ###.#: src/components/VHeader/VFilterButton.vue:78msgctxt "header.filter-button.with-count"-msgid "###count### Filter | ###count### Filters"-msgstr ""+msgid "###count### Filter"+msgid_plural "###count### Filters"+msgstr[0] ""+msgstr[1] ""msgctxt "header.notification.text"msgid "{example_notification_text}"@@ -2003,7 +2007,7 @@ msgctxt "header.aria.primary"msgid "primary"msgstr ""-#: src/components/VHeader/VPageMenu/VDesktopPageMenu.vue:5+#: src/components/VHeader/VPageMenu/VDesktopPageMenu.vue:2msgctxt "header.aria.menu"msgid "menu"msgstr ""diff --git a/src/locales/scripts/get-translations.js b/src/locales/scripts/get-translations.jsindex 17b516cff..2325ff74d 100644--- a/src/locales/scripts/get-translations.js+++ b/src/locales/scripts/get-translations.js@@ -5,7 +5,7 @@const { writeFile } = require('fs/promises')const os = require('os')const axios = require('axios')-const ngxJsonToJson = require('./ngx-json-to-json')+const jed1xJsonToJson = require('./jed1x-json-to-json')const localeJSON = require('./wp-locales.json')/**@@ -35,8 +35,8 @@ const makeTranslationUrl =* fetch a json translation from GlotPress* @param {string} locale*/-const fetchNgxTranslation = (locale) =>- axios.get(makeTranslationUrl('ngx')(locale)).then((res) => res.data)+const fetchJed1xTranslation = (locale) =>+ axios.get(makeTranslationUrl('jed1x')(locale)).then((res) => res.data)const replacePlaceholders = (json) => {if (json === null) {@@ -86,8 +86,8 @@ const isEmpty = (obj) => Object.values(obj).every((x) => x === null)** @param {string[]} locales*/-const fetchAndConvertNGXTranslations = (locales) => {- return Promise.allSettled(locales.map(fetchNgxTranslation))+const fetchAndConvertJed1xTranslations = (locales) => {+ return Promise.allSettled(locales.map(fetchJed1xTranslation)).then((res) => {let successfulTranslations = []res.forEach(({ status, value }, index) => {@@ -99,14 +99,14 @@ const fetchAndConvertNGXTranslations = (locales) => {}).then((res) => {Object.keys(res).forEach((key) => {- res[key] = ngxJsonToJson(res[key])+ res[key] = jed1xJsonToJson(res[key])})return res}).then(writeLocaleFiles)}-fetchAndConvertNGXTranslations(Object.values(localeJSON).map((i) => i.slug))+fetchAndConvertJed1xTranslations(Object.values(localeJSON).map((i) => i.slug)).then((res) => {console.log(`Successfully saved ${res.length} translations.`)})diff --git a/src/locales/scripts/jed1x-json-to-json.js b/src/locales/scripts/jed1x-json-to-json.jsnew file mode 100644index 000000000..fceb24491--- /dev/null+++ b/src/locales/scripts/jed1x-json-to-json.js@@ -0,0 +1,69 @@+const set = require('lodash.set')++/**+ * Convert a Jed1x-Translate object to a nested JSON object.+ *+ * Create a nested JSON object, clean up keys from jed1x context with special+ * character, remove keys without values, convert values from array to string,+ * if strings are for plural forms, join them with the pipe character.+ * Go from this:+ * {+ * "browse-page.load\u0004Load more results": [+ * "Загрузить ещё результаты"+ * ],+ * "browse-page.all-result-count-more\u0004Over ###localeCount### results": [+ * "Более ###localeCount### результата",+ * "Более ###localeCount### результатов",+ * "Более ###localeCount### результатов"+ * ]+ * "browse-page.search-form.button\u0004Search": [],+ * }+ * To:+ * {+ * "browse-page: {+ * "load": "Загрузить ещё результаты"+ * "all-result-count-more": "Более ###localeCount### результата|Более ###localeCount### результатов|Более ###localeCount### результатов",+ * }+ *+ */++// special character, context delimiter in jed format:+// https://github.com/messageformat/Jed/blob/351c47d5c57c5c81e418414c53ca84075c518edb/jed.js#L117+const SPLIT_CHAR = String.fromCharCode(4)++function jed1xJsonToJson(jed1xObject) {+ const result = {}+ Object.entries(jed1xObject?.locale_data?.messages).forEach(([key, value]) => {+ const cleanedKey = key.slice(0, key.indexOf(SPLIT_CHAR))+ if (value.length > 0) {+ const cleanedValue = value.length === 1 ? value[0] : value.join('|')+ return set(result, cleanedKey, cleanedValue)+ }+ })+ return result+}++// test+// node jed1x-json-to-json.js+// console.log(+// JSON.stringify(+// jed1xJsonToJson({+// locale_data: {+// messages: {+// 'browse-page.load\u0004Load more results': [+// 'Загрузить ещё результаты',+// ],+// 'browse-page.all-result-count-more\u0004Over ###localeCount### results':+// [+// 'Более ###localeCount### результата',+// 'Более ###localeCount### результатов',+// 'Более ###localeCount### результатов',+// ],+// 'browse-page.search-form.button\u0004Search': [],+// },+// },+// })+// )+// )++module.exports = jed1xJsonToJsondiff --git a/src/locales/scripts/json-pot-helpers.js b/src/locales/scripts/json-pot-helpers.jsindex 00d16d75f..5df1196c6 100644--- a/src/locales/scripts/json-pot-helpers.js+++ b/src/locales/scripts/json-pot-helpers.js@@ -59,14 +59,13 @@ const getRefComment = (keyPath) => {}const escapeQuotes = (str) => str.replace(/"/g, '\\"')-const pluralizedKeys = [- 'all-result-count',- 'audio-result-count',- 'image-result-count',- 'all-result-count-more',- 'audio-result-count-more',- 'image-result-count-more',-]++/**+ * String values that need to be pluralized contain {count},+ * {time} or {localeCount},+ * and a pipe symbol.+ */+const pluralizedRegex = new RegExp(/(count|time)/i)const pot_creation_date = () => {const today = new Date()@@ -98,18 +97,8 @@ function potTime(json) {let potFileString = ''const jsonKeys = getAllPaths(json)jsonKeys.forEach((key) => {- const parts = key.split('.')- const keyName = parts[parts.length - 1]const value = getKeyValue(key, json)- if (!pluralizedKeys.includes(keyName)) {- potFileString = `${potFileString}-${- checkStringForVars(value) ? `\n#. ${checkStringForVars(value)}` : ''-}${getRefComment(key)}-msgctxt "${key}"-msgid "${processValue(value)}"-msgstr ""`- } else {+ if (value.indexOf('|') > -1 && pluralizedRegex.test(value)) {const pluralizedValues = value.split('|')if (pluralizedValues.length === 1) {pluralizedValues.push(pluralizedValues[0])@@ -123,6 +112,14 @@ msgid "${processValue(pluralizedValues[0])}"msgid_plural "${processValue(pluralizedValues[1])}"msgstr[0] ""msgstr[1] ""`+ } else {+ potFileString = `${potFileString}+${+ checkStringForVars(value) ? `\n#. ${checkStringForVars(value)}` : ''+}${getRefComment(key)}+msgctxt "${key}"+msgid "${processValue(value)}"+msgstr ""`}})return potFileString