Skip to content

Commit c7283f7

Browse files
author
Sebi Nemeth
committed
Merge branch 'main' into develop
2 parents 689844b + c561503 commit c7283f7

File tree

7 files changed

+134
-164
lines changed

7 files changed

+134
-164
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,18 @@ Vue.use(LiveTranslatorPlugin, {
2323
translationLink (meta: TranslationMeta) {
2424
return `<weblate_url>/translate/<project>/<component>/${meta.locale}/?q=context:=${meta.path}`
2525
}
26+
```
27+
28+
## Develop
29+
```bash
30+
git clone https://github.com/apicore-engineering/vue-i18n-live-translator-plugin
31+
```
32+
```bash
33+
cd vue-i18n-live-translator-plugin
34+
```
35+
```bash
36+
npm install
37+
```
38+
```bash
39+
husky install
2640
```

dist/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Vue, { VueConstructor } from 'vue';
21
import VueI18n from 'vue-i18n';
32
export type TranslationMeta = {
43
locale: string;
@@ -12,6 +11,6 @@ type LiveTranslatorPluginOptions = {
1211
persist?: boolean;
1312
};
1413
export declare const LiveTranslatorPlugin: {
15-
install(app: VueConstructor<Vue>, options: LiveTranslatorPluginOptions): void;
14+
install(app: any, options: LiveTranslatorPluginOptions): void;
1615
};
1716
export {};

dist/index.js

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
"use strict";
2-
var __importDefault = (this && this.__importDefault) || function (mod) {
3-
return (mod && mod.__esModule) ? mod : { "default": mod };
4-
};
5-
var _a;
6-
Object.defineProperty(exports, "__esModule", { value: true });
7-
exports.LiveTranslatorPlugin = void 0;
8-
const throttle_1 = __importDefault(require("lodash/throttle"));
1+
import throttle from 'lodash/throttle';
92
const css = `
103
.live-translator-enable-button {
114
position: fixed !important;
@@ -29,6 +22,11 @@ const css = `
2922
border-radius: 100%;
3023
background-color: red;
3124
}
25+
.live-translator-badge-wrapper {
26+
position: relative !important;
27+
width: 0px;
28+
height: 0px;
29+
}
3230
.live-translator-badge-container {
3331
position: absolute !important;
3432
display: flex;
@@ -60,6 +58,12 @@ const css = `
6058
}
6159
`;
6260
class ZeroWidthEncoder {
61+
static START = '\u200B';
62+
static ZERO = '\u200C';
63+
static ONE = '\u200D';
64+
static SPACE = '\u200E';
65+
static END = '\u200F';
66+
static PATTERN = `${this.START}[${this.ZERO}${this.ONE}${this.SPACE}]+${this.END}`;
6367
static encode(text) {
6468
const binary = text
6569
.split('')
@@ -101,14 +105,11 @@ class ZeroWidthEncoder {
101105
return text;
102106
}
103107
}
104-
_a = ZeroWidthEncoder;
105-
ZeroWidthEncoder.START = '\u200B';
106-
ZeroWidthEncoder.ZERO = '\u200C';
107-
ZeroWidthEncoder.ONE = '\u200D';
108-
ZeroWidthEncoder.SPACE = '\u200E';
109-
ZeroWidthEncoder.END = '\u200F';
110-
ZeroWidthEncoder.PATTERN = `${_a.START}[${_a.ZERO}${_a.ONE}${_a.SPACE}]+${_a.END}`;
111108
class LiveTranslatorManager {
109+
_enabled;
110+
_options;
111+
_enableButton;
112+
_indicator;
112113
constructor(options) {
113114
this._enabled = false;
114115
this._options = options;
@@ -142,18 +143,24 @@ class LiveTranslatorManager {
142143
const self = this;
143144
this._options.i18n.formatter = {
144145
interpolate(message, values, path) {
145-
const meta = ZeroWidthEncoder.encode(JSON.stringify({
146-
message,
147-
values,
148-
path,
149-
locale: self._options.i18n.locale,
150-
}));
151146
const original = originalFormatter.interpolate(message, values, path);
152-
return (original && self._enabled) ? [meta, ...original] : original;
147+
let meta = '';
148+
try {
149+
meta = ZeroWidthEncoder.encode(JSON.stringify({
150+
message,
151+
values,
152+
path,
153+
locale: self._options.i18n.locale,
154+
}));
155+
}
156+
catch (exception) {
157+
console.warn(path, exception);
158+
}
159+
return (original && meta && self._enabled) ? [meta, ...original] : original;
153160
},
154161
};
155162
// initialize decode & render
156-
const throttler = (0, throttle_1.default)(() => this.render(), 800);
163+
const throttler = throttle(() => this.render(), 800);
157164
const observer = new MutationObserver(throttler);
158165
observer.observe(document.documentElement, {
159166
subtree: true,
@@ -184,9 +191,9 @@ class LiveTranslatorManager {
184191
console.log(`%c Live Translator ${this._enabled ? 'ON' : 'OFF'} `, 'background: #222; color: #bada55');
185192
}
186193
render() {
187-
const badges = document.querySelectorAll('.live-translator-badge');
188-
badges.forEach((badge) => {
189-
badge.remove();
194+
const badgeWrappers = document.querySelectorAll('.live-translator-badge-wrapper');
195+
badgeWrappers.forEach((wrapper) => {
196+
wrapper.remove();
190197
});
191198
this._indicator.style.background = this._enabled ? 'lightgreen' : 'red';
192199
if (!this._enabled) {
@@ -200,7 +207,7 @@ class LiveTranslatorManager {
200207
const parent = node.parentElement;
201208
if (node instanceof Text) {
202209
const matches = node.textContent.match(re);
203-
for (const match of matches !== null && matches !== void 0 ? matches : []) {
210+
for (const match of matches ?? []) {
204211
const meta = JSON.parse(ZeroWidthEncoder.decode(match));
205212
badges.push(createBadge(meta, this._options));
206213
}
@@ -222,7 +229,10 @@ class LiveTranslatorManager {
222229
else {
223230
container = document.createElement('span');
224231
container.classList.add('live-translator-badge-container');
225-
parent.insertBefore(container, node);
232+
const relativeWrapper = document.createElement('span');
233+
relativeWrapper.classList.add('live-translator-badge-wrapper');
234+
relativeWrapper.appendChild(container);
235+
parent.insertBefore(relativeWrapper, node);
226236
}
227237
for (const badge of badges) {
228238
container.appendChild(badge);
@@ -255,7 +265,7 @@ const createBadge = (meta, options, attribute) => {
255265
});
256266
return badge;
257267
};
258-
exports.LiveTranslatorPlugin = {
268+
export const LiveTranslatorPlugin = {
259269
install(app, options) {
260270
console.log('LiveTranslator is installed');
261271
new LiveTranslatorManager(options);

package-lock.json

Lines changed: 41 additions & 106 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"types": "./dist/index.d.ts",
77
"scripts": {
88
"build": "tsc --declaration",
9-
"test": "echo \"Error: no test specified\" && exit 1",
10-
"prepare": "husky install"
9+
"test": "echo \"Error: no test specified\" && exit 1"
1110
},
1211
"husky": {
1312
"hooks": {
@@ -24,14 +23,14 @@
2423
"url": "https://github.com/apicore-engineering/vue-i18n-live-translator-plugin/issues"
2524
},
2625
"homepage": "https://github.com/apicore-engineering/vue-i18n-live-translator-plugin#readme",
27-
"dependencies": {
26+
"peerDependencies": {
2827
"lodash": "^4.17.21",
29-
"typescript": "^5.1.6",
30-
"vue": "^2.7.14",
31-
"vue-i18n": "^8.28.2"
28+
"vue-demi": "^0.14.5",
29+
"vue-i18n": "^8.0.0"
3230
},
3331
"devDependencies": {
3432
"@types/lodash": "^4.14.195",
35-
"husky": "^8.0.0"
33+
"husky": "^8.0.0",
34+
"typescript": "^5.1.6"
3635
}
3736
}

0 commit comments

Comments
 (0)