123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- import axios from 'axios'
- import Vue from 'vue'
- import { delayPerform } from 'js-perform-lock'
- const WindowsTranslateApi = 'https://jiasm.zfire.top/translate'
- function isNumberRegex(str) {
- const regexPatterns = [
- /^\d+$/,
- /^\d*\.\d+$/,
- /^([01]\d|2[0-3]):([0-5]\d)$/,
- /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)$/,
- /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/,
- /^[A-Za-z]+$/,
- /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$/,
- /^(?=.*[A-Za-z])(?=.*[\p{P}\p{S}])[\p{L}\p{P}\p{S}]+$/u,
- /^(?=.*\d)(?=.*[\p{P}\p{S}])[\d\p{P}\p{S}]+$/u,
- /^[\p{P}\s\dA-Za-z]+$/u
- ]
- return regexPatterns.some(pattern => pattern.test(str)) || !!window.Vue_Translation_Of_Text_Old_Data[str]
- }
- function pushNewText(data) {
- return new Promise(function (r, j) {
- axios
- .post(WindowsTranslateApi + '/api/v1/common/translationOfText', data)
- .then(response => {
- if (response.data.code === 0) {
- var obj = {}
- var obj2 = {}
- response.data.data.map(val => {
- obj[val[0]] = val[1]
- obj2[val[1]] = true
- })
- window.Vue_Translation_Of_Text_Old_Data = { ...(window.Vue_Translation_Of_Text_Old_Data || {}), ...obj2 }
- window.Vue_Translation_Of_Text_Data = { ...(window.Vue_Translation_Of_Text_Data || {}), ...obj }
- r({ ...obj })
- }
- })
- .catch(j)
- })
- }
- var deferredReplacement = (function () {
- var allKeywords = []
- var keywords = []
- var zhixingfun = []
- var index = 0
- var index2 = 0
- const dInit = new delayPerform(250).refactor(function () {
- let len = index
- let len2 = index2
- let keywords_ = keywords.splice(0, len)
- let zhixingfun_ = zhixingfun.splice(0, len2)
- index -= len
- index2 -= len2
- if (keywords_.length > 0) {
- pushNewText({
- keywords: keywords_,
- targetLanguage: window?.Vue_Translation_Of_Text_Type
- }).then(data => {
- for (var i = 0; i < len2; i++) {
- zhixingfun_?.[i]?.()
- }
- })
- } else if (zhixingfun_.length > 0) {
- setTimeout(function () {
- for (var i = 0; i < len2; i++) {
- zhixingfun_?.[i]?.()
- }
- }, 250)
- }
- })
- return function (text, cb) {
- dInit()
- if (!allKeywords.includes(text)) {
- index++
- allKeywords.push(text)
- keywords.push(text)
- }
- index2++
- zhixingfun.push(cb)
- }
- })()
- export const GlobalTextProcessor = {
- install(Vue) {
- Vue.mixin({
- updated() {
- this.$nextTick(() => {
- this.processTextNodes(this.$el)
- this.autoSelect('uni-tabbar-bottom')
- })
- },
- methods: {
-
- autoSelect(name) {
- document.querySelectorAll(`.${name}`).forEach(element => {
- this.processAbsolutely(element)
- })
- },
-
- autoHtmlSelect(name) {
- document.getElementsByTagName(name).forEach(element => {
- this.processAbsolutely(element)
- })
- },
- processTextNodes(el) {
-
- if (el.nodeType === Node.ELEMENT_NODE) {
-
- if (el.nodeName.toLowerCase() === 'td') {
- return
- }
- Array.from(el.childNodes).forEach(child => {
- if (child.nodeType === Node.TEXT_NODE) {
- let text = child.textContent
- let _child = child
- if (text && !isNumberRegex(text.trim())) {
- try {
- if (window?.Vue_Translation_Of_Text_Data?.[text.trim()]) {
- child.textContent = window?.Vue_Translation_Of_Text_Data?.[text.trim()]
- } else if (text.trim()) {
- deferredReplacement(text.trim(), function () {
- if (window.Vue_Translation_Of_Text_Data[text.trim()]) {
- _child.textContent = window.Vue_Translation_Of_Text_Data[text.trim()]
- }
- })
- }
- } catch (error) {}
- }
- } else if (child.nodeName === 'INPUT' || child.nodeName === 'TEXTAREA' || child.nodeName === 'SELECT') {
-
- let placeholder = child.getAttribute('placeholder')
- let _child = child
- if (placeholder && !isNumberRegex(placeholder.trim())) {
- try {
- if (window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + '']) {
- child.setAttribute('placeholder', window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + ''])
- } else if (placeholder.trim() + '') {
- deferredReplacement(placeholder.trim() + '', function () {
- if (window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + '']) {
- _child.setAttribute(
- 'placeholder',
- window?.Vue_Translation_Of_Text_Data?.[placeholder.trim() + '']
- )
- }
- })
- }
- } catch (error) {}
- }
- } else if (child?.classList?.contains?.('el-select')) {
-
- this.processSelect(child)
- } else {
-
- this.processTextNodes(child)
- }
- })
- }
- },
- processSelect(el) {
-
- if (el.nodeType === Node.ELEMENT_NODE) {
- Array.from(el.childNodes).forEach(child => {
- if (child.nodeName === 'INPUT') {
-
- let value = child.value
- let _child = child
- if (value && !isNumberRegex(value.trim())) {
- try {
- if (window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']) {
- child.value = window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']
- } else if (value.trim() + '') {
- deferredReplacement(value.trim() + '', function () {
- if (window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']) {
- _child.value = window?.Vue_Translation_Of_Text_Data?.[value.trim() + '']
- }
- })
- }
- } catch (error) {}
- }
- } else {
-
- this.processSelect(child)
- }
- })
- }
- },
- processAbsolutely(el) {
-
- if (el.nodeType === Node.ELEMENT_NODE) {
- Array.from(el.childNodes).forEach(child => {
- if (child.nodeType === Node.TEXT_NODE) {
- let text = child.textContent
- let _child = child
- if (text && !isNumberRegex(text.trim())) {
- try {
- if (window?.Vue_Translation_Of_Text_Data?.[text.trim()]) {
- child.textContent = window?.Vue_Translation_Of_Text_Data?.[text.trim()]
- } else if (text.trim()) {
- deferredReplacement(text.trim(), function () {
- if (window.Vue_Translation_Of_Text_Data[text.trim()]) {
- _child.textContent = window.Vue_Translation_Of_Text_Data[text.trim()]
- }
- })
- }
- } catch (error) {}
- }
- } else {
-
- this.processAbsolutely(child)
- }
- })
- }
- }
- }
- })
- }
- }
- export function translaBeforeRegistration(cb) {
- window.Vue_Translation_Of_Text_Type = window.localStorage.getItem('Vue_Translation_Of_Text_Type')
- if (window?.Vue_Translation_Of_Text_Type && ['en', 'ar', 'ru', 'ja'].includes(window?.Vue_Translation_Of_Text_Type)) {
- Vue.use(GlobalTextProcessor)
- axios
- .post(WindowsTranslateApi + '/api/v1/common/readTranslationOfText', {
- targetLanguage: window?.Vue_Translation_Of_Text_Type
- })
- .then(({ data }) => {
- var obj = {}
- var obj2 = {}
- data?.data?.map(item => {
- obj[item.source] = item.target
- obj2[item.target] = true
- })
- window.Vue_Translation_Of_Text_Old_Data = { ...(window.Vue_Translation_Of_Text_Old_Data || {}), ...obj2 }
- window.Vue_Translation_Of_Text_Data = { ...(window.Vue_Translation_Of_Text_Data || {}), ...obj }
- cb?.()
- })
- .catch(error => {
- cb?.()
- })
- } else {
- cb?.()
- }
- }
|