gt-sanity 4.0.30 → 4.0.31

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.
Files changed (46) hide show
  1. package/README.md +41 -0
  2. package/dist/index.d.ts +34 -1
  3. package/dist/index.js +713 -157
  4. package/dist/index.js.map +1 -1
  5. package/package.json +2 -1
  6. package/src/__tests__/allowNonDefaultSourceLocales.test.ts +28 -0
  7. package/src/adapter/__tests__/createTask.test.ts +72 -0
  8. package/src/adapter/__tests__/getLocales.test.ts +47 -0
  9. package/src/adapter/core.ts +11 -2
  10. package/src/adapter/createTask.ts +8 -1
  11. package/src/adapter/getLocales.ts +14 -2
  12. package/src/components/TranslationsProvider.tsx +350 -59
  13. package/src/components/__tests__/TranslationsProvider.test.ts +460 -0
  14. package/src/components/page/TranslationsTable.tsx +4 -2
  15. package/src/components/page/TranslationsTool.tsx +31 -2
  16. package/src/components/shared/LocaleDropdown.tsx +2 -4
  17. package/src/components/shared/LocaleSelector.tsx +5 -3
  18. package/src/components/tab/TranslationView.tsx +80 -26
  19. package/src/components/tab/__tests__/TranslationView.test.ts +252 -0
  20. package/src/configuration/baseDocumentLevelConfig/__tests__/documentLevelPatchNonDefaultSource.test.ts +230 -0
  21. package/src/configuration/baseDocumentLevelConfig/__tests__/exportForTranslation.test.ts +76 -0
  22. package/src/configuration/baseDocumentLevelConfig/documentLevelPatch.ts +34 -2
  23. package/src/configuration/baseDocumentLevelConfig/helpers/__tests__/createI18nDocAndPatchMetadata.test.ts +66 -0
  24. package/src/configuration/baseDocumentLevelConfig/helpers/__tests__/getOrCreateTranslationMetadata.test.ts +271 -0
  25. package/src/configuration/baseDocumentLevelConfig/helpers/createI18nDocAndPatchMetadata.ts +21 -0
  26. package/src/configuration/baseDocumentLevelConfig/helpers/getOrCreateTranslationMetadata.ts +65 -17
  27. package/src/configuration/baseDocumentLevelConfig/index.ts +17 -1
  28. package/src/hooks/__tests__/useDocumentRole.test.ts +270 -0
  29. package/src/hooks/useDocumentRole.ts +110 -0
  30. package/src/index.ts +23 -1
  31. package/src/sanity-api/__tests__/findDocuments.test.ts +68 -0
  32. package/src/sanity-api/__tests__/resolveRefs.test.ts +143 -0
  33. package/src/sanity-api/findDocuments.ts +18 -2
  34. package/src/sanity-api/resolveRefs.ts +93 -3
  35. package/src/translation/__tests__/bulkSources.test.ts +254 -0
  36. package/src/translation/__tests__/uploadFiles.test.ts +145 -0
  37. package/src/translation/bulkSources.ts +176 -0
  38. package/src/translation/uploadFiles.ts +13 -2
  39. package/src/types.ts +14 -1
  40. package/src/utils/__tests__/documentRoleState.test.ts +229 -0
  41. package/src/utils/__tests__/groqClient.ts +139 -0
  42. package/src/utils/__tests__/translationSource.test.ts +290 -0
  43. package/src/utils/documentRoleState.ts +152 -0
  44. package/src/utils/locales.ts +11 -0
  45. package/src/utils/translationSource.ts +319 -0
  46. package/src/components/shared/SingleDocumentView.tsx +0 -125
package/README.md CHANGED
@@ -158,6 +158,47 @@ enqueue any translation.
158
158
  To regenerate translations and deliberately discard existing ones for a single
159
159
  run, use **Retranslate from scratch** in the Translate All dialog.
160
160
 
161
+ ## Translating From Other Locales
162
+
163
+ By default only documents in `sourceLocale` can be translated. To translate a
164
+ document written in another locale — say, a page that only exists in French —
165
+ from the Translate dialog on that document, turn on
166
+ `allowNonDefaultSourceLocales`:
167
+
168
+ ```ts
169
+ gtPlugin({
170
+ // ...
171
+ allowNonDefaultSourceLocales: true, // off by default
172
+ });
173
+ ```
174
+
175
+ With it on, such a document's targets include `sourceLocale`.
176
+
177
+ Each translation group (a `translation.metadata` document) has exactly one
178
+ original, and every other document in it is a translation that cannot be
179
+ translated from:
180
+
181
+ - A document in no group is an original.
182
+ - In an existing group, the `sourceLocale` document is the original, as before.
183
+ - When gt-sanity first translates a document outside `sourceLocale`, it records
184
+ that document's language on the group as `gtSourceLanguage`, and that
185
+ document becomes the original.
186
+
187
+ A group with no original — no marker and no `sourceLocale` document, or a
188
+ marked original that was deleted — cannot be translated.
189
+
190
+ **Translate All** only covers originals in `sourceLocale`. It leaves out
191
+ originals in other locales and their translations, and reports how many of
192
+ those originals (already marked by an earlier translation) it skipped.
193
+ **Save Local Edits** only applies to groups whose original is in
194
+ `sourceLocale`, and field-level localization always translates from
195
+ `sourceLocale`.
196
+
197
+ Turning `allowNonDefaultSourceLocales` off again once groups have been marked is
198
+ not supported. gt-sanity then ignores the marker, as it did before the option
199
+ existed, so a `sourceLocale` translation of an original in another locale is
200
+ treated as an original and can be translated from again.
201
+
161
202
  ## Automatic Actions
162
203
 
163
204
  The Translate dialog on a document can refresh status, import a translation as
package/dist/index.d.ts CHANGED
@@ -30,7 +30,15 @@ import { useDocumentInternationalizationContext } from "@sanity/document-interna
30
30
  import { useDuplicateWithTranslationsAction } from "@sanity/document-internationalization";
31
31
 
32
32
  export declare interface Adapter {
33
- getLocales: (secrets: Secrets | null) => Promise<TranslationLocale[]>;
33
+ /**
34
+ * @param sourceLanguage - language of the document being translated, when
35
+ * known. An original outside the configured source locale is also offered
36
+ * the configured source locale as a target.
37
+ */
38
+ getLocales: (
39
+ secrets: Secrets | null,
40
+ sourceLanguage?: string,
41
+ ) => Promise<TranslationLocale[]>;
34
42
  getTranslationTask: (
35
43
  document: GTFile,
36
44
  secrets: Secrets | null,
@@ -231,6 +239,11 @@ declare type GTFile = {
231
239
  documentId: string;
232
240
  versionId?: string;
233
241
  fileName?: string;
242
+ /**
243
+ * Language the document is written in, when it is an original outside the
244
+ * configured source locale. Omitted means the configured source locale.
245
+ */
246
+ sourceLocale?: string;
234
247
  };
235
248
 
236
249
  /**
@@ -254,6 +267,26 @@ export declare type GTPluginConfig = Omit<
254
267
  > & {
255
268
  locales: string[];
256
269
  defaultLocale?: string;
270
+ /**
271
+ * Let documents written in a locale other than `sourceLocale` be translated
272
+ * from the Translate dialog on that document. Off by default: only
273
+ * `sourceLocale` documents can be translated.
274
+ *
275
+ * Each translation group (a `translation.metadata` document) has exactly one
276
+ * original: its `sourceLocale` document, or — once gt-sanity translates a
277
+ * document written in another locale — that document, recorded on the
278
+ * group's metadata as `gtSourceLanguage`. Every other document in a group is
279
+ * a translation and cannot be translated from. Translate All, Save Local
280
+ * Edits and field-level (internationalized-array) localization only
281
+ * translate from `sourceLocale`.
282
+ *
283
+ * Turning this off again after groups have been marked is not supported:
284
+ * translations of an original in another locale can then be treated as
285
+ * originals again.
286
+ *
287
+ * Defaults to false.
288
+ */
289
+ allowNonDefaultSourceLocales?: boolean;
257
290
  singletons?: string[];
258
291
  singletonMapping?: (sourceDocumentId: string, locale: string) => string;
259
292
  ignoreFields?: IgnoreFields[];