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

Make pluralized strings compatible with GlotPress #167

Merged
merged 2 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/components/SearchGridManualLoad.vue
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ export default {
const count = this.useInfiniteScroll
? this.$store.state.imagesCount
: this.imagesCount
if (count === 0) {
return this.$t('browse-page.image-no-results')
}
return count >= 10000
? this.$tc('browse-page.image-result-count-more', count, {
localeCount: count.toLocaleString(this.$i18n.locale),
Expand Down
9 changes: 6 additions & 3 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,12 @@
"show": "Show results"
},
"browse-page": {
"all-result-count": "no results|{localeCount} result|{localeCount} results",
"audio-result-count": "no audio results|{localeCount} audio result|{localeCount} audio results",
"image-result-count": "no image results|{localeCount} image result|{localeCount} image results",
"all-no-results": "no results",
"audio-no-results": "no audio results",
"image-no-results": "no image results",
"all-result-count": "{localeCount} result|{localeCount} results",
"audio-result-count": "{localeCount} audio result|{localeCount} audio results",
"image-result-count": "{localeCount} image result|{localeCount} image results",
"all-result-count-more": "Over {localeCount} results",
"audio-result-count-more": "Over {localeCount} audio results",
"image-result-count-more": "Over {localeCount} image results",
Expand Down
30 changes: 26 additions & 4 deletions src/locales/scripts/json-to-pot.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const getParsedVueFiles = require('./parse-vue-files.js')
const json = require('../en.json')
const fs = require('fs')

const curlyRegex = new RegExp('{[a-z]*}')
const curlyRegex = new RegExp('{[a-zA-Z-]*}')
const containsCurlyWord = (string) => curlyRegex.test(string)
const checkStringForVars = (string) =>
containsCurlyWord(string) ? '(Do not translate words between ###)' : ''
Expand Down Expand Up @@ -81,7 +81,7 @@ const findPath = (ob, key) => {
return path.join('.')
}

const PARSED_VUE_FILES = getParsedVueFiles('src/**/*.?(js|vue)')
const PARSED_VUE_FILES = getParsedVueFiles('**/*.?(js|vue)')

/**
* Returns the comment with a reference github link to the line where the
Expand All @@ -96,7 +96,14 @@ 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',
]
// POT Syntax

// msgctxt context
Expand All @@ -109,12 +116,27 @@ function potTime(json, parent = json) {
let [key, value] = row
if (typeof value === 'string') {
const keyPath = `${findPath(parent, key)}.${key}`
potFile = `${potFile}
if (pluralizedKeys.includes(key)) {
const pluralizedValues = value.split('|')
if (pluralizedValues.length === 1) {
pluralizedValues.push(pluralizedValues[0])
}
potFile = `${potFile}

# ${keyPath} ${checkStringForVars(value)}${getRefComment(keyPath)}
msgctxt "${keyPath}"
msgid "${processValue(pluralizedValues[0])}"
msgid_plural "${processValue(pluralizedValues[1])}"
msgstr[0] ""
msgstr[1] ""`
} else {
potFile = `${potFile}

# ${keyPath} ${checkStringForVars(value)}${getRefComment(keyPath)}
msgctxt "${keyPath}"
msgid "${processValue(value)}"
msgstr ""`
}
}
if (typeof value === 'object') {
potFile = `${potFile}${potTime(value, parent)}`
Expand Down
16 changes: 14 additions & 2 deletions src/locales/scripts/parse-vue-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const fs = require('fs')
const path = require('path')
const glob = require('glob')

const BASE_PATH = path.dirname(path.dirname(path.dirname(process.cwd())))
const BASE_PATH = path.dirname(path.dirname(__dirname))

function readVueFiles(src) {
const targetFiles = glob.sync(src)
Expand Down Expand Up @@ -101,7 +101,19 @@ function parseVueFiles(vueFilesPath) {
return extractI18nItemsFromVueFiles(filesList)
}

const getParsedVueFiles = (vueFiles = './**/*.?(js|vue)') => {
/**
* Parses all vue files found in the glob paths, and returns an
* array of objects with i18n path, line number, and vue file path.
* {
path: 'browse-page.aria.close',
line: 13,
file: '/components/AppModal.vue'
},
* @param {string} vueFiles - glob pattern to find all the vue files,
* from the BASE_PATH (`openverse-frontend/src`)
* @return {Array<Object>}
*/
const getParsedVueFiles = (vueFiles) => {
const resolvedVueFiles = path.resolve(BASE_PATH, vueFiles)
return parseVueFiles(resolvedVueFiles)
}
Expand Down