Skip to content

Commit 4d6bdec

Browse files
author
Sebi Nemeth
committed
rafctor code, force rerender translations in toggle()
1 parent c838e8a commit 4d6bdec

File tree

2 files changed

+67
-52
lines changed

2 files changed

+67
-52
lines changed

dist/index.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,13 @@ class ZeroWidthEncoder {
108108
}
109109
}
110110
class LiveTranslatorEnabler {
111-
constructor(persist) {
111+
constructor(options, callback) {
112112
this._enabled = false;
113-
this.persist = persist;
113+
this._persist = options.persist || false;
114+
this._options = options;
115+
this._callback = callback;
114116
const savedRaw = localStorage.getItem('live-translator-enabled');
115-
if (persist && savedRaw) {
117+
if (this._persist && savedRaw) {
116118
const saved = JSON.parse(savedRaw);
117119
if (typeof saved === 'boolean') {
118120
this.toggle(saved);
@@ -129,9 +131,14 @@ class LiveTranslatorEnabler {
129131
else {
130132
this._enabled = !this._enabled;
131133
}
132-
if (this.persist) {
134+
if (this._persist) {
133135
localStorage.setItem('live-translator-enabled', JSON.stringify(this._enabled));
134136
}
137+
// Refresh translations to show immediately
138+
const originalLocale = this._options.i18n.locale;
139+
this._options.i18n.locale = '';
140+
this._options.i18n.locale = originalLocale;
141+
this._callback();
135142
}
136143
}
137144
const createBadge = (meta, options, attribute) => {
@@ -158,27 +165,8 @@ const createBadge = (meta, options, attribute) => {
158165
exports.LiveTranslatorPlugin = {
159166
install(app, options) {
160167
console.log('LiveTranslator is installed');
168+
// intantiate logic
161169
const zw = new ZeroWidthEncoder();
162-
const ltEnabler = new LiveTranslatorEnabler(options.persist || false);
163-
const enableButton = document.createElement('button');
164-
enableButton.innerText = 'LT';
165-
enableButton.classList.add('live-translator-enable-button');
166-
const indicator = document.createElement('span');
167-
indicator.classList.add('live-translator-enable-button-indicator');
168-
enableButton.appendChild(indicator);
169-
enableButton.addEventListener('click', () => {
170-
ltEnabler.toggle();
171-
visualize();
172-
// Refresh translations to show immediately
173-
const originalLocale = options.i18n.locale;
174-
options.i18n.locale = '';
175-
options.i18n.locale = originalLocale;
176-
});
177-
document.body.appendChild(enableButton);
178-
const style = document.createElement('style');
179-
style.id = 'live-translator-plugin-style';
180-
style.innerHTML = css;
181-
document.head.appendChild(style);
182170
const visualize = () => {
183171
const badges = document.querySelectorAll('.live-translator-badge');
184172
console.log('clearing', badges.length, 'badges');
@@ -230,6 +218,23 @@ exports.LiveTranslatorPlugin = {
230218
}
231219
}
232220
};
221+
const ltEnabler = new LiveTranslatorEnabler(options, visualize);
222+
// bind & style UI
223+
const style = document.createElement('style');
224+
style.id = 'live-translator-plugin-style';
225+
style.innerHTML = css;
226+
document.head.appendChild(style);
227+
const enableButton = document.createElement('button');
228+
enableButton.innerText = 'LT';
229+
enableButton.classList.add('live-translator-enable-button');
230+
const indicator = document.createElement('span');
231+
indicator.classList.add('live-translator-enable-button-indicator');
232+
enableButton.appendChild(indicator);
233+
enableButton.addEventListener('click', () => {
234+
ltEnabler.toggle();
235+
});
236+
document.body.appendChild(enableButton);
237+
// encode meta to translation strings
233238
const originalFormatter = options.i18n.formatter;
234239
options.i18n.formatter = {
235240
interpolate(message, values, path) {
@@ -243,6 +248,7 @@ exports.LiveTranslatorPlugin = {
243248
return (original && ltEnabler.enabled()) ? [meta, ...original] : original;
244249
},
245250
};
251+
// decode & visualize meta
246252
const throttler = (0, throttle_1.default)(visualize, 800);
247253
const observer = new MutationObserver(throttler);
248254
observer.observe(document.documentElement, {

src/index.ts

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,17 @@ class ZeroWidthEncoder {
108108

109109
class LiveTranslatorEnabler {
110110
_enabled: boolean
111-
persist: boolean
111+
_persist: boolean
112+
_options: LiveTranslatorPluginOptions
113+
_callback: CallableFunction
112114

113-
constructor (persist: boolean) {
115+
constructor (options: LiveTranslatorPluginOptions, callback: CallableFunction) {
114116
this._enabled = false
115-
this.persist = persist
117+
this._persist = options.persist || false
118+
this._options = options
119+
this._callback = callback
116120
const savedRaw = localStorage.getItem('live-translator-enabled')
117-
if (persist && savedRaw) {
121+
if (this._persist && savedRaw) {
118122
const saved = JSON.parse(savedRaw)
119123
if (typeof saved === 'boolean') {
120124
this.toggle(saved)
@@ -132,9 +136,15 @@ class LiveTranslatorEnabler {
132136
} else {
133137
this._enabled = !this._enabled
134138
}
135-
if (this.persist) {
139+
if (this._persist) {
136140
localStorage.setItem('live-translator-enabled', JSON.stringify(this._enabled))
137141
}
142+
// Refresh translations to show immediately
143+
const originalLocale = this._options.i18n.locale
144+
this._options.i18n.locale = ''
145+
this._options.i18n.locale = originalLocale
146+
147+
this._callback()
138148
}
139149
}
140150

@@ -175,30 +185,9 @@ const createBadge = (meta: TranslationMeta, options: LiveTranslatorPluginOptions
175185
export const LiveTranslatorPlugin = {
176186
install (app: VueConstructor<Vue>, options: LiveTranslatorPluginOptions) {
177187
console.log('LiveTranslator is installed')
178-
const zw = new ZeroWidthEncoder()
179-
const ltEnabler = new LiveTranslatorEnabler(options.persist || false)
180-
181-
const enableButton = document.createElement('button')
182-
enableButton.innerText = 'LT'
183-
enableButton.classList.add('live-translator-enable-button')
184-
const indicator = document.createElement('span')
185-
indicator.classList.add('live-translator-enable-button-indicator')
186-
enableButton.appendChild(indicator)
187-
enableButton.addEventListener('click', () => {
188-
ltEnabler.toggle()
189-
visualize()
190-
// Refresh translations to show immediately
191-
const originalLocale = options.i18n.locale
192-
options.i18n.locale = ''
193-
options.i18n.locale = originalLocale
194-
})
195-
document.body.appendChild(enableButton)
196-
197-
const style = document.createElement('style')
198-
style.id = 'live-translator-plugin-style'
199-
style.innerHTML = css
200-
document.head.appendChild(style)
201188

189+
// intantiate logic
190+
const zw = new ZeroWidthEncoder()
202191
const visualize = () => {
203192
const badges = document.querySelectorAll('.live-translator-badge')
204193
console.log('clearing', badges.length, 'badges')
@@ -258,7 +247,26 @@ export const LiveTranslatorPlugin = {
258247
}
259248
}
260249
}
250+
const ltEnabler = new LiveTranslatorEnabler(options, visualize)
251+
252+
// bind & style UI
253+
const style = document.createElement('style')
254+
style.id = 'live-translator-plugin-style'
255+
style.innerHTML = css
256+
document.head.appendChild(style)
257+
258+
const enableButton = document.createElement('button')
259+
enableButton.innerText = 'LT'
260+
enableButton.classList.add('live-translator-enable-button')
261+
const indicator = document.createElement('span')
262+
indicator.classList.add('live-translator-enable-button-indicator')
263+
enableButton.appendChild(indicator)
264+
enableButton.addEventListener('click', () => {
265+
ltEnabler.toggle()
266+
})
267+
document.body.appendChild(enableButton)
261268

269+
// encode meta to translation strings
262270
const originalFormatter = options.i18n.formatter
263271
options.i18n.formatter = {
264272
interpolate (message, values, path) {
@@ -275,6 +283,7 @@ export const LiveTranslatorPlugin = {
275283
},
276284
}
277285

286+
// decode & visualize meta
278287
const throttler = throttle(visualize, 800)
279288
const observer = new MutationObserver(throttler)
280289
observer.observe(document.documentElement,

0 commit comments

Comments
 (0)