Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Cache the GlotPress translations download in CI (#5719)
Olga Bulat9497d16c266a
diff --git a/.github/actions/setup-env/action.yml b/.github/actions/setup-env/action.ymlindex 449b1dfa3..94db8c1b1 100644--- a/.github/actions/setup-env/action.yml+++ b/.github/actions/setup-env/action.yml@@ -81,8 +81,44 @@ runs:just ${{ inputs.install_recipe }}# Set up locales+ # GlotPress rate-limits the bulk translations download, so the downloaded+ # archive is cached and reused by every run within the same 2-hour window.+ # Older archives are restored as a fallback for when the download fails.+ - name: Get translations cache key+ id: i18n-cache-key+ if: inputs.locales == 'production'+ shell: bash+ run: |+ echo "window=$(( $(date -u +%s) / 7200 ))" >> "$GITHUB_OUTPUT"+ echo "path=${{ runner.temp }}/glotpress/openverse.zip" >> "$GITHUB_OUTPUT"++ - name: Restore cached translations+ id: i18n-cache+ if: inputs.locales == 'production'+ uses: actions/cache/restore@v4+ with:+ path: ${{ steps.i18n-cache-key.outputs.path }}+ key: glotpress-ngx-${{ steps.i18n-cache-key.outputs.window }}+ restore-keys: glotpress-ngx-+- name: Setup locales+ id: localesif: inputs.locales != ''shell: bash+ env:+ I18N_NGX_CACHE: ${{ steps.i18n-cache-key.outputs.path }}+ I18N_NGX_CACHE_FRESH: ${{ steps.i18n-cache.outputs.cache-hit }}run: |+ marker="${{ runner.temp }}/i18n-start"+ touch "$marker"just locales ${{ inputs.locales }}+ if [ -n "$I18N_NGX_CACHE" ] && [ "$I18N_NGX_CACHE" -nt "$marker" ]; then+ echo "downloaded=true" >> "$GITHUB_OUTPUT"+ fi++ - name: Cache downloaded translations+ if: steps.locales.outputs.downloaded == 'true'+ uses: actions/cache/save@v4+ with:+ path: ${{ steps.i18n-cache-key.outputs.path }}+ key: glotpress-ngx-${{ steps.i18n-cache-key.outputs.window }}diff --git a/frontend/i18n/scripts/translations.mjs b/frontend/i18n/scripts/translations.mjsindex edc1859b1..550cede5b 100644--- a/frontend/i18n/scripts/translations.mjs+++ b/frontend/i18n/scripts/translations.mjs@@ -1,5 +1,12 @@-import { createWriteStream, readFileSync, unlinkSync } from "fs"-import { basename, join } from "path"+import {+ copyFileSync,+ createWriteStream,+ existsSync,+ mkdirSync,+ readFileSync,+ unlinkSync,+} from "fs"+import { basename, dirname, join } from "path"import { pipeline } from "stream/promises"import AdmZip from "adm-zip"@@ -81,7 +88,7 @@ const logWarnings = (deprecatedKeys, invalidKeys, debug) => {console.log(prettify(keysObject.keys))}} else {- console.log(keysObject[keysKind].success)+ console.log(warnings[keysKind].success)}}}@@ -219,6 +226,44 @@ const replacePlaceholders = (json, locale, deprecatedKeys, invalidKeys) => {return json}+/**+ * Get the path to the translations ZIP archive, downloading it from GlotPress+ * unless a fresh cached copy can be used.+ *+ * `I18N_NGX_CACHE` is the path where the downloaded archive is kept between+ * runs (CI restores it with `actions/cache`). If `I18N_NGX_CACHE_FRESH` is+ * "true", the cached archive is used without downloading. If the download+ * fails, a cached archive of any age is used instead.+ *+ * @return {Promise<{ zipPath: string, isTemporary: boolean }>}+ */+const getNgxZip = async () => {+ const cachePath = process.env.I18N_NGX_CACHE+ const hasCache = Boolean(cachePath) && existsSync(cachePath)++ if (hasCache && process.env.I18N_NGX_CACHE_FRESH === "true") {+ console.log(`Using cached translations from ${cachePath}.`)+ return { zipPath: cachePath, isTemporary: false }+ }++ try {+ const zipPath = await fetchBulkNgx()+ if (cachePath) {+ mkdirSync(dirname(cachePath), { recursive: true })+ copyFileSync(zipPath, cachePath)+ }+ return { zipPath, isTemporary: true }+ } catch (error) {+ if (!hasCache) {+ throw error+ }+ console.warn(+ `${error.message}. Falling back to the cached translations in ${cachePath}.`+ )+ return { zipPath: cachePath, isTemporary: false }+ }+}+/*** Perform a bulk download of translation strings from GlotPress and extract the* JSON files from the ZIP archive.@@ -228,9 +273,11 @@ const replacePlaceholders = (json, locale, deprecatedKeys, invalidKeys) => {export const downloadTranslations = async (debug) => {console.log("Performing bulk translations download.")try {- const zipPath = await fetchBulkNgx()+ const { zipPath, isTemporary } = await getNgxZip()const translationsCount = await extractZip(zipPath, debug)- unlinkSync(zipPath)+ if (isTemporary) {+ unlinkSync(zipPath)+ }console.log(`Successfully saved ${translationsCount} translations.`)return true} catch (error) {@@ -238,6 +285,7 @@ export const downloadTranslations = async (debug) => {return false}}+/*** Convert en.json5 to en.json, and return the number of keys in the file,* if `countKeys` is true.