gt-sanity 2.0.2 → 2.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "gt-sanity",
3
- "version": "2.0.2",
3
+ "version": "2.0.4",
4
4
  "description": "General Translation integration with Sanity",
5
5
  "keywords": [
6
6
  "sanity",
@@ -50,8 +50,9 @@
50
50
  "@sanity/util": "^5.18.0",
51
51
  "jsonpath-plus": "^10.3.0",
52
52
  "jsonpointer": "^5.0.1",
53
+ "@sanity/document-internationalization": "^6.0.6",
53
54
  "lodash.merge": "^4.6.2",
54
- "generaltranslation": "8.2.2"
55
+ "generaltranslation": "8.2.3"
55
56
  },
56
57
  "devDependencies": {
57
58
  "@portabletext/types": "^2.0.15",
@@ -0,0 +1,14 @@
1
+ export {
2
+ documentInternationalization,
3
+ useDocumentInternationalizationContext,
4
+ DocumentInternationalizationMenu,
5
+ useDeleteTranslationAction,
6
+ useDuplicateWithTranslationsAction,
7
+ } from '@sanity/document-internationalization';
8
+
9
+ export type {
10
+ PluginConfig as DocumentInternationalizationConfig,
11
+ Language,
12
+ Metadata,
13
+ TranslationReference,
14
+ } from '@sanity/document-internationalization';
package/src/index.ts CHANGED
@@ -1,3 +1,17 @@
1
+ export {
2
+ documentInternationalization,
3
+ useDocumentInternationalizationContext,
4
+ DocumentInternationalizationMenu,
5
+ useDeleteTranslationAction,
6
+ useDuplicateWithTranslationsAction,
7
+ } from './documentInternationalization';
8
+ export type {
9
+ DocumentInternationalizationConfig,
10
+ Language,
11
+ Metadata,
12
+ TranslationReference,
13
+ } from './documentInternationalization';
14
+
1
15
  import TranslationsTab from './components/tab/TranslationsTab';
2
16
  import {
3
17
  Secrets,
@@ -45,6 +59,7 @@ import { definePlugin } from 'sanity';
45
59
  import { route } from 'sanity/router';
46
60
  import { gt, pluginConfig } from './adapter/core';
47
61
  import { libraryDefaultLocale } from 'generaltranslation/internal';
62
+ import { getLocaleProperties } from 'generaltranslation';
48
63
  import type {
49
64
  IgnoreFields,
50
65
  SkipFields,
@@ -54,6 +69,7 @@ import TranslationsTool from './components/page/TranslationsTool';
54
69
  import { SECRETS_NAMESPACE } from './utils/shared';
55
70
  import type { PortableTextHtmlComponents } from '@portabletext/to-html';
56
71
  import { attachGTData, detachGTData } from './serialization/data';
72
+ import { documentInternationalization } from './documentInternationalization';
57
73
 
58
74
  export type GTPluginConfig = Omit<
59
75
  Parameters<typeof gt.setConfig>[0],
@@ -69,12 +85,16 @@ export type GTPluginConfig = Omit<
69
85
  ignoreFields?: IgnoreFields[];
70
86
  skipFields?: SkipFields[];
71
87
  languageField?: string;
72
- translateDocuments?: TranslateDocumentFilter[];
88
+ translateDocuments?: TranslateDocumentFilter[] | string[];
73
89
  secretsNamespace?: string;
74
90
  additionalStopTypes?: string[];
75
91
  additionalSerializers?: Partial<PortableTextHtmlComponents>;
76
92
  additionalDeserializers?: Record<string, any>;
77
93
  additionalBlockDeserializers?: any[];
94
+ // When true (default), automatically adds the @sanity/document-internationalization plugin
95
+ // with language badges, translation menu, and per-language templates.
96
+ // Requires translateDocuments to specify which document types to enable translations for.
97
+ showDocumentInternationalization?: boolean;
78
98
  };
79
99
 
80
100
  /**
@@ -109,18 +129,19 @@ export const gtPlugin = definePlugin<GTPluginConfig>(
109
129
  additionalSerializers = {},
110
130
  additionalDeserializers = {},
111
131
  additionalBlockDeserializers = [],
132
+ showDocumentInternationalization = true,
112
133
  }) => {
113
134
  // Resolve sourceLocale: explicit sourceLocale > defaultLocale (from gt.config.json) > library default
114
135
  const resolvedSourceLocale =
115
136
  sourceLocale ?? defaultLocale ?? libraryDefaultLocale;
116
137
 
117
- // Validate the translateDocuments
118
- translateDocuments = translateDocuments?.filter((filter) => {
119
- if (filter.documentId || filter.type) {
120
- return true;
121
- }
122
- return false;
123
- });
138
+ // Normalize translateDocuments: string[] → TranslateDocumentFilter[]
139
+ let normalizedTranslateDocuments: TranslateDocumentFilter[] | undefined;
140
+ if (translateDocuments) {
141
+ normalizedTranslateDocuments = translateDocuments
142
+ .map((entry) => (typeof entry === 'string' ? { type: entry } : entry))
143
+ .filter((filter) => filter.documentId || filter.type);
144
+ }
124
145
 
125
146
  pluginConfig.init(
126
147
  secretsNamespace,
@@ -133,7 +154,7 @@ export const gtPlugin = definePlugin<GTPluginConfig>(
133
154
  ((sourceDocumentId, locale) => `${sourceDocumentId}-${locale}`),
134
155
  ignoreFields || [],
135
156
  skipFields || [],
136
- translateDocuments || [],
157
+ normalizedTranslateDocuments || [],
137
158
  additionalStopTypes,
138
159
  additionalSerializers,
139
160
  additionalDeserializers,
@@ -145,8 +166,33 @@ export const gtPlugin = definePlugin<GTPluginConfig>(
145
166
  apiKey: apiKey,
146
167
  projectId: projectId,
147
168
  });
169
+
170
+ // Auto-add document internationalization plugin
171
+ const plugins = [];
172
+ if (showDocumentInternationalization) {
173
+ const schemaTypes =
174
+ normalizedTranslateDocuments
175
+ ?.map((filter) => filter.type)
176
+ .filter((type): type is string => !!type) ?? [];
177
+ if (schemaTypes.length > 0) {
178
+ const allLocales = [resolvedSourceLocale, ...locales];
179
+ const supportedLanguages = allLocales.map((locale) => {
180
+ const props = getLocaleProperties(locale, resolvedSourceLocale);
181
+ return { id: locale, title: props.name };
182
+ });
183
+ plugins.push(
184
+ documentInternationalization({
185
+ supportedLanguages,
186
+ schemaTypes,
187
+ languageField,
188
+ })
189
+ );
190
+ }
191
+ }
192
+
148
193
  return {
149
194
  name: 'gt-sanity',
195
+ plugins,
150
196
  tools: [
151
197
  {
152
198
  name: 'translations',