r1tsuu / payload-enchants

Payload 3.0 enchantment packages
MIT License
135 stars 8 forks source link

Feature request: Option to add custom translation services #4

Closed matthijs166 closed 10 months ago

matthijs166 commented 10 months ago

It would be awesome if this plugin has a parameter option to add your own translation service.

https://github.com/payloadcms/payload/discussions/4370#discussioncomment-7798429

r1tsuu commented 10 months ago

Hello, thanks for bringing up my plugin :)

But you can pass translateService async function prop to the plugin.

type TranslateService = (text: string, target: string, source: string) => Promise<string>;

export type TranslatorConfig = {
  GOOGLE_API_KEY?: string;
  translateService?: TranslateService;
  afterTranslate?: (data: AfterTranslateData) => Promise<string[]>;
  beforeTranslate?: (data: BeforeTransalateData) => Promise<string[]>;
  access?: (req: PayloadRequest) => Promise<boolean> | boolean;
  slateToHtmlConfig?: SlateToHtmlConfig;
  htmlToSlateConfig?: HtmlToSlateConfig;
  collections?: string[];
  globals?: string[];
};

From readme:

translateService - If you would like to use for example DeepL instead of Google, you can write you own implementation for the service

For example with this:

plugins: [
  translatorPlugin({
    GOOGLE_API_KEY: process.env.GOOGLE_API_KEY,
    collections: ['examples'],
    translateService: (text, target, source) => `${text} ${target} ${source}`
  }),

Any text should transform to this pattern, target - language to transform, source - current language. Maybe would be good to add additional pre-defined services like DeepL and GPT3.5. I even tried GPT3.5, but it's much slower than Google and sometimes it breaks the structure of the needed response, DeepL should be fine.

I plan to make some things better, improve the code (now it's kinda mess), publish to npm (probably would nice?) after some time

matthijs166 commented 10 months ago

Ahhh think I'm blind 🙈 Should have read the docs... haha

Very cool! Thx for the response. Love the plugin you made