Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow specifying a translation style guide #61

Merged
merged 3 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
- [What it solves](#what-ai-assist-field-level-translations-solves)
- [Configure](#configure-field-translations)
- [Adding translation actions to fields](#adding-translation-actions-to-fields)
- [Translation style guide](#translation-style-guide)
- [License](#license)
- [Develop \& test](#develop--test)
- [Release new version](#release-new-version)
Expand Down Expand Up @@ -817,6 +818,28 @@ defineField({
})
```

## Translation style guide

In some cases you might want/need the translator to follow a certain style guide - for
instance you might tell it not to translate certain words, or be more formal or casual.
To configure this you can pass a `styleguide` property under the translation
configuration:

```ts
assist({
translate: {
styleguide: `Be extremely formal and precise. Translate as if you are Spock from Star Trek.`,
},
})
```

The style guide is currently limited to 2000 characters, and the translation might get
slower the longer your style guide is. If the provided string is longer than the limit,
the plugin will throw upon studio startup.

Note that this is currently only available on a global level - it can not be defined
per-field for now.

## Caveats

Large Language Models (LLMs) are a new technology. Constraints and limitations are still being explored,
Expand Down
8 changes: 8 additions & 0 deletions plugin/src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ export interface AssistPluginConfig {

export const assist = definePlugin<AssistPluginConfig | void>((config) => {
const configWithDefaults = config ?? {}
const styleguide = configWithDefaults.translate?.styleguide || ''

if (styleguide.length > 2000) {
throw new Error(
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`,
)
}

return {
name: packageName,

Expand Down
5 changes: 4 additions & 1 deletion plugin/src/translate/FieldTranslationProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function hasValuesToTranslate(
export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
const {config: assistConfig} = useAiAssistanceConfig()
const apiClient = useApiClient(assistConfig.__customApiClient)
const styleguide = assistConfig.translate?.styleguide
const config = assistConfig.translate?.field
const {translate: runTranslate} = useTranslate(apiClient)

Expand Down Expand Up @@ -193,7 +194,8 @@ export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
if (fieldLanguageMaps && documentId && translatePath) {
runTranslate({
documentId,
translatePath: translatePath,
translatePath,
styleguide,
fieldLanguageMap: fieldLanguageMaps.map((map) => ({
...map,
// eslint-disable-next-line max-nested-callbacks
Expand All @@ -207,6 +209,7 @@ export function FieldTranslationProvider(props: PropsWithChildren<{}>) {
fieldLanguageMaps,
documentId,
runTranslate,
styleguide,
close,
toLanguages,
fieldTranslationParams?.translatePath,
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/translate/translateActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export const translateActions: DocumentFieldAction = {
task: translationApi.translate,
})

const styleguide = config.translate?.styleguide
const languagePath = config.translate?.document?.languageField

// if this is true, it is stable, and not breaking rules of hooks
Expand All @@ -98,6 +99,7 @@ export const translateActions: DocumentFieldAction = {
translate({
languagePath,
translatePath: path,
styleguide,
documentId: documentId ?? '',
conditionalMembers: formStateRef.current
? getConditionalMembers(formStateRef.current)
Expand All @@ -110,6 +112,7 @@ export const translateActions: DocumentFieldAction = {
}, [
languagePath,
translate,
styleguide,
documentId,
translationApi.loading,
documentTranslationEnabled,
Expand Down
6 changes: 6 additions & 0 deletions plugin/src/translate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,10 @@ export interface TranslationConfig {
* Config for document types with a single language field that determines the language for the whole document.
*/
document?: DocumentTranslationConfig
/**
* A "style guide" that can be used to provide guidance on how to translate content.
* Will be passed to the LLM - ergo this is only a guide and the model _may_ not
* always follow it to the letter.
*/
styleguide?: string
}
3 changes: 3 additions & 0 deletions plugin/src/useApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface TranslateRequest {
documentId: string
translatePath: Path
languagePath?: string
styleguide?: string
fieldLanguageMap?: FieldLanguageMap[]
conditionalMembers?: ConditionalMemberState[]
}
Expand Down Expand Up @@ -63,6 +64,7 @@ export function useTranslate(apiClient: SanityClient) {
({
documentId,
languagePath,
styleguide,
translatePath,
fieldLanguageMap,
conditionalMembers,
Expand All @@ -79,6 +81,7 @@ export function useTranslate(apiClient: SanityClient) {
documentId,
types,
languagePath,
userStyleguide: styleguide,
fieldLanguageMap,
conditionalMembers,
translatePath:
Expand Down
1 change: 1 addition & 0 deletions studio/sanity.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export default defineConfig({
documentTypes: translatedDocTypes,
languageField: 'language',
},
styleguide: 'Retain the word "Headless" in all translations as is, regardless of language.',
},

__customApiClient: (defaultClient) =>
Expand Down