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

chore: doc, exports #7

Merged
merged 1 commit into from
Apr 29, 2024
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
22 changes: 21 additions & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "payload-plugin-translator",
"private": true,
"version": "1.0.0-beta.1",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -31,6 +31,16 @@
"require": "./src/index.ts",
"types": "./src/index.ts"
},
"./types": {
"import": "./src/types.ts",
"require": "./src/types.ts",
"types": "./src/types.ts"
},
"./resolvers/types": {
"import": "./src/resolvers/types.ts",
"require": "./src/resolvers/types.ts",
"types": "./src/resolvers/types.ts"
},
"./resolvers/google": {
"import": "./src/resolvers/google.ts",
"require": "./src/resolvers/google.ts",
Expand Down Expand Up @@ -59,6 +69,16 @@
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./types": {
"import": "./dist/types.js",
"require": "./dist/types.js",
"types": "./dist/types.d.ts"
},
"./resolvers/types": {
"import": "./dist/resolvers/types.js",
"require": "./dist/resolvers/types.js",
"types": "./dist/resolvers/types.d.ts"
},
"./resolvers/google": {
"import": "./dist/resolvers/google.js",
"require": "./dist/resolvers/google.js",
Expand Down
6 changes: 4 additions & 2 deletions plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { deepMerge } from 'payload/utilities';
import { CustomSaveButton } from './client/components/CustomSaveButton';
import { translateEndpoint } from './translate/endpoint';
import { translations } from './translations';
import type { PluginConfig } from './types';
import type { TranslatorPluginConfig } from './types';

export const payloadPluginTranslator: (pluginConfig: PluginConfig) => Plugin = (pluginConfig) => {
export const payloadPluginTranslator: (pluginConfig: TranslatorPluginConfig) => Plugin = (
pluginConfig,
) => {
return (config) => {
if (pluginConfig.disabled || !config.localization || config.localization.locales.length < 2)
return config;
Expand Down
18 changes: 12 additions & 6 deletions plugin/src/resolvers/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import type { TranslateResolver } from './types';

type GoogleResponse = {
data: {
translations: {
detectedSourceLanguage: string;
model: string;
translatedText: string;
}[];
data: {
translations: {
detectedSourceLanguage: string;
model: string;
translatedText: string;
}[];
};
};
success: boolean;
};
Expand All @@ -23,6 +25,10 @@ const mapLocale = (incoming: string) =>

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

Expand Down Expand Up @@ -73,7 +79,7 @@ export const googleResolver = ({
}

const translatedTexts = responses
.flatMap((chunk) => chunk.data.translations)
.flatMap((chunk) => chunk.data.data.translations)
.map((translation) => translation.translatedText);

return {
Expand Down
7 changes: 7 additions & 0 deletions plugin/src/resolvers/openAI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ export type OpenAIPrompt = (args: {

export type OpenAIResolverConfig = {
apiKey: string;
/**
* How many texts to include into 1 request
* @default 100
*/
chunkLength?: number;
/**
* @default "gpt-3.5-turbo"
*/
model?: string;
promt?: OpenAIPrompt;
};
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/resolvers/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { PayloadRequest } from 'payload/types';

export type TranslateResolverArgs = {
/** Locale to translate from */
localeFrom: string;
/** Locale to translate to */
localeTo: string;
req: PayloadRequest;
texts: string[];
Expand Down
14 changes: 13 additions & 1 deletion plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ import type { GeneratedTypes } from 'payload';

import type { TranslateResolver } from './resolvers/types';

export type PluginConfig = {
export type TranslatorPluginConfig = {
/**
* Collections with the enabled translator in the admin UI
*/
collections: (keyof GeneratedTypes['collections'])[];
/**
* Disable the plugin
*/
disabled?: boolean;
/**
* Globals with the enabled translator in the admin UI
*/
globals: (keyof GeneratedTypes['globals'])[];
/**
* Add resolvers that you want to include, examples on how to write your own in ./plugin/src/resolvers
*/
resolvers: TranslateResolver[];
};