UNPKG

strapi-to-lokalise-plugin

Version:

Preview and sync Lokalise translations from Strapi admin

71 lines (56 loc) 1.71 kB
'use strict'; const STORE_KEY = 'entrySlugMap'; const getStore = () => strapi.store({ type: 'plugin', name: 'lokalise-sync', }); module.exports = { async getAll() { const store = getStore(); const value = await store.get({ key: STORE_KEY }); return value || {}; }, async setAll(value = {}) { const store = getStore(); await store.set({ key: STORE_KEY, value }); return value; }, async update(entries = []) { if (!Array.isArray(entries) || entries.length === 0) { return this.getAll(); } const current = await this.getAll(); let changed = false; entries.forEach((entry) => { if (!entry || typeof entry !== 'object') return; const { type, entryId, slug } = entry; if (!type || typeof type !== 'string') return; if (entryId === undefined || entryId === null) return; const typeKey = type.trim(); const idKey = String(entryId); if (!current[typeKey]) { current[typeKey] = {}; } if (slug === null || slug === undefined || slug === '') { if (current[typeKey] && current[typeKey][idKey]) { delete current[typeKey][idKey]; changed = true; if (Object.keys(current[typeKey]).length === 0) { delete current[typeKey]; } } } else { const normalizedSlug = String(slug).trim(); if (current[typeKey]?.[idKey] !== normalizedSlug) { current[typeKey][idKey] = normalizedSlug; changed = true; } } }); if (changed) { await this.setAll(current); } return current; }, };