Skip to content

Commit

Permalink
chore: update version of translations tab and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
cngonzalez committed Sep 13, 2023
1 parent 6e25342 commit 5665fe0
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 34 deletions.
46 changes: 46 additions & 0 deletions docs/00-faq.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
## Where should I place the Translations Tab?
The Translations Tab should be invoked as a view. This can be done in the `getDefaultDocumentNode` function in `schemas.ts`:
```ts
import {TranslationsTab, defaultDocumentLevelConfig, defaultFieldLevelConfig} from 'sanity-plugin-studio-smartling'

export const defaultDocumentNodeResolver = (S) =>
S.document().views([
// Give all documents the JSON preview,
// as well as the default form view
S.view.form(),
S.view.component(TranslationsTab).options(defaultDocumentLevelConfig)
])
```
If using a document-level translation pattern, you should likely only include this view in your "base" language. Please see the document internationalization plugin on building different desk structure options for different locales. (example code coming soon)

## What happens when I click the "Create Job" button?
The tab will do the following:
1. Fetch the latest draft of the document you're currently in.
2. Filter out (as best as it can) any fields that should not be translated (references, dates, numbers, etc.). It will also utilize options passed in to ignore certain fields and objects. See more in the Advanced Configuration docs.
3. Serialize the document into an HTML string. It will utilize options to serialize objects in particular ways, if provided.
4. Send the HTML string to the translation vendor's API, along with the locale code of the language(s) you want to translate to.
5. Look up the translation job ID in the response from the translation vendor (this will either match your document ID or be invoked by a custom job name resolver [coming soon]).
6. Show you the progress of the ongoing translation and a link to the job in the vendor, if available.

## How am I seeing my progress?
On load, the tab fetches the total amount of strings that have reached the LAST stage of translation and are ready to be imported. That is divided over the total amount of strings in the document, and the progress bar is updated accordingly.

## How do I import translations?
When the translation vendor has completed the translation, you can click the "Import Translations" button. This will do the following:
1. Deserialize the HTML string from the translation vendor into a Sanity document.
2. Fetch the revision of the document you're currently in at the time the translation was sent, if available. (This is to resolve the translation as smoothly as possible, in case the document has changed since it was sent to translation and cannot resolve conflicts)
3. Compare the two documents and merge the translated content with anything that was not sent for translation.
4. Issue a patch with the relevant merges to the relevant document. If using document internationalization, this will also update translation metadata.

## How can I prevent certain fields from being sent to translation?
You can pass in a `stopTypes` parameter to name all objects you do not want translated. Alternately, the serializer also introspects your schema. You can set `localize: false` on any field you do not want translated.
```js
fields: [
defineField({
name: 'categories',
type: 'array',
//ts-ignore
localize: false,
...
})]
```
60 changes: 60 additions & 0 deletions docs/01-advanced-configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Advanced configuration

This plugin provides defaults for most configuration options, but they can be overridden as needed. Please refer to the types to see what you should declare, but we also provide the type for all options, which we recommend using for quicker development.

```typescript
//sanity.config.ts
import {TranslationsTab, defaultDocumentLevelConfig, defaultFieldLevelConfig, TranslationsTabConfigOptions} from 'sanity-plugin-studio-smartling'

const customConfig = {
...defaultDocumentLevelConfig,
//baseLanguage is `en` by default, overwrite as needed. This is important for coalescing translations and creating accurate translation metadata.
baseLanguage: 'en_US',
//this is the field that will be used to determine the language of the document. It is `language` by default.
languageField: 'locale',
serializationOptions: {
//use this option to exclude objects that do not need to be translated. The plugin already uses defaults for references, dates, numbers, etc.
additionalStopTypes: ['colorTheme'],
//use this option to serialize objects in a particular way. The plugin will try to filter and serialize objects as best as it can, but you can override this behavior here.
//For now, you should also include these for annotations and inline objects in PortableText, if you want them to be translated.
//NOTE: it is VERY important to include the _type as a class and the _key as id for accurately deserializing and coalescing
additionalSerializers: {
testObject: ({value}) => {
return `<span class="${value._type}" id="${value._key}">${value.title}</span>`
}
},
//Create a method to deserialize any custom serialization rules
additonalDeserializers: {
testObject: ({node}) => {
return {
_type: 'testObject',
_key: node.id,
title: node.textContent
}
}
},
//Block text requires a special deserialization format based on @sanity/block-tools. Use this option for any annotations or inline objects that need to be translated.
additionalBlockDeserializers: [
{
deserialize(el, next): TypedObject | undefined {
if (!el.className || el.className?.toLowerCase() !== 'myannotation') {
return undefined
}

const markDef = {
_key: el.id,
_type: 'myAnnotation',
}

return {
_type: '__annotation',
markDef: markDef,
children: next(el.childNodes),
}
},
},
]
}
//adapter, baseLanguage, secretsNamespace, importTranslation, exportForTranslation should likely not be touched unless you very much want to customize your plugin.
} satisfies TranslationsTabConfigOptions
```
77 changes: 68 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sanity-plugin-studio-smartling",
"version": "4.0.3",
"version": "4.1.0",
"description": "!smartling gif",
"keywords": [
"sanity",
Expand Down Expand Up @@ -48,7 +48,7 @@
},
"dependencies": {
"@sanity/incompatible-plugin": "^1.0.4",
"sanity-translations-tab": "^4.0.1"
"sanity-translations-tab": "^4.1.0"
},
"devDependencies": {
"@commitlint/cli": "^17.7.1",
Expand Down
8 changes: 4 additions & 4 deletions src/adapter/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ export const findExistingJob = async (
const correctJob = items
.filter((item: {jobStatus: string}) => item.jobStatus !== 'DELETED')
.find(
(item: {jobName: string; referenceNumber: string}) =>
(item.jobName && item.jobName === documentId) ||
(item.referenceNumber && item.referenceNumber === documentId),
)
(item: {jobName: string; referenceNumber: string}) =>
(item.jobName && item.jobName === documentId) ||
(item.referenceNumber && item.referenceNumber === documentId),
)

if (correctJob) {
return correctJob.translationJobUid
Expand Down
25 changes: 6 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {SerializedDocument} from 'sanity-naive-html-serializer'
import {
TranslationsTab,
baseDocumentLevelConfig,
Expand All @@ -10,39 +9,25 @@ import {
BaseDocumentMerger,
defaultStopTypes,
customSerializers,
Adapter,
legacyDocumentLevelPatch,
documentLevelPatch,
fieldLevelPatch,
TranslationFunctionContext,
TranslationsTabConfigOptions,
} from 'sanity-translations-tab'
import {SmartlingAdapter} from './adapter'

interface ConfigOptions {
adapter: Adapter
secretsNamespace: string | null
exportForTranslation: (
id: string,
context: TranslationFunctionContext,
) => Promise<SerializedDocument>
importTranslation: (
id: string,
localeId: string,
doc: string,
context: TranslationFunctionContext,
) => Promise<void>
}
const defaultDocumentLevelConfig: ConfigOptions = {
const defaultDocumentLevelConfig: TranslationsTabConfigOptions = {
...baseDocumentLevelConfig,
adapter: SmartlingAdapter,
}

const legacyDocumentLevelConfig: ConfigOptions = {
const legacyDocumentLevelConfig: TranslationsTabConfigOptions = {
...baseLegacyDocumentLevelConfig,
adapter: SmartlingAdapter,
}

const defaultFieldLevelConfig: ConfigOptions = {
const defaultFieldLevelConfig: TranslationsTabConfigOptions = {
...baseFieldLevelConfig,
adapter: SmartlingAdapter,
}
Expand All @@ -63,3 +48,5 @@ export {
defaultDocumentLevelConfig,
defaultFieldLevelConfig,
}

export type {TranslationFunctionContext, TranslationsTabConfigOptions}

0 comments on commit 5665fe0

Please sign in to comment.