gt-sanity 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +1 -8
- package/README.md +5 -5
- package/dist/index.d.mts +35 -30
- package/dist/index.d.ts +35 -30
- package/dist/index.js +234 -123
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +237 -124
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -3
- package/src/adapter/core.ts +72 -7
- package/src/adapter/createTask.ts +1 -1
- package/src/adapter/types.ts +9 -0
- package/src/components/LanguageStatus.tsx +2 -0
- package/src/components/NewTask.tsx +2 -0
- package/src/components/ProgressBar.tsx +2 -0
- package/src/components/TaskView.tsx +2 -0
- package/src/components/TranslationContext.tsx +5 -0
- package/src/components/TranslationView.tsx +34 -2
- package/src/components/TranslationsTab.tsx +4 -0
- package/src/components/page/TranslationsTool.tsx +876 -0
- package/src/configuration/baseDocumentLevelConfig/documentLevelPatch.ts +23 -10
- package/src/configuration/baseDocumentLevelConfig/helpers/createI18nDocAndPatchMetadata.ts +77 -24
- package/src/configuration/baseDocumentLevelConfig/helpers/createTranslationMetadata.ts +2 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/getOrCreateTranslationMetadata.ts +2 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/getTranslationMetadata.ts +2 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/patchI18nDoc.ts +51 -8
- package/src/configuration/baseDocumentLevelConfig/index.ts +6 -37
- package/src/configuration/baseFieldLevelConfig.ts +4 -1
- package/src/configuration/utils/checkSerializationVersion.ts +2 -0
- package/src/configuration/utils/findDocumentAtRevision.ts +2 -0
- package/src/configuration/utils/findLatestDraft.ts +2 -0
- package/src/hooks/useClient.ts +3 -1
- package/src/hooks/useSecrets.ts +2 -0
- package/src/index.ts +70 -32
- package/src/translation/checkTranslationStatus.ts +42 -0
- package/src/translation/createJobs.ts +16 -0
- package/src/translation/downloadTranslations.ts +68 -0
- package/src/translation/importDocument.ts +24 -0
- package/src/translation/initProject.ts +61 -0
- package/src/translation/uploadFiles.ts +32 -0
- package/src/types.ts +4 -1
- package/src/utils/applyDocuments.ts +72 -0
- package/src/utils/serialize.ts +32 -0
- package/src/utils/shared.ts +1 -0
- package/src/configuration/baseDocumentLevelConfig/helpers/index.ts +0 -5
- package/src/configuration/baseDocumentLevelConfig/legacyDocumentLevelPatch.ts +0 -69
- package/src/configuration/index.ts +0 -18
- package/src/configuration/utils/index.ts +0 -3
package/dist/index.mjs
CHANGED
|
@@ -3,14 +3,76 @@ import o, { useDebugValue, createElement, useRef, useContext, useState, useEffec
|
|
|
3
3
|
import { useClient as useClient$1, useSchema, definePlugin } from "sanity";
|
|
4
4
|
import { randomKey } from "@sanity/util/content";
|
|
5
5
|
import { useToast, Stack, Text, Flex, Button, Grid, Select, Switch, Box, Card, Label, ThemeProvider, Spinner, Layer, ToastProvider } from "@sanity/ui";
|
|
6
|
+
import { GT } from "generaltranslation";
|
|
7
|
+
import { libraryDefaultLocale } from "generaltranslation/internal";
|
|
6
8
|
import { CheckmarkCircleIcon, DownloadIcon, ArrowTopRightIcon } from "@sanity/icons";
|
|
7
9
|
import { BaseDocumentMerger, BaseDocumentDeserializer, customBlockDeserializers, BaseDocumentSerializer, defaultStopTypes, customSerializers } from "sanity-naive-html-serializer";
|
|
8
10
|
import { BaseDocumentDeserializer as BaseDocumentDeserializer2, BaseDocumentMerger as BaseDocumentMerger2, BaseDocumentSerializer as BaseDocumentSerializer2, customSerializers as customSerializers2, defaultStopTypes as defaultStopTypes2 } from "sanity-naive-html-serializer";
|
|
9
|
-
import {
|
|
10
|
-
import
|
|
11
|
+
import { JSONPath } from "jsonpath-plus";
|
|
12
|
+
import JSONPointer from "jsonpointer";
|
|
11
13
|
const TranslationContext = o.createContext(
|
|
12
14
|
null
|
|
13
|
-
);
|
|
15
|
+
), SECRETS_NAMESPACE = "generaltranslation", gt = new GT();
|
|
16
|
+
function overrideConfig(secrets) {
|
|
17
|
+
gt.setConfig({
|
|
18
|
+
...secrets?.project && { projectId: secrets?.project },
|
|
19
|
+
...secrets?.secret && { apiKey: secrets?.secret }
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
class GTConfig {
|
|
23
|
+
secretsNamespace;
|
|
24
|
+
languageField;
|
|
25
|
+
sourceLocale;
|
|
26
|
+
locales;
|
|
27
|
+
singletons;
|
|
28
|
+
singletonMapping;
|
|
29
|
+
ignoreFields;
|
|
30
|
+
translateDocuments;
|
|
31
|
+
static instance;
|
|
32
|
+
constructor(secretsNamespace, languageField, sourceLocale, locales, singletons, singletonMapping, ignoreFields, translateDocuments) {
|
|
33
|
+
this.secretsNamespace = secretsNamespace, this.languageField = languageField, this.sourceLocale = sourceLocale, this.locales = locales, this.singletons = singletons, this.singletonMapping = singletonMapping, this.ignoreFields = ignoreFields, this.translateDocuments = translateDocuments;
|
|
34
|
+
}
|
|
35
|
+
static getInstance() {
|
|
36
|
+
return this.instance || (this.instance = new GTConfig(
|
|
37
|
+
SECRETS_NAMESPACE,
|
|
38
|
+
"language",
|
|
39
|
+
gt.sourceLocale || libraryDefaultLocale,
|
|
40
|
+
[],
|
|
41
|
+
[],
|
|
42
|
+
() => "",
|
|
43
|
+
[],
|
|
44
|
+
[]
|
|
45
|
+
)), this.instance;
|
|
46
|
+
}
|
|
47
|
+
init(secretsNamespace, languageField, sourceLocale, locales, singletons, singletonMapping, ignoreFields, translateDocuments) {
|
|
48
|
+
this.secretsNamespace = secretsNamespace, this.languageField = languageField, this.sourceLocale = sourceLocale, this.locales = locales, this.singletons = singletons, this.singletonMapping = singletonMapping, this.ignoreFields = ignoreFields, this.translateDocuments = translateDocuments;
|
|
49
|
+
}
|
|
50
|
+
getSecretsNamespace() {
|
|
51
|
+
return this.secretsNamespace;
|
|
52
|
+
}
|
|
53
|
+
getLanguageField() {
|
|
54
|
+
return this.languageField;
|
|
55
|
+
}
|
|
56
|
+
getSourceLocale() {
|
|
57
|
+
return this.sourceLocale;
|
|
58
|
+
}
|
|
59
|
+
getLocales() {
|
|
60
|
+
return this.locales;
|
|
61
|
+
}
|
|
62
|
+
getSingletons() {
|
|
63
|
+
return this.singletons;
|
|
64
|
+
}
|
|
65
|
+
getSingletonMapping() {
|
|
66
|
+
return this.singletonMapping;
|
|
67
|
+
}
|
|
68
|
+
getIgnoreFields() {
|
|
69
|
+
return this.ignoreFields;
|
|
70
|
+
}
|
|
71
|
+
getTranslateDocuments() {
|
|
72
|
+
return this.translateDocuments;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
const gtConfig = GTConfig.getInstance();
|
|
14
76
|
var __assign = function() {
|
|
15
77
|
return __assign = Object.assign || function(t) {
|
|
16
78
|
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
@@ -1552,7 +1614,7 @@ const LanguageStatus = ({
|
|
|
1552
1614
|
] }) })
|
|
1553
1615
|
] });
|
|
1554
1616
|
}, TranslationView = () => {
|
|
1555
|
-
const [locales, setLocales] = useState([]), [task, setTask] = useState(null), context = useContext(TranslationContext), toast = useToast();
|
|
1617
|
+
const [locales, setLocales] = useState([]), [task, setTask] = useState(null), context = useContext(TranslationContext), toast = useToast(), currentDocumentLanguage = useMemo(() => !context?.document || !context?.languageField ? null : context.document[context.languageField] || gtConfig.getSourceLocale(), [context?.document, context?.languageField]), shouldShowTranslationComponents = useMemo(() => currentDocumentLanguage ? currentDocumentLanguage === gtConfig.getSourceLocale() : !1, [currentDocumentLanguage]);
|
|
1556
1618
|
useEffect(() => {
|
|
1557
1619
|
async function fetchData() {
|
|
1558
1620
|
if (!context) {
|
|
@@ -1592,11 +1654,16 @@ const LanguageStatus = ({
|
|
|
1592
1654
|
);
|
|
1593
1655
|
task2 && setTask(task2);
|
|
1594
1656
|
}, [context, setTask]);
|
|
1595
|
-
return /* @__PURE__ */ jsxs(Stack, { space: 6, children: [
|
|
1657
|
+
return shouldShowTranslationComponents ? /* @__PURE__ */ jsxs(Stack, { space: 6, children: [
|
|
1596
1658
|
/* @__PURE__ */ jsx(NewTask, { locales, refreshTask }),
|
|
1597
1659
|
task && /* @__PURE__ */ jsx(TaskView, { task, locales, refreshTask })
|
|
1598
|
-
] })
|
|
1599
|
-
|
|
1660
|
+
] }) : /* @__PURE__ */ jsx(Card, { padding: 4, tone: "neutral", border: !0, children: /* @__PURE__ */ jsxs(Text, { size: 1, muted: !0, children: [
|
|
1661
|
+
"Translation tools are only available for",
|
|
1662
|
+
" ",
|
|
1663
|
+
/* @__PURE__ */ jsx("code", { children: gtConfig.getSourceLocale() }),
|
|
1664
|
+
" documents."
|
|
1665
|
+
] }) });
|
|
1666
|
+
}, useClient = () => useClient$1({ apiVersion: "2025-09-15" });
|
|
1600
1667
|
function useSecrets(id) {
|
|
1601
1668
|
const [loading, setLoading] = useState(!0), [secrets, setSecrets] = useState(null), client = useClient();
|
|
1602
1669
|
return useEffect(() => {
|
|
@@ -1671,6 +1738,8 @@ const TranslationTab = (props) => {
|
|
|
1671
1738
|
{
|
|
1672
1739
|
value: {
|
|
1673
1740
|
documentInfo: { documentId, versionId: revisionId },
|
|
1741
|
+
document: displayed,
|
|
1742
|
+
languageField: props.options.languageField || "language",
|
|
1674
1743
|
secrets,
|
|
1675
1744
|
importTranslation,
|
|
1676
1745
|
exportForTranslation,
|
|
@@ -1684,40 +1753,99 @@ const TranslationTab = (props) => {
|
|
|
1684
1753
|
}
|
|
1685
1754
|
)
|
|
1686
1755
|
] }) }) }) }) : /* @__PURE__ */ jsx(ThemeProvider, { children: /* @__PURE__ */ jsx(Box, { padding: 4, children: /* @__PURE__ */ jsx(Card, { tone: "caution", padding: [2, 3, 4, 4], shadow: 1, radius: 2, children: /* @__PURE__ */ jsx(Text, { children: "Can't find secrets for your translation service. Did you load them into this dataset?" }) }) }) });
|
|
1687
|
-
}, findDocumentAtRevision = async (documentId, rev, client) => {
|
|
1688
|
-
const baseUrl = `/data/history/${client.config().dataset}/documents/${documentId}?revision=${rev}`, url = client.getUrl(baseUrl);
|
|
1689
|
-
return await fetch(url, { credentials: "include" }).then((req) => req.json()).then((req) => req.documents && req.documents.length ? req.documents[0] : null);
|
|
1690
1756
|
}, findLatestDraft = (documentId, client) => {
|
|
1691
1757
|
const query = "*[_id == $id || _id == $draftId]", params = { id: documentId, draftId: `drafts.${documentId}` };
|
|
1692
1758
|
return client.fetch(query, params).then(
|
|
1693
1759
|
(docs) => docs.find((doc) => doc._id.startsWith("drafts.")) ?? docs[0]
|
|
1694
1760
|
);
|
|
1695
|
-
},
|
|
1761
|
+
}, findDocumentAtRevision = async (documentId, rev, client) => {
|
|
1762
|
+
const baseUrl = `/data/history/${client.config().dataset}/documents/${documentId}?revision=${rev}`, url = client.getUrl(baseUrl);
|
|
1763
|
+
return await fetch(url, { credentials: "include" }).then((req) => req.json()).then((req) => req.documents && req.documents.length ? req.documents[0] : null);
|
|
1764
|
+
};
|
|
1765
|
+
function applyDocuments(documentId, sourceDocument, targetDocument, ignore) {
|
|
1766
|
+
const ignoreFields = ignore.filter(
|
|
1767
|
+
(field) => field.documentId === documentId || field.documentId === void 0 || field.documentId === null
|
|
1768
|
+
), mergedDocument = { ...sourceDocument };
|
|
1769
|
+
for (const [key, value] of Object.entries(targetDocument))
|
|
1770
|
+
mergedDocument[key] = value;
|
|
1771
|
+
for (const ignoreField of ignoreFields)
|
|
1772
|
+
if (ignoreField.fields)
|
|
1773
|
+
for (const field of ignoreField.fields) {
|
|
1774
|
+
const { property, type } = field;
|
|
1775
|
+
try {
|
|
1776
|
+
const sourceResults = JSONPath({
|
|
1777
|
+
json: sourceDocument,
|
|
1778
|
+
path: property,
|
|
1779
|
+
resultType: "all",
|
|
1780
|
+
flatten: !0,
|
|
1781
|
+
wrap: !0
|
|
1782
|
+
});
|
|
1783
|
+
sourceResults && sourceResults.length > 0 && sourceResults.forEach((result) => {
|
|
1784
|
+
const sourceValue = result.value;
|
|
1785
|
+
type !== void 0 ? typeof sourceValue == "object" && sourceValue !== null && sourceValue._type === type && JSONPointer.set(mergedDocument, result.pointer, sourceValue) : JSONPointer.set(mergedDocument, result.pointer, sourceValue);
|
|
1786
|
+
});
|
|
1787
|
+
} catch (error) {
|
|
1788
|
+
console.warn(`Invalid JSONPath: ${property}`, error);
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
return mergedDocument;
|
|
1792
|
+
}
|
|
1793
|
+
async function createI18nDocAndPatchMetadata(sourceDocument, translatedDoc, localeId, client, translationMetadata, sourceDocumentId, languageField = "language", publish = !1) {
|
|
1696
1794
|
translatedDoc[languageField] = localeId;
|
|
1697
1795
|
const existingLocaleKey = translationMetadata.translations.find(
|
|
1698
1796
|
(translation) => translation._key === localeId
|
|
1699
|
-
), operation = existingLocaleKey ? "replace" : "after", location = existingLocaleKey ? `translations[_key == "${localeId}"]` : "translations[-1]", { _updatedAt, _createdAt, ...rest } = translatedDoc
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1706
|
-
|
|
1707
|
-
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1797
|
+
), operation = existingLocaleKey ? "replace" : "after", location = existingLocaleKey ? `translations[_key == "${localeId}"]` : "translations[-1]", { _updatedAt, _createdAt, ...rest } = translatedDoc, appliedDocument = applyDocuments(
|
|
1798
|
+
sourceDocumentId,
|
|
1799
|
+
sourceDocument,
|
|
1800
|
+
rest,
|
|
1801
|
+
gtConfig.getIgnoreFields()
|
|
1802
|
+
), isSingleton = gtConfig.getSingletons().includes(sourceDocumentId);
|
|
1803
|
+
let createDocumentPromise;
|
|
1804
|
+
if (isSingleton) {
|
|
1805
|
+
const translatedDocId = gtConfig.getSingletonMapping()(sourceDocumentId, localeId);
|
|
1806
|
+
createDocumentPromise = client.create({
|
|
1807
|
+
...appliedDocument,
|
|
1808
|
+
_type: rest._type,
|
|
1809
|
+
_id: `drafts.${translatedDocId}`
|
|
1810
|
+
});
|
|
1811
|
+
} else
|
|
1812
|
+
createDocumentPromise = client.create({
|
|
1813
|
+
...appliedDocument,
|
|
1814
|
+
_type: rest._type,
|
|
1815
|
+
_id: "drafts."
|
|
1816
|
+
});
|
|
1817
|
+
const doc = await createDocumentPromise, _ref = doc._id.replace("drafts.", "");
|
|
1818
|
+
if (await client.transaction().patch(
|
|
1819
|
+
translationMetadata._id,
|
|
1820
|
+
(p) => p.insert(operation, location, [
|
|
1821
|
+
{
|
|
1822
|
+
_key: localeId,
|
|
1823
|
+
_type: "internationalizedArrayReferenceValue",
|
|
1824
|
+
value: {
|
|
1825
|
+
_type: "reference",
|
|
1826
|
+
_ref,
|
|
1827
|
+
_weak: !0,
|
|
1828
|
+
_strengthenOnPublish: {
|
|
1829
|
+
type: doc._type
|
|
1715
1830
|
}
|
|
1716
1831
|
}
|
|
1717
|
-
|
|
1718
|
-
)
|
|
1719
|
-
|
|
1720
|
-
|
|
1832
|
+
}
|
|
1833
|
+
])
|
|
1834
|
+
).commit(), publish)
|
|
1835
|
+
try {
|
|
1836
|
+
doc._id.startsWith("drafts.") && await client.action(
|
|
1837
|
+
{
|
|
1838
|
+
actionType: "sanity.action.document.publish",
|
|
1839
|
+
draftId: doc._id,
|
|
1840
|
+
publishedId: doc._id.replace("drafts.", "")
|
|
1841
|
+
},
|
|
1842
|
+
{}
|
|
1843
|
+
);
|
|
1844
|
+
} catch (error) {
|
|
1845
|
+
console.error("Error publishing document", error);
|
|
1846
|
+
}
|
|
1847
|
+
}
|
|
1848
|
+
const getOrCreateTranslationMetadata = async (documentId, baseDocument, client, baseLanguage) => {
|
|
1721
1849
|
const existingMetadata = await client.fetch(
|
|
1722
1850
|
`*[
|
|
1723
1851
|
_type == 'translation.metadata' &&
|
|
@@ -1762,43 +1890,38 @@ const TranslationTab = (props) => {
|
|
|
1762
1890
|
return metadata;
|
|
1763
1891
|
throw error;
|
|
1764
1892
|
}
|
|
1765
|
-
},
|
|
1893
|
+
}, SYSTEM_FIELDS = ["_id", "_rev", "_updatedAt", "language"], isSystemField = (field) => SYSTEM_FIELDS.includes(field);
|
|
1894
|
+
async function patchI18nDoc(sourceDocumentId, i18nDocId, sourceDocument, mergedDocument, translatedFields, client, publish = !1) {
|
|
1766
1895
|
const cleanedMerge = {};
|
|
1767
1896
|
Object.entries(mergedDocument).forEach(([key, value]) => {
|
|
1768
1897
|
key in translatedFields && //don't overwrite any existing system values on the i18n doc
|
|
1769
|
-
!
|
|
1770
|
-
}), client.transaction().patch(i18nDocId, (p) => p.set(cleanedMerge)).commit();
|
|
1771
|
-
}, gt = new GT();
|
|
1772
|
-
function overrideConfig(secrets) {
|
|
1773
|
-
gt.setConfig({
|
|
1774
|
-
...secrets?.project && { projectId: secrets?.project },
|
|
1775
|
-
...secrets?.secret && { apiKey: secrets?.secret }
|
|
1898
|
+
!isSystemField(key) && (cleanedMerge[key] = value);
|
|
1776
1899
|
});
|
|
1900
|
+
const cleanedSourceDocument = {};
|
|
1901
|
+
Object.entries(sourceDocument).forEach(([key, value]) => {
|
|
1902
|
+
isSystemField(key) || (cleanedSourceDocument[key] = value);
|
|
1903
|
+
});
|
|
1904
|
+
const appliedDocument = applyDocuments(
|
|
1905
|
+
sourceDocumentId,
|
|
1906
|
+
cleanedSourceDocument,
|
|
1907
|
+
cleanedMerge,
|
|
1908
|
+
gtConfig.getIgnoreFields()
|
|
1909
|
+
), newDocument = await client.patch(i18nDocId, { set: appliedDocument }).commit();
|
|
1910
|
+
if (publish)
|
|
1911
|
+
try {
|
|
1912
|
+
newDocument._id.startsWith("drafts.") && await client.action(
|
|
1913
|
+
{
|
|
1914
|
+
actionType: "sanity.action.document.publish",
|
|
1915
|
+
draftId: newDocument._id,
|
|
1916
|
+
publishedId: newDocument._id.replace("drafts.", "")
|
|
1917
|
+
},
|
|
1918
|
+
{}
|
|
1919
|
+
);
|
|
1920
|
+
} catch (error) {
|
|
1921
|
+
console.error("Error publishing document", error);
|
|
1922
|
+
}
|
|
1777
1923
|
}
|
|
1778
|
-
|
|
1779
|
-
sourceLocale;
|
|
1780
|
-
locales;
|
|
1781
|
-
static instance;
|
|
1782
|
-
constructor(sourceLocale, locales) {
|
|
1783
|
-
this.sourceLocale = sourceLocale, this.locales = locales;
|
|
1784
|
-
}
|
|
1785
|
-
static getInstance() {
|
|
1786
|
-
return this.instance || (this.instance = new GTConfig(gt.sourceLocale || libraryDefaultLocale, [])), this.instance;
|
|
1787
|
-
}
|
|
1788
|
-
setSourceLocale(sourceLocale) {
|
|
1789
|
-
this.sourceLocale = sourceLocale;
|
|
1790
|
-
}
|
|
1791
|
-
getSourceLocale() {
|
|
1792
|
-
return this.sourceLocale;
|
|
1793
|
-
}
|
|
1794
|
-
setLocales(locales) {
|
|
1795
|
-
this.locales = locales;
|
|
1796
|
-
}
|
|
1797
|
-
getLocales() {
|
|
1798
|
-
return this.locales;
|
|
1799
|
-
}
|
|
1800
|
-
}
|
|
1801
|
-
const gtConfig = GTConfig.getInstance(), documentLevelPatch = async (docInfo, translatedFields, localeId, client, languageField = "language", mergeWithTargetLocale = !1) => {
|
|
1924
|
+
const documentLevelPatch = async (docInfo, translatedFields, localeId, client, languageField = "language", mergeWithTargetLocale = !1, publish = !1) => {
|
|
1802
1925
|
const baseLanguage = gtConfig.getSourceLocale();
|
|
1803
1926
|
let baseDoc = null, i18nDoc = null;
|
|
1804
1927
|
docInfo.documentId && docInfo.versionId && (baseDoc = await findDocumentAtRevision(
|
|
@@ -1821,31 +1944,24 @@ const gtConfig = GTConfig.getInstance(), documentLevelPatch = async (docInfo, tr
|
|
|
1821
1944
|
translatedFields,
|
|
1822
1945
|
baseDoc
|
|
1823
1946
|
);
|
|
1824
|
-
i18nDoc ? patchI18nDoc(
|
|
1947
|
+
i18nDoc ? await patchI18nDoc(
|
|
1948
|
+
docInfo.documentId,
|
|
1949
|
+
i18nDoc._id,
|
|
1950
|
+
baseDoc,
|
|
1951
|
+
merged,
|
|
1952
|
+
translatedFields,
|
|
1953
|
+
client,
|
|
1954
|
+
publish
|
|
1955
|
+
) : await createI18nDocAndPatchMetadata(
|
|
1956
|
+
baseDoc,
|
|
1825
1957
|
merged,
|
|
1826
1958
|
localeId,
|
|
1827
1959
|
client,
|
|
1828
1960
|
translationMetadata,
|
|
1829
|
-
languageField
|
|
1830
|
-
);
|
|
1831
|
-
}, legacyDocumentLevelPatch = async (docInfo, translatedFields, localeId, client) => {
|
|
1832
|
-
let baseDoc = null;
|
|
1833
|
-
docInfo.documentId && docInfo.versionId && (baseDoc = await findDocumentAtRevision(
|
|
1834
1961
|
docInfo.documentId,
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
)
|
|
1838
|
-
const merged = BaseDocumentMerger.documentLevelMerge(
|
|
1839
|
-
translatedFields,
|
|
1840
|
-
baseDoc
|
|
1841
|
-
), targetId = `drafts.${docInfo.documentId}__i18n_${localeId}`, i18nDoc = await findLatestDraft(targetId, client);
|
|
1842
|
-
if (i18nDoc) {
|
|
1843
|
-
const cleanedMerge = {};
|
|
1844
|
-
Object.entries(merged).forEach(([key, value]) => {
|
|
1845
|
-
Object.keys(translatedFields).includes(key) && !["_id", "_rev", "_updatedAt"].includes(key) && (cleanedMerge[key] = value);
|
|
1846
|
-
}), await client.transaction().patch(i18nDoc._id, (p) => p.set(cleanedMerge)).commit();
|
|
1847
|
-
} else
|
|
1848
|
-
merged._id = targetId, merged.__i18n_lang = localeId, await client.create(merged);
|
|
1962
|
+
languageField,
|
|
1963
|
+
publish
|
|
1964
|
+
);
|
|
1849
1965
|
}, baseDocumentLevelConfig = {
|
|
1850
1966
|
exportForTranslation: async (...params) => {
|
|
1851
1967
|
const [
|
|
@@ -1885,7 +2001,8 @@ const gtConfig = GTConfig.getInstance(), documentLevelPatch = async (docInfo, tr
|
|
|
1885
2001
|
context,
|
|
1886
2002
|
serializationOptions = {},
|
|
1887
2003
|
languageField = "language",
|
|
1888
|
-
mergeWithTargetLocale = !1
|
|
2004
|
+
mergeWithTargetLocale = !1,
|
|
2005
|
+
publish = !1
|
|
1889
2006
|
] = params, { client } = context, deserializers = {
|
|
1890
2007
|
types: {
|
|
1891
2008
|
...serializationOptions.additionalDeserializers ?? {}
|
|
@@ -1905,33 +2022,11 @@ const gtConfig = GTConfig.getInstance(), documentLevelPatch = async (docInfo, tr
|
|
|
1905
2022
|
localeId,
|
|
1906
2023
|
client,
|
|
1907
2024
|
languageField,
|
|
1908
|
-
mergeWithTargetLocale
|
|
2025
|
+
mergeWithTargetLocale,
|
|
2026
|
+
publish
|
|
1909
2027
|
);
|
|
1910
2028
|
},
|
|
1911
2029
|
secretsNamespace: "translationService"
|
|
1912
|
-
}, legacyDocumentLevelConfig$1 = {
|
|
1913
|
-
...baseDocumentLevelConfig,
|
|
1914
|
-
importTranslation: (...params) => {
|
|
1915
|
-
const [docInfo, localeId, document2, context, serializationOptions = {}] = params, { client } = context, deserializers = {
|
|
1916
|
-
types: {
|
|
1917
|
-
...serializationOptions.additionalDeserializers ?? {}
|
|
1918
|
-
}
|
|
1919
|
-
}, blockDeserializers = [
|
|
1920
|
-
...serializationOptions.additionalBlockDeserializers ?? [],
|
|
1921
|
-
...customBlockDeserializers
|
|
1922
|
-
], deserialized = BaseDocumentDeserializer.deserializeDocument(
|
|
1923
|
-
document2,
|
|
1924
|
-
deserializers,
|
|
1925
|
-
blockDeserializers
|
|
1926
|
-
);
|
|
1927
|
-
return legacyDocumentLevelPatch(
|
|
1928
|
-
docInfo,
|
|
1929
|
-
// versionId is not used here, since we just use the _rev id in the deserialized HTML itself
|
|
1930
|
-
deserialized,
|
|
1931
|
-
localeId,
|
|
1932
|
-
client
|
|
1933
|
-
);
|
|
1934
|
-
}
|
|
1935
2030
|
}, fieldLevelPatch = async (docInfo, translatedFields, localeId, client, mergeWithTargetLocale = !1) => {
|
|
1936
2031
|
let baseDoc;
|
|
1937
2032
|
const baseLanguage = gtConfig.getSourceLocale();
|
|
@@ -2031,7 +2126,7 @@ const gtConfig = GTConfig.getInstance(), documentLevelPatch = async (docInfo, tr
|
|
|
2031
2126
|
versionId: documentInfo.versionId || void 0,
|
|
2032
2127
|
locale: localeId
|
|
2033
2128
|
})) : "", createTask = async (documentInfo, serializedDocument, localeIds, secrets, workflowUid, callbackUrl) => {
|
|
2034
|
-
const fileName = `sanity
|
|
2129
|
+
const fileName = `sanity/${documentInfo.documentId}`;
|
|
2035
2130
|
overrideConfig(secrets);
|
|
2036
2131
|
const uploadResult = await gt.uploadSourceFiles(
|
|
2037
2132
|
[
|
|
@@ -2063,22 +2158,42 @@ const gtConfig = GTConfig.getInstance(), documentLevelPatch = async (docInfo, tr
|
|
|
2063
2158
|
...baseDocumentLevelConfig,
|
|
2064
2159
|
adapter: GTAdapter,
|
|
2065
2160
|
secretsNamespace: "generaltranslation"
|
|
2066
|
-
}, legacyDocumentLevelConfig = {
|
|
2067
|
-
...legacyDocumentLevelConfig$1,
|
|
2068
|
-
adapter: GTAdapter,
|
|
2069
|
-
secretsNamespace: "generaltranslation"
|
|
2070
2161
|
}, defaultFieldLevelConfig = {
|
|
2071
2162
|
...baseFieldLevelConfig,
|
|
2072
2163
|
adapter: GTAdapter,
|
|
2073
2164
|
secretsNamespace: "generaltranslation"
|
|
2074
|
-
}, gtPlugin = definePlugin(
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2165
|
+
}, gtPlugin = definePlugin(
|
|
2166
|
+
({
|
|
2167
|
+
languageField = "language",
|
|
2168
|
+
sourceLocale = libraryDefaultLocale,
|
|
2169
|
+
locales,
|
|
2170
|
+
customMapping,
|
|
2171
|
+
apiKey,
|
|
2172
|
+
projectId,
|
|
2173
|
+
singletons,
|
|
2174
|
+
singletonMapping,
|
|
2175
|
+
ignoreFields,
|
|
2176
|
+
translateDocuments,
|
|
2177
|
+
secretsNamespace = SECRETS_NAMESPACE
|
|
2178
|
+
}) => (translateDocuments = translateDocuments?.filter((filter2) => !!(filter2.documentId || filter2.type)), gtConfig.init(
|
|
2179
|
+
secretsNamespace,
|
|
2180
|
+
languageField,
|
|
2181
|
+
sourceLocale,
|
|
2182
|
+
locales,
|
|
2183
|
+
singletons || [],
|
|
2184
|
+
// singletons is a string array of singleton document ids
|
|
2185
|
+
singletonMapping || ((sourceDocumentId, locale) => `${sourceDocumentId}-${locale}`),
|
|
2186
|
+
ignoreFields || [],
|
|
2187
|
+
translateDocuments || []
|
|
2188
|
+
), gt.setConfig({
|
|
2189
|
+
sourceLocale,
|
|
2190
|
+
customMapping,
|
|
2191
|
+
apiKey,
|
|
2192
|
+
projectId
|
|
2193
|
+
}), {
|
|
2194
|
+
name: "gt-sanity"
|
|
2195
|
+
})
|
|
2196
|
+
);
|
|
2082
2197
|
export {
|
|
2083
2198
|
BaseDocumentDeserializer2 as BaseDocumentDeserializer,
|
|
2084
2199
|
BaseDocumentMerger2 as BaseDocumentMerger,
|
|
@@ -2092,8 +2207,6 @@ export {
|
|
|
2092
2207
|
documentLevelPatch,
|
|
2093
2208
|
fieldLevelPatch,
|
|
2094
2209
|
findLatestDraft,
|
|
2095
|
-
gtPlugin
|
|
2096
|
-
legacyDocumentLevelConfig,
|
|
2097
|
-
legacyDocumentLevelPatch
|
|
2210
|
+
gtPlugin
|
|
2098
2211
|
};
|
|
2099
2212
|
//# sourceMappingURL=index.mjs.map
|