Skip to content

Commit 1f17179

Browse files
committed
fix: translate toasts dynamically
1 parent 3dc2374 commit 1f17179

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/utils/2.toasts.js

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import Translation from 'src/structures/constants/translation.js';
22
import { buttonCSS } from './1.variables.js';
33
import merge from './merge.js';
4+
import eventManager from './eventManager.js';
5+
import toArray from './toArray.js';
6+
7+
let ready = false;
8+
9+
eventManager.on('underscript:ready', () => ready = true);
410

511
export function blankToast() {
612
return new SimpleToast();
@@ -13,6 +19,11 @@ export function toast(arg) {
1319
text: arg,
1420
};
1521
}
22+
const isTranslation = [
23+
arg.text,
24+
arg.title,
25+
...toArray(arg.buttons).map(({ text }) => text),
26+
].some((obj) => obj instanceof Translation);
1627
const defaults = {
1728
footer: 'via UnderScript',
1829
css: {
@@ -24,7 +35,15 @@ export function toast(arg) {
2435
},
2536
},
2637
};
27-
return new SimpleToast(merge(defaults, arg));
38+
if (ready && isTranslation) preprocess(arg);
39+
const slice = new SimpleToast(merge(defaults, arg));
40+
if (!ready && isTranslation && slice.exists()) {
41+
const el = $('#AlertToast > div:last');
42+
eventManager.on('underscript:ready', () => {
43+
process(slice, el, arg);
44+
});
45+
}
46+
return slice;
2847
}
2948

3049
export function errorToast(error) {
@@ -37,7 +56,7 @@ export function errorToast(error) {
3756
}
3857

3958
const lToast = {
40-
title: error.name || error.title || Translation.ERROR.translate(),
59+
title: error.name || error.title || Translation.ERROR,
4160
text: error.message || error.text || getStack(error.error || error) || error,
4261
css: {
4362
'background-color': 'rgba(200,0,0,0.6)',
@@ -68,7 +87,7 @@ export function infoToast(arg, key, val = '1') {
6887
},
6988
};
7089
const defaults = {
71-
title: Translation.INFO.translate(),
90+
title: Translation.INFO,
7291
css: {
7392
'font-family': 'inherit',
7493
},
@@ -79,7 +98,7 @@ export function infoToast(arg, key, val = '1') {
7998
export function dismissable({ title, text, key, value = 'true', css = {} }) {
8099
if (localStorage.getItem(key) === value) return undefined;
81100
const buttons = {
82-
text: Translation.DISMISS.translate(),
101+
text: Translation.DISMISS,
83102
className: 'dismiss',
84103
css: buttonCSS,
85104
onclick: (e) => {
@@ -94,3 +113,21 @@ export function dismissable({ title, text, key, value = 'true', css = {} }) {
94113
css,
95114
});
96115
}
116+
117+
function preprocess(arg) {
118+
['text', 'title'].forEach((prop) => {
119+
if (arg[prop] instanceof Translation) arg[prop] = `${arg[prop]}`;
120+
});
121+
toArray(arg.buttons).forEach((button) => {
122+
if (button.text instanceof Translation) button.text = `${button.text}`;
123+
});
124+
}
125+
126+
function process(instance, el, { text, title, buttons }) {
127+
if (text instanceof Translation) instance.setText(`${text}`);
128+
if (title instanceof Translation) el.find('> span:first').text(title);
129+
const $buttons = el.find('> button');
130+
toArray(buttons).forEach((button, i) => {
131+
if (button.text instanceof Translation) $buttons[i].textContent = button.text;
132+
});
133+
}

0 commit comments

Comments
 (0)