Skip to content

Commit

Permalink
(translator): omitting fields, fix emptyOnly, pass current data from …
Browse files Browse the repository at this point in the history
…admin (#74)
  • Loading branch information
r1tsuu authored Jun 16, 2024
1 parent 5e4dffa commit f88c0f0
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "payload-enchants",
"version": "1.1.35",
"version": "1.1.36",
"private": true,
"description": "",
"keywords": [],
Expand Down
2 changes: 1 addition & 1 deletion packages/better-localized-fields/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/better-localized-fields",
"version": "1.1.35",
"version": "1.1.36",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/better-use-as-title/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/better-use-as-title",
"version": "1.1.35",
"version": "1.1.36",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/bump-payload/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bump-payload",
"version": "1.1.35",
"version": "1.1.36",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/cached-local-api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/cached-local-api",
"version": "1.1.35",
"version": "1.1.36",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/docs-reorder/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/docs-reorder",
"version": "1.1.35",
"version": "1.1.36",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
2 changes: 1 addition & 1 deletion packages/seo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/seo",
"version": "1.1.35",
"version": "1.1.36",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
16 changes: 16 additions & 0 deletions packages/translator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ https://github.com/r1tsuu/payload-plugin-translator/assets/64744993/d39aeba4-baf
2. Can be used not only from the admin panel, but within Local API as well. [Example of the hook](#example-of-the-hook-that-uses-local-operation-to-copy-the-doc-data-to-other-locales) 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](#omitting-fields)

## Usage:

Expand Down Expand Up @@ -191,3 +192,18 @@ export const copyOtherLocales: CollectionAfterChangeHook = async ({
}
};
```

### Omitting fields

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

```ts
const field = {
custom: {
translatorSkip: true,
},
localized: true,
name: 'skip',
type: 'text',
};
```
2 changes: 1 addition & 1 deletion packages/translator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@payload-enchants/translator",
"version": "1.1.35",
"version": "1.1.36",
"private": false,
"bugs": "https://github.com/r1tsuu/payload-enchants/issues",
"repository": "https://github.com/r1tsuu/payload-enchants",
Expand Down
3 changes: 2 additions & 1 deletion packages/translator/src/translate/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ export const translateEndpoint: PayloadHandler = async (req) => {

const args: TranslateEndpointArgs = await req.json();

const { collectionSlug, emptyOnly, globalSlug, id, locale, localeFrom, resolver } = args;
const { collectionSlug, data, emptyOnly, globalSlug, id, locale, localeFrom, resolver } = args;

const result = await translateOperation({
collectionSlug,
data,
emptyOnly,
globalSlug,
id,
Expand Down
2 changes: 2 additions & 0 deletions packages/translator/src/translate/findEntityWithConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const findEntityWithConfig = async (
const docPromise = isGlobal
? payload.findGlobal({
depth: 0,
fallbackLocale: null,
locale,
overrideAccess,
req,
Expand All @@ -55,6 +56,7 @@ export const findEntityWithConfig = async (
: payload.findByID({
collection: collectionSlug as string,
depth: 0,
fallbackLocale: null,
id: id as number | string,
locale,
overrideAccess,
Expand Down
2 changes: 2 additions & 0 deletions packages/translator/src/translate/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export const translateOperation = async (args: TranslateOperationArgs) => {

let translatedData = args.data;

console.log(translatedData);

if (!translatedData) {
const { doc } = await findEntityWithConfig({
collectionSlug,
Expand Down
54 changes: 32 additions & 22 deletions packages/translator/src/translate/traverseFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,25 @@ export const traverseFields = ({

if (isEmpty(arrayDataFrom)) break;

const arrayDataTranslated = [] as { id: string }[];
let arrayDataTranslated =
(siblingDataTranslated[field.name] as { id: string }[] | undefined) ?? [];

arrayDataFrom.forEach((item, index) => {
arrayDataTranslated.push({
...(item ?? {}),
// ensure ids are different if localized
id: field.localized || !item.id ? ObjectID().toHexString() : item.id,
});
if (field.localized || localizedParent) {
if (arrayDataTranslated.length > 0 && emptyOnly) break;

arrayDataTranslated = arrayDataFrom.map(() => ({
id: ObjectID().toHexString(),
}));
}

arrayDataTranslated.forEach((item, index) => {
traverseFields({
dataFrom,
emptyOnly,
fields: field.fields,
localizedParent: localizedParent ?? field.localized,
siblingDataFrom: item,
siblingDataTranslated: arrayDataTranslated[index],
siblingDataFrom: arrayDataFrom[index],
siblingDataTranslated: item,
translatedData,
valuesToTranslate,
});
Expand All @@ -111,19 +114,24 @@ export const traverseFields = ({
break;

case 'blocks':
const blockDataFrom = siblingDataFrom[field.name] as { blockType: string; id: string }[];
const blocksDataFrom = siblingDataFrom[field.name] as { blockType: string; id: string }[];

if (isEmpty(blockDataFrom)) break;
if (isEmpty(blocksDataFrom)) break;

const blockDataTranslated = [] as { blockType: string; id: string }[];
let blocksDataTranslated =
(siblingDataTranslated[field.name] as { blockType: string; id: string }[] | undefined) ??
[];

blockDataFrom.forEach((item, index) => {
blockDataTranslated.push({
...item,
// ensure ids are different if localized
id: field.localized || !item.id ? ObjectID().toHexString() : item.id,
});
if (field.localized || localizedParent) {
if (blocksDataTranslated.length > 0 && emptyOnly) break;

blocksDataTranslated = blocksDataFrom.map(({ blockType }) => ({
blockType,
id: ObjectID().toHexString(),
}));
}

blocksDataTranslated.forEach((item, index) => {
const block = field.blocks.find((each) => each.slug === item.blockType);

if (!block) return;
Expand All @@ -132,15 +140,15 @@ export const traverseFields = ({
dataFrom,
emptyOnly,
fields: block.fields,
localizedParent,
siblingDataFrom: item,
siblingDataTranslated: blockDataTranslated[index],
localizedParent: localizedParent ?? field.localized,
siblingDataFrom: blocksDataFrom[index],
siblingDataTranslated: item,
translatedData,
valuesToTranslate,
});
});

siblingDataTranslated[field.name] = blockDataTranslated;
siblingDataTranslated[field.name] = blocksDataTranslated;

break;

Expand Down Expand Up @@ -176,6 +184,8 @@ export const traverseFields = ({

case 'text':
case 'textarea':
if (field.custom && typeof field.custom === 'object' && field.custom.translatorSkip) return;

if (!field.localized && !localizedParent && isEmpty(siblingDataFrom[field.name])) return;
if (emptyOnly && siblingDataTranslated[field.name]) return;

Expand Down
2 changes: 1 addition & 1 deletion packages/translator/src/translate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ export type TranslateResult =
translatedData: Record<string, any>;
};

export type TranslateEndpointArgs = Omit<TranslateArgs, 'data' | 'update'>;
export type TranslateEndpointArgs = Omit<TranslateArgs, 'update'>;
2 changes: 1 addition & 1 deletion test/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "test",
"version": "1.1.35",
"version": "1.1.36",
"private": true,
"type": "module",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions test/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ export default buildConfig({
name: 'title',
type: 'text',
},
{
custom: {
translatorSkip: true,
},
localized: true,
name: 'skip',
type: 'text',
},
{
localized: true,
name: 'checkbox',
Expand Down

0 comments on commit f88c0f0

Please sign in to comment.