@braze/sanity-plugin-ai-translation 0.2.75 → 0.2.76
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/dist/index.js +18 -17
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -870,14 +870,11 @@ const fetchTranslationPrompts = async ({
|
|
|
870
870
|
languageFieldName
|
|
871
871
|
}) => {
|
|
872
872
|
try {
|
|
873
|
-
const doc = await retryWithBackoff(
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
context: { operation: "localizeReferences.fetchDocument", documentId }
|
|
879
|
-
}
|
|
880
|
-
);
|
|
873
|
+
const doc = await retryWithBackoff(() => client.fetch("*[_id == $documentId][0]", { documentId }), {
|
|
874
|
+
maxAttempts: 3,
|
|
875
|
+
initialDelayMs: 1e3,
|
|
876
|
+
context: { operation: "localizeReferences.fetchDocument", documentId }
|
|
877
|
+
});
|
|
881
878
|
if (!doc) {
|
|
882
879
|
devLog.error("Document not found", new Error("Document not found"), {
|
|
883
880
|
documentId,
|
|
@@ -904,20 +901,19 @@ const fetchTranslationPrompts = async ({
|
|
|
904
901
|
component: "localizeReferences"
|
|
905
902
|
});
|
|
906
903
|
}
|
|
907
|
-
}, updateReferences = async (documentId, client, refs,
|
|
904
|
+
}, updateReferences = async (documentId, client, refs, _sourceLanguage, targetLanguage) => {
|
|
908
905
|
if (!refs?.length)
|
|
909
906
|
return [];
|
|
910
907
|
const refIds = refs.map((r) => getPublishedId(r.value)), translations = await retryWithBackoff(
|
|
911
908
|
() => client.fetch(
|
|
912
909
|
`*[_type == $translationMetadataSchemaType && array::intersects(translations[].value._ref, $refIds)] {
|
|
913
|
-
"
|
|
910
|
+
"allSourceRefs": translations[_key != $targetLanguage][].value._ref,
|
|
914
911
|
"targetId": translations[_key == $targetLanguage][0].value._ref,
|
|
915
|
-
"targetType": translations[_key == $
|
|
912
|
+
"targetType": translations[_key == $targetLanguage][0].value -> _type
|
|
916
913
|
}`,
|
|
917
914
|
{
|
|
918
915
|
translationMetadataSchemaType: TRANSLATION_METADATA_TYPE,
|
|
919
916
|
refIds,
|
|
920
|
-
sourceLanguage,
|
|
921
917
|
targetLanguage
|
|
922
918
|
}
|
|
923
919
|
),
|
|
@@ -928,16 +924,21 @@ const fetchTranslationPrompts = async ({
|
|
|
928
924
|
}
|
|
929
925
|
), translationMap = /* @__PURE__ */ new Map();
|
|
930
926
|
for (const t of translations)
|
|
931
|
-
|
|
927
|
+
if (t.targetId)
|
|
928
|
+
for (const sourceRef of t.allSourceRefs ?? [])
|
|
929
|
+
sourceRef && translationMap.set(sourceRef, { targetId: t.targetId, targetType: t.targetType });
|
|
932
930
|
const patch = client.patch(documentId);
|
|
933
931
|
let hasChanges = !1;
|
|
934
932
|
for (const ref of refs) {
|
|
935
|
-
const sourceId = getPublishedId(ref.value),
|
|
936
|
-
|
|
933
|
+
const sourceId = getPublishedId(ref.value), match = translationMap.get(sourceId);
|
|
934
|
+
if (!match)
|
|
935
|
+
continue;
|
|
936
|
+
const newId = getPublishedId(match.targetId);
|
|
937
|
+
patch.set({ [pathToString(ref.path)]: newId }), patch.set({
|
|
937
938
|
[`${pathToString(ref.path.slice(0, -1))}._strengthenOnPublish`]: {
|
|
938
|
-
type:
|
|
939
|
+
type: match.targetType || void 0
|
|
939
940
|
}
|
|
940
|
-
}), hasChanges = !0
|
|
941
|
+
}), hasChanges = !0;
|
|
941
942
|
}
|
|
942
943
|
hasChanges && (await retryWithBackoff(
|
|
943
944
|
async () => {
|