Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Use jed1x json format to correctly handle pluralization #753

Merged
merged 1 commit into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"loading": "Loading...",
"filter-button": {
"simple": "Filters",
"with-count": "{count} Filter | {count} Filters"
"with-count": "{count} Filter|{count} Filters"
}
},
"footer": {
Expand Down Expand Up @@ -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",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's some kind of ESLint rule we could write to emit a warning or something when we find a translation string that should probably have plural variations... like maybe something basic, if the English string ends with s and also contains a variable interpolation? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's an interesting idea, @sarayourfriend !

"load": "Load more results",
"fetching-error": "Error fetching {type}:",
"search-rating": {
Expand Down Expand Up @@ -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}"
Expand Down
36 changes: 20 additions & 16 deletions src/locales/po-files/openverse.pot
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -88,7 +88,7 @@ msgctxt "migration-notice.read"
msgid "Read more"
msgstr ""

#: src/components/DropdownButton.vue:82
#: src/components/DropdownButton.vue:83
msgctxt "dropdown-button.aria.arrow-label"
msgid "Dropdown"
msgstr ""
Expand Down Expand Up @@ -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:570
msgctxt "waveform.current-time"
msgid "###time### seconds"
msgstr ""
msgid "###time### second"
msgid_plural "###time### seconds"
msgstr[0] ""
msgstr[1] ""

msgctxt "browsers.chrome"
msgid "Chrome"
Expand Down Expand Up @@ -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:63
msgctxt "photo-details.creator"
msgid "by ###name###"
msgstr ""

#: src/components/ImageDetails/VPhotoDetails.vue:144
#: src/components/ImageDetails/VPhotoDetails.vue:150
msgctxt "photo-details.weblink"
msgid "Go to image's website"
msgstr ""
Expand All @@ -392,7 +394,7 @@ msgctxt "photo-details.aria.dmca"
msgid "dmca form"
msgstr ""

#: src/components/ImageDetails/VPhotoDetails.vue:52
#: src/components/ImageDetails/VPhotoDetails.vue:58
msgctxt "photo-details.aria.details"
msgid "image details"
msgstr ""
Expand All @@ -414,7 +416,7 @@ msgid "###provider### form"
msgstr ""

#. Do not translate words between ### ###.
#: src/components/ImageDetails/VPhotoDetails.vue:67
#: src/components/ImageDetails/VPhotoDetails.vue:73
msgctxt "photo-details.aria.creator-url"
msgid "author ###creator###"
msgstr ""
Expand All @@ -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:34
msgctxt "photo-details.content-report.title"
msgid "Report this content"
msgstr ""
Expand Down Expand Up @@ -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:106
msgctxt "photo-details.information.title"
msgid "Information"
msgstr ""
Expand Down Expand Up @@ -575,7 +577,7 @@ msgctxt "photo-details.information.pixels"
msgid "pixels"
msgstr ""

#: src/components/ImageDetails/VPhotoDetails.vue:89
#: src/components/ImageDetails/VPhotoDetails.vue:95
msgctxt "photo-details.reuse.title"
msgid "Reuse"
msgstr ""
Expand Down Expand Up @@ -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] ""
Expand Down Expand Up @@ -1984,8 +1986,10 @@ msgstr ""
#. Do not translate words between ### ###.
#: src/components/VHeader/VFilterButton.vue:78
msgctxt "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}"
Expand All @@ -2003,7 +2007,7 @@ msgctxt "header.aria.primary"
msgid "primary"
msgstr ""

#: src/components/VHeader/VPageMenu/VDesktopPageMenu.vue:5
#: src/components/VHeader/VPageMenu/VDesktopPageMenu.vue:2
msgctxt "header.aria.menu"
msgid "menu"
msgstr ""
Expand Down
14 changes: 7 additions & 7 deletions src/locales/scripts/get-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) => {
Expand All @@ -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.`)
})
Expand Down
69 changes: 69 additions & 0 deletions src/locales/scripts/jed1x-json-to-json.js
Original file line number Diff line number Diff line change
@@ -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### результатов",
* }
*
*/
Comment on lines +3 to +28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excellent documentation, great!


// 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)
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved
}
})
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 = jed1xJsonToJson
35 changes: 16 additions & 19 deletions src/locales/scripts/json-pot-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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])
Expand All @@ -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
Expand Down