11import Translation from 'src/structures/constants/translation.js' ;
22import { buttonCSS } from './1.variables.js' ;
33import 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
511export 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
3049export 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') {
7998export 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