Merge branch 'front-styles' of ssh://git.ma-al.com:8822/goc_daniel/b2b into translate
This commit is contained in:
@@ -45,6 +45,7 @@ export const useProductStore = defineStore('product', () => {
|
||||
}
|
||||
}
|
||||
|
||||
const translat = ref()
|
||||
const settingStore = useSettingsStore()
|
||||
async function translateProductDescription(productID: number, toLangId: number, model: string = 'Google') {
|
||||
loading.value = true
|
||||
@@ -53,6 +54,7 @@ export const useProductStore = defineStore('product', () => {
|
||||
try {
|
||||
const response = await useFetchJson<ProductDescription>(`/api/v1/restricted/product-translation/translate-product-description?productID=${productID}&productFromLangID=${settingStore.shopDefaultLanguage}&productToLangID=${toLangId}&model=${model}`)
|
||||
productDescription.value = response.items
|
||||
saveProductDescription(productID, toLangId)
|
||||
return response.items
|
||||
} catch (e: any) {
|
||||
error.value = e?.message || 'Failed to translate product description'
|
||||
@@ -62,16 +64,46 @@ export const useProductStore = defineStore('product', () => {
|
||||
}
|
||||
}
|
||||
|
||||
function fixHtml(html: string) {
|
||||
return html
|
||||
// 1. fix img
|
||||
.replace(/<img([^>]*?)>/g, '<img$1 />')
|
||||
|
||||
function stripHtml(html: string) {
|
||||
const div = document.createElement('div')
|
||||
div.innerHTML = html
|
||||
return div.textContent || div.innerText || ''
|
||||
// 2. escape text only
|
||||
.replace(/>([^<]+)</g, (match, text) => {
|
||||
const escaped = text
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''')
|
||||
|
||||
return `>${escaped}<`
|
||||
})
|
||||
}
|
||||
|
||||
async function saveProductDescription(productID?: number, langId?: number) {
|
||||
function fixAll(obj: any): any {
|
||||
if (typeof obj === 'string') {
|
||||
return fixHtml(obj)
|
||||
}
|
||||
|
||||
if (Array.isArray(obj)) {
|
||||
return obj.map(fixAll)
|
||||
}
|
||||
|
||||
if (typeof obj === 'object' && obj !== null) {
|
||||
const result: any = {}
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
result[key] = fixAll(value)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
async function saveProductDescription(productID?: number, langId?: number | null) {
|
||||
const id = productID || 1
|
||||
const lang = langId || 1
|
||||
|
||||
try {
|
||||
const data = await useFetchJson(
|
||||
`/api/v1/restricted/product-translation/save-product-description?productID=${id}&productLangID=${lang}`,
|
||||
@@ -81,14 +113,14 @@ export const useProductStore = defineStore('product', () => {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: stripHtml(productDescription.value?.name || ''),
|
||||
description: stripHtml(productDescription.value?.description || ''),
|
||||
description_short: stripHtml(productDescription.value?.description_short || ''),
|
||||
meta_title: stripHtml(productDescription.value?.meta_title || ''),
|
||||
meta_description: stripHtml(productDescription.value?.meta_description || ''),
|
||||
available_now: stripHtml(productDescription.value?.available_now || ''),
|
||||
available_later: stripHtml(productDescription.value?.available_later || ''),
|
||||
usage: stripHtml(productDescription.value?.usage || ''),
|
||||
name: productDescription.value?.name || '',
|
||||
description: productDescription.value?.description || '',
|
||||
description_short: productDescription.value?.description_short || '',
|
||||
meta_title: productDescription.value?.meta_title || '',
|
||||
meta_description: productDescription.value?.meta_description || '',
|
||||
available_now: productDescription.value?.available_now || '',
|
||||
available_later: productDescription.value?.available_later || '',
|
||||
usage: productDescription.value?.usage || '',
|
||||
})
|
||||
}
|
||||
)
|
||||
@@ -102,6 +134,7 @@ export const useProductStore = defineStore('product', () => {
|
||||
productDescription,
|
||||
loading,
|
||||
error,
|
||||
translat,
|
||||
translateProductDescription,
|
||||
getProductDescription,
|
||||
saveProductDescription
|
||||
|
||||
Reference in New Issue
Block a user