r1tsuu

translator

by r1tsuu
payload-enchants
Translation
v3
^3.1.0
MIT

Overview

Summary generated from the README

Adds a translate action to the admin UI for enabled collections and globals, filling localized fields from another locale. Resolvers perform the work, with built-in copy, Google Translate, OpenAI and LibreTranslate options, plus support for custom ones. It also exposes a Local API operation for use in hooks. Requires Payload 3.1 or later with localization enabled.

Features

  • Resolvers for copy, Google Translate, OpenAI and LibreTranslate
  • Write custom resolvers for any translation service or transformation
  • Handles nested document structures and Lexical and Slate rich text
  • Local API translateOperation for use in hooks and scripts
  • Translate only empty fields or all fields
  • Skip individual fields with a custom translatorSkip flag

Use cases

  • Translate a new post into every configured locale with one click in the admin panel
  • Copy or machine-translate document data to other locales automatically in an afterChange hook
  • Use a custom OpenAI prompt to control translation tone for localized content

Installation

npm i @payload-enchants/translator

335

Stars

28

Forks

28

Open Issues

1 years ago

Last Updated

1.5k

Weekly Downloads

v1.3.0

npm Version

157.5 KB

Unpacked Size

2

Dependencies

Repository stars

335+2 since Sep 7

Show data
Repository stars by day (UTC)
DateStars
Dec 6, 2026335
Dec 5, 2026335
Dec 4, 2026335
Dec 3, 2026335
Dec 2, 2026335
Dec 1, 2026335
Nov 30, 2026335
Nov 29, 2026335
Nov 28, 2026335
Nov 27, 2026335
Nov 26, 2026335
Nov 25, 2026335
Nov 24, 2026335
Nov 23, 2026335
Nov 22, 2026335
Nov 21, 2026335
Nov 20, 2026335
Nov 19, 2026336
Nov 18, 2026336
Nov 17, 2026335
Nov 16, 2026335
Nov 15, 2026335
Nov 14, 2026335
Nov 13, 2026335
Nov 12, 2026335
Nov 11, 2026335
Nov 10, 2026335
Nov 9, 2026335
Nov 8, 2026335
Nov 7, 2026335
Nov 6, 2026335
Nov 5, 2026335
Nov 4, 2026335
Nov 3, 2026335
Nov 2, 2026335
Nov 1, 2026335
Oct 31, 2026335
Oct 30, 2026335
Oct 29, 2026335
Oct 28, 2026335
Oct 27, 2026335
Oct 26, 2026335
Oct 25, 2026335
Oct 24, 2026334
Oct 23, 2026334
Oct 22, 2026334
Oct 21, 2026334
Oct 20, 2026334
Oct 19, 2026334
Oct 18, 2026334
Oct 17, 2026334
Oct 16, 2026334
Oct 15, 2026334
Oct 14, 2026334
Oct 13, 2026334
Oct 12, 2026334
Oct 11, 2026333
Oct 10, 2026333
Oct 9, 2026333
Oct 8, 2026333
Oct 7, 2026333
Oct 6, 2026333
Oct 5, 2026333
Oct 4, 2026333
Oct 3, 2026333
Oct 2, 2026333
Oct 1, 2026333
Sep 30, 2026333
Sep 29, 2026333
Sep 28, 2026333
Sep 27, 2026333
Sep 26, 2026333
Sep 25, 2026333
Sep 24, 2026333
Sep 23, 2026333
Sep 22, 2026333
Sep 21, 2026333
Sep 20, 2026333
Sep 19, 2026333
Sep 18, 2026333
Sep 17, 2026333
Sep 16, 2026333
Sep 15, 2026333
Sep 14, 2026333
Sep 13, 2026333
Sep 12, 2026333
Sep 11, 2026333
Sep 10, 2026333
Sep 9, 2026333
Sep 8, 2026333
Sep 7, 2026333

Shared by 7 plugins in r1tsuu/payload-enchants.

npm weekly downloads

1.5K−2.5K since Sep 7

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 6, 20261,484
Dec 5, 20261,408
Dec 4, 20261,805
Dec 3, 20262,878
Dec 2, 20263,304
Dec 1, 20263,300
Nov 30, 20263,521
Nov 29, 20264,419
Nov 28, 20264,849
Nov 27, 20264,627
Nov 26, 20264,257
Nov 25, 20263,635
Nov 24, 20263,650
Nov 23, 20263,514
Nov 22, 20263,064
Nov 21, 20263,219
Nov 20, 20263,943
Nov 19, 20263,639
Nov 18, 20263,997
Nov 17, 20264,014
Nov 16, 20264,067
Nov 15, 20263,833
Nov 14, 20263,714
Nov 13, 20263,223
Nov 12, 20263,422
Nov 11, 20263,244
Nov 10, 20263,268
Nov 9, 20263,095
Nov 8, 20263,757
Nov 7, 20264,125
Nov 6, 20263,934
Nov 5, 20263,525
Nov 4, 20263,511
Nov 3, 20263,495
Nov 2, 20263,629
Nov 1, 20263,170
Oct 31, 20262,786
Oct 30, 20262,615
Oct 29, 20262,159
Oct 28, 20261,697
Oct 27, 20261,790
Oct 26, 20261,801
Oct 25, 20261,879
Oct 24, 20262,019
Oct 23, 20261,711
Oct 22, 20262,324
Oct 21, 20263,083
Oct 20, 20263,076
Oct 19, 20263,028
Oct 18, 20262,443
Oct 17, 20261,864
Oct 16, 20262,088
Oct 15, 20262,035
Oct 14, 20261,775
Oct 13, 20261,645
Oct 12, 20261,555
Oct 11, 20261,661
Oct 10, 20262,620
Oct 9, 20263,080
Oct 8, 20263,228
Oct 7, 20263,876
Oct 6, 20264,440
Oct 5, 20265,191
Oct 4, 20267,360
Oct 3, 20268,749
Oct 2, 202614,647
Oct 1, 202620,512
Sep 30, 202623,725
Sep 29, 202624,428
Sep 28, 202625,308
Sep 27, 202623,449
Sep 26, 202622,139
Sep 25, 202616,747
Sep 24, 202611,994
Sep 23, 20268,439
Sep 22, 20267,439
Sep 21, 20266,118
Sep 20, 202611,905
Sep 19, 202613,962
Sep 18, 202613,855
Sep 17, 202613,150
Sep 16, 202612,760
Sep 15, 202613,921
Sep 14, 202614,192
Sep 13, 20268,396
Sep 12, 20265,758
Sep 11, 20265,420
Sep 10, 20264,839
Sep 9, 20264,872
Sep 8, 20263,763
Sep 7, 20263,984

Each point is npm's rolling 7-day download count.

Health Score

54/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/684372049-translator.svg)](https://payloaddirectory.dev/plugins/684372049-translator)
HTML
<a href="https://payloaddirectory.dev/plugins/684372049-translator"><img src="https://payloaddirectory.dev/badge/684372049-translator.svg" alt="Payload Directory"></a>
Updated Jan 4, 2025Created Aug 29, 2023Published Nov 24, 2024

README

Translator plugin for Payload 3.0 (beta)

Install

pnpm add @payload-enchants/translator

Video

https://github.com/r1tsuu/payload-plugin-translator/assets/64744993/d39aeba4-bafc-4c3b-838e-9abc5cf1d64a

Features:

  1. A flexible structure with resolvers that allows you to apply any kind of transformation to your localizated data.
  2. Can be used not only from the admin panel, but within Local API as well. Example of the hook that automatically fills the other locales data on create
  3. Out of the box supports 3 resolvers - Copy, Google Translate, OpenAI and your own can be written easily.
  4. Works with any nested document structure and 2 Rich Text editor adapters - Lexical and Slate.
  5. You can omit some fields to translate. Documentation

Usage:

import { buildConfig } from 'payload/config';
import {
  translator,
  copyResolver,
  googleResolver,
  openAIResolver,
  libreResolver,
} from '@payload-enchants/translator';

export default buildConfig({
  plugins: [
    translator({
      // collections with the enabled translator in the admin UI
      collections: ['posts', 'small-posts'],
      // globals with the enabled translator in the admin UI
      globals: [],
      // add resolvers that you want to include, examples on how to write your own in ./plugin/src/resolvers
      resolvers: [
        copyResolver(),
        googleResolver({
          apiKey: process.env.GOOGLE_API_KEY!,
        }),
        openAIResolver({
          apiKey: process.env.OPENAI_KEY!,
        }),
        libreResolver({
          apiKey: process.env.LIBRE_KEY!,
        }),
      ],
    }),
  ],
});

Resolvers

OpenAI

Config:
export type OpenAIResolverConfig = {
  apiKey: string; // API key
  chunkLength?: number; // How many texts to include into 1 request, default: 100
  model?: string; // model, default: 'gpt-3.5-turbo'
  promt?: OpenAIPrompt; // custom prompt
};

Custom prompt:

export type OpenAIPrompt = (args: {
  localeFrom: string;
  localeTo: string;
  texts: string[];
}) => string;

// Default
const defaultPromt: OpenAIPrompt = ({ localeFrom, localeTo, texts }) => {
  return `Translate me the following array: ${JSON.stringify(texts)} in locale=${localeFrom} to locale ${localeTo}, respond me with the same array structure`;
};

Google

Config:
export type GoogleResolverConfig = {
  apiKey: string; // API key
  chunkLength?: number; / /How many texts to include into 1 request, default: 100
};

Writing your own

import type { TranslateResolver } from '@payload-enchants/translator/resolvers/types';

const myResolver: TranslateResolver = {
  key: 'my',
  resolve: async (args) => {
    const { localeFrom, localeTo, req, texts } = args;
    // here you can apply any kind of transformation to incoming texts, could be as well API call to a service.
    const transformed = texts.map((each) => `${each} translated to ${localeTo}`);

    return {
      success: true,
      translatedTexts: transformed,
    };
  },
};

export default buildConfig({
  plugins: [
    payloadPluginTranslator({
      collections: ['posts', 'small-posts'],
      globals: [],
      resolvers: [myResolver],
    }),
  ],
  // apply translations that will be used for your resolver in the admin UI
  i18n: {
    supportedLanguages: { en },
    translations: {
      en: {
        'plugin-translator': {
          resolver_my_buttonLabel: 'Google Translate',
          resolver_my_errorMessage: 'An error occurred when trying to translate the data',
          resolver_my_modalTitle: 'Choose the locale to translate from',
          resolver_my_submitButtonLabelEmpty: 'Translate only empty fields',
          resolver_my_submitButtonLabelFull: 'Translate all',
          resolver_my_successMessage: 'Successfully translated. Press "Save" to apply the changes.',
        },
      },
    },
  },
});

Using the Local API

import { translateOperation } from '@payload-enchants/translator';

const translateResult = await translateOperation({
  collectionSlug: 'posts', // or globalSlug if globals,
  emptyOnly: false, // optional, should translate all the fields values or only fields that are empty, by default false.
  id: postDefaultLocale.id, // pass the doc id if it's a collection
  locale: 'de', // locale to translate to
  localeFrom: 'en', // locale to translate from
  payload, // payload instance, you can get it with getPayload or req.payload in hooks
  req, // PayloadRequest, can be used instead of payload, more appreciable than payload if you have it
  overrideAccess: false, //
  resolver: 'copy', // pass resolver key
  update: true, // optional, should update immediately or just return the translated result, default false
  data: {}, // Optional, if you want to translate from your data passed here instead of the current doc, should be in "localeFrom" locale, for example { title: "Hello" }
});

Example of the hook that uses local operation to copy the doc data to other locales

import type { CollectionAfterChangeHook } from 'payload/types';
import { translateOperation } from '@payload-enchants/translator';

export const copyOtherLocales: CollectionAfterChangeHook = async ({
  collection,
  doc,
  operation,
  req,
}) => {
  if (operation !== 'create') return;

  const locale = req.locale;

  if (!locale || !req.payload.config.localization) return;

  const otherLocales = req.payload.config.localization.locales.filter(
    (each) => each.code !== locale,
  );

  const { id } = doc;

  for (const { code } of otherLocales) {
    await translateOperation({
      collectionSlug: collection.slug,
      data: doc,
      id,
      locale: code,
      localeFrom: locale,
      req,
      resolver: 'copy',
      update: true,
    });
  }
};

Omitting fields

To omit a specific field for translation, simply add custom.translatorSkip = true to the field's config.

const field = {
  custom: {
    translatorSkip: true,
  },
  localized: true,
  name: 'skip',
  type: 'text',
};

Similar plugins