mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Simplify setting of Trix editor translations
Co-authored-by: David Cook <david@redcliffs.net>
This commit is contained in:
@@ -28,8 +28,6 @@ export default class extends Controller {
|
||||
) {
|
||||
this.#addHRButton();
|
||||
}
|
||||
|
||||
this.#setTranslations();
|
||||
};
|
||||
|
||||
#addHRButton = () => {
|
||||
@@ -44,57 +42,4 @@ export default class extends Controller {
|
||||
this.#trixActionInvoke(event);
|
||||
});
|
||||
};
|
||||
|
||||
#setTranslation(selector, attribute, value) {
|
||||
let element = this.element.parentElement.querySelector(selector);
|
||||
if(element && element.hasAttribute(attribute)) {
|
||||
element.setAttribute(attribute, value);
|
||||
}
|
||||
}
|
||||
|
||||
#setTranslations() {
|
||||
if(I18n.t("js.trix")) {
|
||||
// Calling 'Trix.config.lang = I18n.t("js.trix")' doesn't work due to read-only error, so set
|
||||
// translations one at a time.
|
||||
for (const [key, translation] of Object.entries(I18n.t("js.trix"))) {
|
||||
|
||||
// Set all translations (only works in Firefox for some reason)
|
||||
Trix.config.lang[key] = translation;
|
||||
|
||||
// Set toolbar translations (Chrome)
|
||||
this.#setTranslation(
|
||||
`[data-trix-action="${this.#attributeOrActionForTranslationKey(key)}"]`,
|
||||
"title",
|
||||
translation
|
||||
);
|
||||
this.#setTranslation(
|
||||
`[data-trix-attribute="${this.#attributeOrActionForTranslationKey(key)}"]`,
|
||||
"title",
|
||||
translation
|
||||
);
|
||||
}
|
||||
|
||||
// Set translations for link dialog (Chrome)
|
||||
this.#setTranslation(`[data-trix-dialog="href"] input`,
|
||||
"aria-label", I18n.t("js.trix.url"));
|
||||
this.#setTranslation(`[data-trix-dialog="href"] input`,
|
||||
"placeholder", I18n.t("js.trix.urlPlaceholder"));
|
||||
this.#setTranslation('.trix-dialog--link input[data-trix-method="setAttribute"]',
|
||||
"value", I18n.t("js.trix.link"));
|
||||
this.#setTranslation('.trix-dialog--link input[data-trix-method="removeAttribute"]',
|
||||
"value", I18n.t("js.trix.unlink"));
|
||||
}
|
||||
}
|
||||
|
||||
#attributeOrActionForTranslationKey(key) {
|
||||
let mapping = {
|
||||
"bullets": "bullet",
|
||||
"link": "href",
|
||||
"numbers": "number",
|
||||
"indent": "increaseNestingLevel",
|
||||
"outdent": "decreaseNestingLevel"
|
||||
}[key];
|
||||
|
||||
return mapping || key;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,11 @@ debounced.initialize({ input: { wait: 300 } });
|
||||
|
||||
import Trix from "trix";
|
||||
|
||||
document.addEventListener("trix-before-initialize", (event) => {
|
||||
// Set Trix translations
|
||||
Object.assign(Trix.config.lang, I18n.t("js.trix"));
|
||||
});
|
||||
|
||||
document.addEventListener("trix-file-accept", (event) => {
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user