Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

fix: resolve i18n template placeholders in settings search results (#6814)

Matt Van Horn55ff1f442f84
diff --git a/frontend/editor/src/core/data/settingsContentSearch.test.ts b/frontend/editor/src/core/data/settingsContentSearch.test.ts
new file mode 100644
index 0000000000..3a46d129d7
--- /dev/null
+++ b/frontend/editor/src/core/data/settingsContentSearch.test.ts
@@ -0,0 +1,46 @@
+import { describe, expect, test } from "vitest";
+import { flattenTranslationStrings } from "@app/data/settingsContentSearch";
+
+describe("flattenTranslationStrings", () => {
+ test("keeps plain strings and walks nested subtrees", () => {
+ expect(
+ flattenTranslationStrings({
+ description: " Static general description: ",
+ nested: { label: "Inner label" },
+ list: ["First", "Second"],
+ }),
+ ).toEqual([
+ "Static general description:",
+ "Inner label",
+ "First",
+ "Second",
+ ]);
+ });
+
+ test("excludes strings that contain unresolved interpolation placeholders", () => {
+ expect(
+ flattenTranslationStrings({
+ defaultLabel: "Default: {{shortcut}}",
+ notificationLabel: "Send {{email}} about {{message}}",
+ leadingPlaceholder: "{{shortcut}}: Open settings",
+ plain: "Keyboard shortcuts",
+ }),
+ ).toEqual(["Keyboard shortcuts"]);
+ });
+
+ test("removes indexed Trans markup", () => {
+ expect(flattenTranslationStrings("<0>test</0> setting")).toEqual([
+ "test setting",
+ ]);
+ });
+
+ test("drops placeholder-only content entirely", () => {
+ expect(
+ flattenTranslationStrings({
+ emailOnly: "{{email}}",
+ messageOnly: "<0>{{message}}</0>",
+ blank: " ",
+ }),
+ ).toEqual([]);
+ });
+});
diff --git a/frontend/editor/src/core/data/settingsContentSearch.ts b/frontend/editor/src/core/data/settingsContentSearch.ts
index ab5028564c..16d9e7aeb4 100644
--- a/frontend/editor/src/core/data/settingsContentSearch.ts
+++ b/frontend/editor/src/core/data/settingsContentSearch.ts
@@ -70,10 +70,48 @@ export const getTranslationPrefixesForNavKey = (key: string): string[] => {
return Array.from(new Set([...explicitPrefixes, ...inferredPrefixes]));
};
+const INTERPOLATION_PLACEHOLDER_PATTERN = /\{\{\s*[^}]+?\s*\}\}/;
+const INDEXED_TRANS_TAG_PATTERN = /<\/?\d+>/g;
+const SEARCHABLE_TEXT_PATTERN = /[\p{L}\p{N}]/u;
+
+/**
+ * Searchable text from one translation leaf.
+ *
+ * An unresolved `{{interpolation}}` placeholder stands in for a runtime value
+ * this index cannot supply. The leftover label ("Default:") is not a useful
+ * hit, so the whole string is omitted. Indexed `<0>` Trans tags are markup
+ * around words the user sees, so only the tags are removed. Null when nothing
+ * searchable remains.
+ */
+export const sanitizeSearchableTranslationString = (
+ value: string,
+): string | null => {
+ const trimmed = value.trim();
+
+ if (!trimmed) {
+ return null;
+ }
+
+ if (INTERPOLATION_PLACEHOLDER_PATTERN.test(trimmed)) {
+ return null;
+ }
+
+ if (!trimmed.match(INDEXED_TRANS_TAG_PATTERN)) {
+ return trimmed;
+ }
+
+ const sanitized = trimmed
+ .replace(INDEXED_TRANS_TAG_PATTERN, "")
+ .replace(/\s+/g, " ")
+ .trim();
+
+ return SEARCHABLE_TEXT_PATTERN.test(sanitized) ? sanitized : null;
+};
+
export const flattenTranslationStrings = (value: unknown): string[] => {
if (typeof value === "string") {
- const trimmed = value.trim();
- return trimmed ? [trimmed] : [];
+ const sanitized = sanitizeSearchableTranslationString(value);
+ return sanitized ? [sanitized] : [];
}
if (Array.isArray(value)) {