Skip to content

A platform for community discussion. Free, open, simple.

UX: Display interpolation keys as interactive pills in admin editors (#37254)

Régis Hanol5f6e69f64f3e
diff --git a/app/assets/stylesheets/admin/admin_base.scss b/app/assets/stylesheets/admin/admin_base.scss
index 5c6b5521873..46baefa8724 100644
--- a/app/assets/stylesheets/admin/admin_base.scss
+++ b/app/assets/stylesheets/admin/admin_base.scss
@@ -352,10 +352,6 @@ $mobile-breakpoint: 700px;
}
}
- .go-back {
- margin-top: 1em;
- }
-
.desc {
padding-top: 3px;
font-size: var(--font-down-1);
@@ -363,6 +359,10 @@ $mobile-breakpoint: 700px;
color: var(--primary-medium);
}
+ .go-back {
+ margin-top: 1em;
+ }
+
.outdated {
border: 1px solid var(--content-border-color);
box-sizing: border-box;
@@ -382,6 +382,48 @@ $mobile-breakpoint: 700px;
}
}
+.interpolation-keys {
+ display: flex;
+ flex-wrap: wrap;
+ gap: 0.35em;
+ padding-top: 0.5em;
+
+ &__pill {
+ display: inline-flex;
+ align-items: center;
+ gap: 0.25em;
+ padding: 0.15em 0.5em;
+ border: 1px solid var(--primary-low-mid);
+ border-radius: var(--d-border-radius);
+ background: transparent;
+ color: var(--primary-medium);
+ font-size: var(--font-down-1);
+ font-family: var(--d-font-family--monospace);
+ cursor: pointer;
+ transition:
+ background 0.15s,
+ border-color 0.15s;
+
+ &:hover {
+ background: var(--primary-very-low);
+ border-color: var(--primary-low);
+ }
+
+ &.is-used {
+ color: var(--success);
+ border-color: var(--success-low);
+ background: var(--success-low);
+ }
+
+ &.is-invalid {
+ color: var(--danger);
+ border-color: var(--danger-low);
+ background: var(--danger-low);
+ cursor: default;
+ }
+ }
+}
+
.content-list {
width: 27%;
float: left;
diff --git a/app/controllers/admin/site_texts_controller.rb b/app/controllers/admin/site_texts_controller.rb
index ca78ff89fc5..1f25f256e2f 100644
--- a/app/controllers/admin/site_texts_controller.rb
+++ b/app/controllers/admin/site_texts_controller.rb
@@ -186,7 +186,7 @@ class Admin::SiteTextsController < Admin::AdminController
interpolation_keys =
I18nInterpolationKeysFinder.find(I18n.overrides_disabled { I18n.t(en_key, locale: :en) })
custom_keys = TranslationOverride.custom_interpolation_keys(en_key)
- { id: key, value: value, locale: locale, interpolation_keys: interpolation_keys + custom_keys }
+ { id: key, value:, locale:, interpolation_keys: (interpolation_keys | custom_keys).sort }
end
PLURALIZED_REGEX = /(.*)\.(zero|one|two|few|many|other)\z/
diff --git a/app/models/translation_override.rb b/app/models/translation_override.rb
index 029ff6383c5..7f5e50e4590 100644
--- a/app/models/translation_override.rb
+++ b/app/models/translation_override.rb
@@ -152,18 +152,17 @@ class TranslationOverride < ActiveRecord::Base
end
def invalid_interpolation_keys
- return [] if current_default.blank?
+ return [] if current_default.blank? || value.blank?
original_interpolation_keys = I18nInterpolationKeysFinder.find(current_default)
- new_interpolation_keys = I18nInterpolationKeysFinder.find(value)
- custom_interpolation_keys = []
+ custom_keys = self.class.custom_interpolation_keys(transformed_key)
+ allowed_keys = original_interpolation_keys + custom_keys
- ALLOWED_CUSTOM_INTERPOLATION_KEYS.select do |keys, value|
- custom_interpolation_keys = value if keys.any? { |key| transformed_key.start_with?(key) }
- end
+ # Find all patterns that look like interpolation attempts: %{...}
+ attempted_keys = value.scan(/%\{([^{}]+?)\}/).flatten.uniq
- (original_interpolation_keys | new_interpolation_keys) - original_interpolation_keys -
- custom_interpolation_keys
+ # Return keys that aren't in the allowed list
+ attempted_keys - allowed_keys
end
def current_default
diff --git a/app/serializers/admin_email_template_serializer.rb b/app/serializers/admin_email_template_serializer.rb
index d48256dcaba..64b948ebdfc 100644
--- a/app/serializers/admin_email_template_serializer.rb
+++ b/app/serializers/admin_email_template_serializer.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
class AdminEmailTemplateSerializer < ApplicationSerializer
- attributes :id, :title, :subject, :body, :can_revert?
+ attributes :id, :title, :subject, :body, :can_revert?, :interpolation_keys
def id
object
@@ -37,4 +37,23 @@ class AdminEmailTemplateSerializer < ApplicationSerializer
TranslationOverride.exists?(locale: I18n.locale, translation_key: keys)
end
end
+
+ def interpolation_keys
+ @interpolation_keys ||=
+ begin
+ keys = []
+
+ subject_key = "#{object}.subject_template"
+ subject_text = I18n.overrides_disabled { I18n.t(subject_key, locale: :en, default: "") }
+ keys |= I18nInterpolationKeysFinder.find(subject_text) if subject_text.is_a?(String)
+
+ body_key = "#{object}.text_body_template"
+ body_text = I18n.overrides_disabled { I18n.t(body_key, locale: :en, default: "") }
+ keys |= I18nInterpolationKeysFinder.find(body_text) if body_text.is_a?(String)
+
+ keys |= TranslationOverride.custom_interpolation_keys(object)
+
+ keys.sort
+ end
+ end
end
diff --git a/app/serializers/site_text_serializer.rb b/app/serializers/site_text_serializer.rb
index aeea09cb574..e02f1f1f166 100644
--- a/app/serializers/site_text_serializer.rb
+++ b/app/serializers/site_text_serializer.rb
@@ -7,7 +7,6 @@ class SiteTextSerializer < ApplicationSerializer
:old_default,
:new_default,
:interpolation_keys,
- :has_interpolation_keys?,
:overridden?,
:can_revert?
@@ -39,10 +38,6 @@ class SiteTextSerializer < ApplicationSerializer
object[:interpolation_keys]
end
- def has_interpolation_keys?
- object[:interpolation_keys].present?
- end
-
def overridden?
if options[:overridden_keys]
options[:overridden_keys].include?(object[:id])
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index b5a768276ac..c6a584f69a2 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -8623,6 +8623,9 @@ en:
more_than_50_results: "There are more than 50 results. Please refine your search."
no_results: "No matching site texts found"
interpolation_keys: "Available interpolation keys:"
+ interpolation_key_insert: "Click to insert"
+ interpolation_key_used: "Already used"
+ interpolation_key_invalid: "Unknown key — will not be interpolated"
outdated:
title: "This translation is outdated"
description: "The default translation for this key has changed since this override was created. Please check below that your translation matches any changes that have been made to the original intent."
diff --git a/frontend/discourse/admin/components/admin-interpolation-keys.gjs b/frontend/discourse/admin/components/admin-interpolation-keys.gjs
new file mode 100644
index 00000000000..8321108aacb
--- /dev/null
+++ b/frontend/discourse/admin/components/admin-interpolation-keys.gjs
@@ -0,0 +1,50 @@
+import { fn } from "@ember/helper";
+import { on } from "@ember/modifier";
+import concatClass from "discourse/helpers/concat-class";
+import { i18n } from "discourse-i18n";
+
+function pillClass(item) {
+ if (item.isInvalid) {
+ return "is-invalid";
+ }
+ if (item.isUsed) {
+ return "is-used";
+ }
+ return null;
+}
+
+function pillTitle(item) {
+ if (item.isInvalid) {
+ return i18n("admin.site_text.interpolation_key_invalid");
+ }
+ if (item.isUsed) {
+ return i18n("admin.site_text.interpolation_key_used");
+ }
+ return i18n("admin.site_text.interpolation_key_insert");
+}
+
+<template>
+ {{#if @keys.length}}
+ <div class="interpolation-keys">
+ {{#each @keys as |item|}}
+ {{#if item.isInvalid}}
+ <span
+ class={{concatClass "interpolation-keys__pill" (pillClass item)}}
+ title={{pillTitle item}}
+ >
+ {{item.key}}
+ </span>
+ {{else}}
+ <button
+ type="button"
+ class={{concatClass "interpolation-keys__pill" (pillClass item)}}
+ title={{pillTitle item}}
+ {{on "click" (fn @onInsertKey item.key)}}
+ >
+ {{item.key}}
+ </button>
+ {{/if}}
+ {{/each}}
+ </div>
+ {{/if}}
+</template>
diff --git a/frontend/discourse/admin/controllers/admin-email-templates/edit.js b/frontend/discourse/admin/controllers/admin-email-templates/edit.js
index a51f3c3232c..560e9aa8631 100644
--- a/frontend/discourse/admin/controllers/admin-email-templates/edit.js
+++ b/frontend/discourse/admin/controllers/admin-email-templates/edit.js
@@ -4,6 +4,7 @@ import { action } from "@ember/object";
import { dependentKeyCompat } from "@ember/object/compat";
import { service } from "@ember/service";
import BufferedProxy from "ember-buffered-proxy/proxy";
+import { interpolationKeysWithStatus as computeInterpolationKeysWithStatus } from "discourse/admin/lib/interpolation-keys";
import { popupAjaxError } from "discourse/lib/ajax-error";
import discourseComputed from "discourse/lib/decorators";
import { isObject } from "discourse/lib/object";
@@ -16,6 +17,9 @@ export default class AdminEmailTemplatesEditController extends Controller {
@tracked emailTemplate = null;
saved = false;
+ #activeTextarea = null;
+ #lastCursorPos = null;
+
@cached
@dependentKeyCompat
get buffered() {
@@ -49,6 +53,57 @@ export default class AdminEmailTemplatesEditController extends Controller {
}
}
+ @action
+ trackTextarea(event) {
+ const target = event.target;
+ if (target.tagName === "TEXTAREA" || target.tagName === "INPUT") {
+ this.#activeTextarea = target;
+ }
+ }
+
+ @action
+ saveCursorPos(event) {
+ const target = event.target;
+ if (target.tagName === "TEXTAREA" || target.tagName === "INPUT") {
+ this.#lastCursorPos = {
+ start: target.selectionStart,
+ end: target.selectionEnd,
+ };
+ }
+ }
+
+ resetTextarea() {
+ this.#activeTextarea = null;
+ this.#lastCursorPos = null;
+ }
+
+ @action
+ registerTextarea(element) {
+ this.#activeTextarea = element.querySelector("textarea") ?? element;
+ }
+
+ @action
+ insertInterpolationKey(key) {
+ const textarea = this.#activeTextarea;
+ if (!textarea) {
+ return;
+ }
+
+ const token = `%{${key}}`;
+ const start = this.#lastCursorPos?.start ?? textarea.value.length;
+ const end = this.#lastCursorPos?.end ?? textarea.value.length;
+
+ textarea.focus();
+ textarea.setSelectionRange(start, end);
+ document.execCommand("insertText", false, token);
+
+ const newPos = textarea.selectionStart;
+ this.#lastCursorPos = { start: newPos, end: newPos };
+
+ const field = textarea.tagName === "INPUT" ? "subject" : "body";
+ this.buffered.set(field, textarea.value);
+ }
+
@action
saveChanges() {
this.set("saved", false);
@@ -79,4 +134,16 @@ export default class AdminEmailTemplatesEditController extends Controller {
},
});
}
+
+ @discourseComputed(
+ "buffered.subject",
+ "buffered.body",
+ "emailTemplate.interpolation_keys"
+ )
+ interpolationKeysWithStatus(subject, body, keys) {
+ return computeInterpolationKeysWithStatus(
+ `${subject || ""} ${body || ""}`,
+ keys
+ );
+ }
}
diff --git a/frontend/discourse/admin/controllers/admin-site-text/edit.js b/frontend/discourse/admin/controllers/admin-site-text/edit.js
index 319dbd880d4..56af7fa9edd 100644
--- a/frontend/discourse/admin/controllers/admin-site-text/edit.js
+++ b/frontend/discourse/admin/controllers/admin-site-text/edit.js
@@ -4,6 +4,7 @@ import { action } from "@ember/object";
import { dependentKeyCompat } from "@ember/object/compat";
import { service } from "@ember/service";
import BufferedProxy from "ember-buffered-proxy/proxy";
+import { interpolationKeysWithStatus as computeInterpolationKeysWithStatus } from "discourse/admin/lib/interpolation-keys";
import { popupAjaxError } from "discourse/lib/ajax-error";
import discourseComputed from "discourse/lib/decorators";
import { i18n } from "discourse-i18n";
@@ -12,10 +13,12 @@ export default class AdminSiteTextEdit extends Controller {
@service dialog;
@tracked siteText;
-
saved = false;
queryParams = ["locale"];
+ #activeTextarea = null;
+ #lastCursorPos = null;
+
@cached
@dependentKeyCompat
get buffered() {
@@ -34,6 +37,52 @@ export default class AdminSiteTextEdit extends Controller {
return status === "outdated";
}
+ @action
+ trackTextarea(event) {
+ this.#activeTextarea = event.target;
+ }
+
+ @action
+ saveCursorPos() {
+ const textarea = this.#activeTextarea;
+ if (textarea) {
+ this.#lastCursorPos = {
+ start: textarea.selectionStart,
+ end: textarea.selectionEnd,
+ };
+ }
+ }
+
+ resetTextarea() {
+ this.#activeTextarea = null;
+ this.#lastCursorPos = null;
+ }
+
+ @action
+ registerTextarea(element) {
+ this.#activeTextarea = element.querySelector("textarea") ?? element;
+ }
+
+ @action
+ insertInterpolationKey(key) {
+ const textarea = this.#activeTextarea;
+ if (!textarea) {
+ return;
+ }
+
+ const token = `%{${key}}`;
+ const start = this.#lastCursorPos?.start ?? textarea.value.length;
+ const end = this.#lastCursorPos?.end ?? textarea.value.length;
+
+ textarea.focus();
+ textarea.setSelectionRange(start, end);
+ document.execCommand("insertText", false, token);
+
+ const newPos = textarea.selectionStart;
+ this.#lastCursorPos = { start: newPos, end: newPos };
+ this.buffered.set("value", textarea.value);
+ }
+
@action
saveChanges() {
const attrs = this.buffered.getProperties("value");
@@ -77,7 +126,8 @@ export default class AdminSiteTextEdit extends Controller {
.catch(popupAjaxError);
}
- get interpolationKeys() {
- return this.siteText.interpolation_keys.join(", ");
+ @discourseComputed("buffered.value", "siteText.interpolation_keys")
+ interpolationKeysWithStatus(value, keys) {
+ return computeInterpolationKeysWithStatus(value, keys);
}
}
diff --git a/frontend/discourse/admin/lib/interpolation-keys.js b/frontend/discourse/admin/lib/interpolation-keys.js
new file mode 100644
index 00000000000..30268bd6627
--- /dev/null
+++ b/frontend/discourse/admin/lib/interpolation-keys.js
@@ -0,0 +1,26 @@
+export function interpolationKeysWithStatus(content, keys) {
+ if (!keys) {
+ return [];
+ }
+
+ const knownKeys = new Set(keys);
+ const usedKeys = new Set();
+ const invalidKeys = new Set();
+
+ if (content) {
+ const matches = content.match(/%\{(\w+)\}/g) || [];
+ for (const m of matches) {
+ const k = m.slice(2, -1);
+ if (knownKeys.has(k)) {
+ usedKeys.add(k);
+ } else {
+ invalidKeys.add(k);
+ }
+ }
+ }
+
+ return [
+ ...keys.map((key) => ({ key, isUsed: usedKeys.has(key) })),
+ ...[...invalidKeys].map((key) => ({ key, isInvalid: true })),
+ ];
+}
diff --git a/frontend/discourse/admin/routes/admin-email-templates/edit.js b/frontend/discourse/admin/routes/admin-email-templates/edit.js
index 3976dc83e41..89a69bb7443 100644
--- a/frontend/discourse/admin/routes/admin-email-templates/edit.js
+++ b/frontend/discourse/admin/routes/admin-email-templates/edit.js
@@ -8,6 +8,7 @@ export default class AdminEmailTemplatesEditRoute extends Route {
}
setupController(controller, emailTemplate) {
+ controller.resetTextarea();
controller.setProperties({ emailTemplate, saved: false });
scrollTop();
}
diff --git a/frontend/discourse/admin/routes/admin-site-text/edit.js b/frontend/discourse/admin/routes/admin-site-text/edit.js
index d96e492c4cb..4e267c287f2 100644
--- a/frontend/discourse/admin/routes/admin-site-text/edit.js
+++ b/frontend/discourse/admin/routes/admin-site-text/edit.js
@@ -21,6 +21,7 @@ export default class AdminSiteTextEditRoute extends Route {
return locale.value === controller.locale;
}).name;
+ controller.resetTextarea();
controller.setProperties({
siteText,
saved: false,
diff --git a/frontend/discourse/admin/templates/admin-email-templates/edit.gjs b/frontend/discourse/admin/templates/admin-email-templates/edit.gjs
index 14679f8247c..132960489a9 100644
--- a/frontend/discourse/admin/templates/admin-email-templates/edit.gjs
+++ b/frontend/discourse/admin/templates/admin-email-templates/edit.gjs
@@ -1,6 +1,9 @@
import { Input } from "@ember/component";
import { hash } from "@ember/helper";
+import { on } from "@ember/modifier";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { LinkTo } from "@ember/routing";
+import AdminInterpolationKeys from "discourse/admin/components/admin-interpolation-keys";
import DButton from "discourse/components/d-button";
import DEditor from "discourse/components/d-editor";
import PluginOutlet from "discourse/components/plugin-outlet";
@@ -31,6 +34,8 @@ export default <template>
<Input
@value={{@controller.buffered.subject}}
class="email-template__subject"
+ {{on "focusin" @controller.trackTextarea}}
+ {{on "focusout" @controller.saveCursorPos}}
/>
{{/if}}
<br />
@@ -47,12 +52,20 @@ export default <template>
}}</LinkTo></h3>
{{else}}
<DEditor
+ {{didInsert @controller.registerTextarea}}
@value={{@controller.buffered.body}}
@forceEditorMode={{USER_OPTION_COMPOSITION_MODES.markdown}}
class="email-template__body"
+ {{on "focusin" @controller.trackTextarea}}
+ {{on "focusout" @controller.saveCursorPos}}
/>
{{/if}}
+ <AdminInterpolationKeys
+ @keys={{@controller.interpolationKeysWithStatus}}
+ @onInsertKey={{@controller.insertInterpolationKey}}
+ />
+
<SaveControls
@model={{@controller.emailTemplate}}
@action={{@controller.saveChanges}}
diff --git a/frontend/discourse/admin/templates/admin-site-text/edit.gjs b/frontend/discourse/admin/templates/admin-site-text/edit.gjs
index c53b55b852c..890611df0ac 100644
--- a/frontend/discourse/admin/templates/admin-site-text/edit.gjs
+++ b/frontend/discourse/admin/templates/admin-site-text/edit.gjs
@@ -1,6 +1,8 @@
import { fn, hash } from "@ember/helper";
import { on } from "@ember/modifier";
+import didInsert from "@ember/render-modifiers/modifiers/did-insert";
import { LinkTo } from "@ember/routing";
+import AdminInterpolationKeys from "discourse/admin/components/admin-interpolation-keys";
import DButton from "discourse/components/d-button";
import ExpandingTextArea from "discourse/components/expanding-text-area";
import SaveControls from "discourse/components/save-controls";
@@ -36,17 +38,19 @@ export default <template>
{{/if}}
<ExpandingTextArea
+ {{didInsert @controller.registerTextarea}}
{{on "input" (withEventValue (fn (mut @controller.buffered.value)))}}
+ {{on "focusin" @controller.trackTextarea}}
+ {{on "focusout" @controller.saveCursorPos}}
value={{@controller.buffered.value}}
rows="1"
class="site-text-value"
/>
- {{#if @controller.siteText.has_interpolation_keys}}
- <div class="desc">{{i18n "admin.site_text.interpolation_keys"}}
- {{@controller.interpolationKeys}}
- </div>
- {{/if}}
+ <AdminInterpolationKeys
+ @keys={{@controller.interpolationKeysWithStatus}}
+ @onInsertKey={{@controller.insertInterpolationKey}}
+ />
<SaveControls
@model={{@controller.siteText}}
diff --git a/spec/models/translation_override_spec.rb b/spec/models/translation_override_spec.rb
index c7f64cd1823..868cb2d7e81 100644
--- a/spec/models/translation_override_spec.rb
+++ b/spec/models/translation_override_spec.rb
@@ -48,9 +48,7 @@ RSpec.describe TranslationOverride do
].each do |i18n_key|
it "should validate keys for #{i18n_key}" do
interpolation_key_names =
- described_class::ALLOWED_CUSTOM_INTERPOLATION_KEYS.find do |keys, _|
- keys.include?("user_notifications.user_")
- end
+ described_class.custom_interpolation_keys("user_notifications.user_")
string_with_interpolation_keys =
interpolation_key_names.map { |x| "%{#{x}}" }.join(" ")
diff --git a/spec/requests/admin/email_templates_controller_spec.rb b/spec/requests/admin/email_templates_controller_spec.rb
index 79684dc88c2..902d7198a6c 100644
--- a/spec/requests/admin/email_templates_controller_spec.rb
+++ b/spec/requests/admin/email_templates_controller_spec.rb
@@ -57,6 +57,39 @@ RSpec.describe Admin::EmailTemplatesController do
expect(template["can_revert"]).to eq(false)
end
+ it "returns interpolation_keys for each email template" do
+ get "/admin/email/templates.json"
+ expect(response.status).to eq(200)
+
+ templates = response.parsed_body["email_templates"]
+ template = templates.find { |t| t["id"] == "user_notifications.admin_login" }
+
+ expect(template["interpolation_keys"]).to eq(
+ %w[base_url email_prefix email_token site_name],
+ )
+ end
+
+ it "returns interpolation_keys from body when subject is pluralized" do
+ get "/admin/email/templates.json"
+ expect(response.status).to eq(200)
+
+ templates = response.parsed_body["email_templates"]
+ template = templates.find { |t| t["id"] == "system_messages.pending_users_reminder" }
+
+ expect(template["interpolation_keys"]).to eq(%w[base_url])
+ end
+
+ it "returns empty interpolation_keys for templates without any keys" do
+ get "/admin/email/templates.json"
+ expect(response.status).to eq(200)
+
+ templates = response.parsed_body["email_templates"]
+ template =
+ templates.find { |t| t["id"] == "system_messages.download_remote_images_disabled" }
+
+ expect(template["interpolation_keys"]).to eq([])
+ end
+
it "includes custom email template keys added via modifier" do
custom_keys = %w[custom.email_template_one custom.email_template_two]
@@ -239,6 +272,25 @@ RSpec.describe Admin::EmailTemplatesController do
include_examples "invalid email template"
end
+ context "when body contains malformed interpolation keys" do
+ let(:email_subject) { "%{email_prefix} Foo" }
+ let(:email_body) { "Hello %{user.username}" }
+
+ let(:expected_errors) do
+ [
+ "<b>Body</b>: #{
+ I18n.t(
+ "activerecord.errors.models.translation_overrides.attributes.value.invalid_interpolation_keys",
+ keys: "user.username",
+ count: 1,
+ )
+ }",
+ ]
+ end
+
+ include_examples "invalid email template"
+ end
+
context "when subject and body contain all required interpolation keys" do
let(:email_subject) { "%{email_prefix} Foo" }
let(:email_body) { "The body contains [%{site_name}](%{base_url}) and %{email_token}." }
diff --git a/spec/requests/admin/site_texts_controller_spec.rb b/spec/requests/admin/site_texts_controller_spec.rb
index cc0c53e80ca..819feeb7238 100644
--- a/spec/requests/admin/site_texts_controller_spec.rb
+++ b/spec/requests/admin/site_texts_controller_spec.rb
@@ -245,7 +245,6 @@ RSpec.describe Admin::SiteTextsController do
can_revert: overridden,
overridden:,
interpolation_keys:,
- has_interpolation_keys: interpolation_keys.present?,
}
end
@@ -478,11 +477,7 @@ RSpec.describe Admin::SiteTextsController do
expect(response.status).to eq(200)
json = response.parsed_body
- expect(json["site_text"]["interpolation_keys"]).to include(
- "username",
- "name",
- "name_or_username",
- )
+ expect(json["site_text"]["interpolation_keys"]).to eq(%w[name name_or_username username])
end
context "with plural keys" do
diff --git a/spec/system/admin_email_templates_spec.rb b/spec/system/admin_email_templates_spec.rb
index ff5b03d2153..c66104a498b 100644
--- a/spec/system/admin_email_templates_spec.rb
+++ b/spec/system/admin_email_templates_spec.rb
@@ -51,4 +51,16 @@ describe "Admin Email Templates", type: :system do
"#{Discourse.base_url}/admin/customize/site_texts?q=system_messages.reviewables_reminder",
)
end
+
+ it "shows interpolation keys for templates that have them" do
+ email_templates_page.visit_template("user_notifications.admin_login")
+ expect(email_templates_page).to have_interpolation_keys(
+ %w[base_url email_prefix email_token site_name],
+ )
+ end
+
+ it "does not show interpolation keys for templates without any" do
+ email_templates_page.visit_template("system_messages.download_remote_images_disabled")
+ expect(email_templates_page).to have_no_interpolation_keys
+ end
end
diff --git a/spec/system/page_objects/pages/admin_email_templates.rb b/spec/system/page_objects/pages/admin_email_templates.rb
index e5015159bab..0768ed69a9e 100644
--- a/spec/system/page_objects/pages/admin_email_templates.rb
+++ b/spec/system/page_objects/pages/admin_email_templates.rb
@@ -58,6 +58,16 @@ module PageObjects
expect(link[:href]).to eq(href)
expect(link.text).to eq(I18n.t("admin_js.admin.customize.email_templates.multiple_bodies"))
end
+
+ def has_interpolation_keys?(keys)
+ container = find(".email-template .interpolation-keys")
+ keys.each { |key| expect(container).to have_css("button", text: key) }
+ true
+ end
+
+ def has_no_interpolation_keys?
+ has_no_css?(".email-template .interpolation-keys")
+ end
end
end
end