Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Keep translations that contain the quoted Openverse name (#5863)

Mayank Majejie18d2aac59dd
diff --git a/frontend/i18n/scripts/translations.mjs b/frontend/i18n/scripts/translations.mjs
index 2d786550d..7b1d8d405 100644
--- a/frontend/i18n/scripts/translations.mjs
+++ b/frontend/i18n/scripts/translations.mjs
@@ -166,7 +166,12 @@ const extractZip = async (zipPath, debug) => {
* @param {object} invalidKeys - object to store invalid values that contain extra `#` or `{}`, and number of replacements.
* @return {any} the sanitised JSON object
*/
-const replacePlaceholders = (json, locale, deprecatedKeys, invalidKeys) => {
+export const replacePlaceholders = (
+ json,
+ locale,
+ deprecatedKeys,
+ invalidKeys
+) => {
const recordProblems = (key, problems) => {
problems.count++
problems.keys[locale] = [...(problems.keys[locale] ?? []), key]
@@ -203,7 +208,7 @@ const replacePlaceholders = (json, locale, deprecatedKeys, invalidKeys) => {
// Not all Openverse instances were replaced with 'Openverse'
// Pot file generation wasn't updated, and {'Openverse'} wasn't replaced with
// ###'Openverse'### before being sent to GlotPress
- let replaced = cleaned.replace(/{'Openverse'}/g, "Openverse")
+ let replaced = cleaned.replace(/###'Openverse'###/g, "Openverse")
replaced = replaced.replace(/###([a-zA-Z-]+?)###/g, replacer)
// Irregular placeholders with more or fewer than 3 #s
replaced = replaced.replace(/#{1,4}([a-zA-Z-]+?)#{1,4}/g, "{$1}")
diff --git a/frontend/test/unit/specs/i18n/translations.spec.js b/frontend/test/unit/specs/i18n/translations.spec.js
new file mode 100644
index 000000000..02b7a8f46
--- /dev/null
+++ b/frontend/test/unit/specs/i18n/translations.spec.js
@@ -0,0 +1,34 @@
+import { describe, expect, it } from "vitest"
+import { replacePlaceholders } from "~~/i18n/scripts/translations.mjs"
+
+const replace = (json) =>
+ replacePlaceholders(
+ json,
+ "ko.json",
+ { count: 0, keys: {} },
+ { count: 0, keys: {} }
+ )
+
+describe("replacePlaceholders", () => {
+ it("replaces placeholders with vue-i18n interpolations", () => {
+ expect(
+ replace({ a: "###openverse### and ###link###", b: "###sources-link###" })
+ ).toEqual({ a: "{'Openverse'} and {link}", b: "{sourcesLink}" })
+ })
+
+ it.each([
+ ["###'Openverse'### 프로젝트는 ###link###를 준수합니다."],
+ ["{'Openverse'} 프로젝트는 {link}를 준수합니다."],
+ ])(
+ "keeps translations that contain the quoted Openverse name: %s",
+ (value) => {
+ expect(replace({ key: value })).toEqual({
+ key: "Openverse 프로젝트는 {link}를 준수합니다.",
+ })
+ }
+ )
+
+ it("empties translations with broken placeholders", () => {
+ expect(replace({ key: "#link and #### text" })).toEqual({ key: "" })
+ })
+})