forked from telepenin/cloudlinux-doc-theme
-
Notifications
You must be signed in to change notification settings - Fork 7
/
GTranslateIO.vue
41 lines (36 loc) · 1.15 KB
/
GTranslateIO.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<script>
import Vue from "vue";
export default {
name: "gtranslate-io",
render() {
const isClient = typeof window !== "undefined";
if (isClient) {
var self = this;
const proto = "https:";
const path = window.location.pathname.split(this.$localePath).join("");
const host = this.$site.themeConfig.translationSource;
const url = `${proto}//${this.$lang}.${host}/${path}`;
// gtranslate.io has ability to work via dns
fetch(url)
.then(response => {
if (!response.ok) {
return Promise.reject(new Error(response.statusText));
}
return response.text();
})
.then(text => {
const parser = new DOMParser();
const htmlDoc = parser.parseFromString(text, "text/html");
const el = htmlDoc.getElementsByClassName("content")[0];
const res = Vue.compile("<div>" + el.innerHTML + "</div>");
self.$options.render = res.render;
self.$options.staticRenderFns = res.staticRenderFns;
self.$forceUpdate();
})
.catch(err => {
console.error(err);
});
}
}
};
</script>