Skip to content

Commit

Permalink
feat(endpoints): add custom endpoints for global translation sync (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Jul 7, 2023
1 parent 2ad3af9 commit 08b2163
Show file tree
Hide file tree
Showing 11 changed files with 1,009 additions and 731 deletions.
92 changes: 11 additions & 81 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,91 +113,21 @@ All of the collections created by this plugin are designed to emulate the struct

Translation synchronisation refers to the process of loading translations from CrowdIn into Payload CMS. If [drafts](https://payloadcms.com/docs/versions/drafts) are enabled, this will create a new version in Payload CMS for each locale. The source locale (e.g. `en`) is not affected.

**A UI has not been developed for this feature yet**. To perform updates now, add the following routes to `server.ts` in your Payload installation:
**A UI has not been developed for this feature yet**. To perform updates now, use custom REST API endpoints that are made available by this plugin.

```ts
import {
updateTranslation
} from 'payload-crowdin-sync'

app.get('/translations/global/:global', async (_, res) => {
const collection = _.params.global
const client = (_ as any).crowdinClient

const report = await updateTranslation({
projectId: 323731,
documentId: null,
collection: collection,
payload: payload,
crowdin: client,
global: true,
})

res.setHeader('content-type', 'application/json')
res.send(report)
})

app.get('/translations/global/:global/update', async (_, res) => {
const collection = _.params.global
const client = (_ as any).crowdinClient

const report = await updateTranslation({
projectId: 323731,
documentId: null,
collection: collection,
payload: payload,
crowdin: client,
global: true,
dryRun: false,
})

res.setHeader('content-type', 'application/json')
res.send(report)
})

app.get('/translations/:collection/:id', async (_, res) => {
const collection = _.params.collection
const id = _.params.id
const client = (_ as any).crowdinClient

const report = await updateTranslation({
projectId: 323731,
documentId: id,
collection: collection,
payload: payload,
crowdin: client
})

res.setHeader('content-type', 'application/json')
res.send(report)
})

app.get('/translations/:collection/:id/update', async (_, res) => {
const collection = _.params.collection
const id = _.params.id
const client = (_ as any).crowdinClient

const report = await updateTranslation({
projectId: 323731,
documentId: id,
collection: collection,
payload: payload,
crowdin: client,
dryRun: false
})

res.setHeader('content-type', 'application/json')
res.send(report)
})
```
If supplied translations do not contain required fields, translation updates will not be applied and validation errors will be returned in the API response.

#### Sync global translations

To sync global translations, add a new article in **Crowdin Translations** that contains the global `slug`. Each article contains an `excludeLocales` field that can be used to prevent some locales from being included in the update operation.

#### Dry run
##### Dry run

To review translations, visit:

`<payload-base-url>/translations/<collection-slug>/<article-id>`
`<payload-base-url>/api/crowdin-translations/<article-id>/review`

e.g. `https://my-payload-app.com/translations/policies/635fe6227589577f859835d4`
e.g. `https://my-payload-app.com/api/crowdin-translations/64a880bb87ef685285a4d9dc/update`

A JSON object is returned that allows you to review what will be updated in the database. The JSON object will contain the following keys:

Expand All @@ -212,9 +142,9 @@ A JSON object is returned that allows you to review what will be updated in the

To update translations, visit:

`<payload-base-url>/translations/<collection-slug>/<article-id>/update`
`<payload-base-url>/api/crowdin-translations/<article-id>/review`

e.g. `https://my-payload-app.com/translations/policies/635fe6227589577f859835d4/update`
e.g. `https://my-payload-app.com/api/crowdin-translations/64a880bb87ef685285a4d9dc/update`

The document will be updated and the same report will be generated as for a review.

Expand Down
4 changes: 2 additions & 2 deletions dev/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
"dependencies": {
"dotenv": "^16.3.1",
"express": "^4.18.2",
"payload": "^1.10.5"
"payload": "^1.11.0"
},
"devDependencies": {
"@types/express": "^4.17.17",
"copyfiles": "^2.4.1",
"cross-env": "^7.0.3",
"jest": "^29.5.0",
"jest": "^29.6.1",
"mongodb-memory-server": "^8.13.0",
"nodemon": "^2.0.22",
"ts-node": "^10.9.1",
Expand Down
22 changes: 22 additions & 0 deletions dev/src/globals/Nav.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { GlobalConfig } from "payload/types";

const Nav: GlobalConfig = {
slug: "nav",
fields: [
{
name: "items",
type: "array",
required: true,
maxRows: 8,
fields: [
{
name: "label",
type: "text",
localized: true,
},
],
},
],
};

export default Nav;
8 changes: 6 additions & 2 deletions dev/src/payload.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { buildConfig } from 'payload/config';
import path from 'path';
import Nav from './globals/Nav';
import Categories from './collections/Categories';
import LocalizedPosts from './collections/LocalizedPosts';
import Posts from './collections/Posts';
Expand All @@ -10,9 +11,11 @@ import { resolve } from 'path';

import { crowdInSync } from '../../dist';

require('dotenv').config({
import dotenv from 'dotenv'

dotenv.config({
path: resolve(__dirname, '../.env'),
});
})

export const localeMap = {
de_DE: {
Expand Down Expand Up @@ -44,6 +47,7 @@ export default buildConfig({
Tags,
Users,
],
globals: [Nav],
localization: {
locales: ['en', ...Object.keys(localeMap)],
defaultLocale: 'en',
Expand Down
Loading

0 comments on commit 08b2163

Please sign in to comment.