Skip to content

Commit

Permalink
feat(crowdincollectiontranslations): add (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Jul 8, 2023
1 parent 208bac7 commit 15e4edb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
25 changes: 25 additions & 0 deletions src/collections/CrowdInCollectionTranslations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CollectionConfig } from 'payload/types'

const CrowdInCollectionTranslations: CollectionConfig = {
slug: 'crowdin-collection-translations',
admin: {
defaultColumns: ['id', 'collection', 'updatedAt'],
useAsTitle: 'slug',
group: "CrowdIn Admin",
},
access: {
read: () => true,
},
fields: [
{
name: 'documentId',
type: 'text',
},
{
name: 'collection',
type: 'text',
},
],
}

export default CrowdInCollectionTranslations
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CollectionConfig } from 'payload/types'

const CrowdInGlobalTranslations: CollectionConfig = {
slug: 'crowdin-translations',
slug: 'crowdin-global-translations',
admin: {
defaultColumns: ['slug', 'updatedAt'],
useAsTitle: 'slug',
Expand Down
12 changes: 8 additions & 4 deletions src/endpoints/globals/reviewTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { Endpoint } from 'payload/config'
import { PluginOptions } from '../../types'
import { payloadCrowdInSyncTranslationsApi } from '../../api/payload-crowdin-sync/translations'

export const getReviewTranslationEndpoint = (pluginOptions: PluginOptions, type: 'review' | 'update' = 'review'): Endpoint => ({
export const getReviewTranslationEndpoint = ({
pluginOptions,
global = false,
type = 'review',
}: {pluginOptions: PluginOptions, global?: boolean, type?: 'review' | 'update'}): Endpoint => ({
path: `/:id/${type}`,
method: "get",
handler: async (req, res, next) => {
Expand All @@ -16,9 +20,9 @@ export const getReviewTranslationEndpoint = (pluginOptions: PluginOptions, type:
)
try {
const translations = await translationsApi.updateTranslation({
documentId: '',
collection: translation.slug as string,
global: true,
documentId: !global && translation.documentId,
collection: global ? translation.slug as string : translation.collection as string,
global,
dryRun: type === 'update' ? false : true,
excludeLocales: translation.excludeLocales || [],
})
Expand Down
39 changes: 36 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { getFields } from './fields/getFields'
import CrowdInFiles from './collections/CrowdInFiles'
import CrowdInCollectionDirectories from './collections/CrowdInCollectionDirectories'
import CrowdInArticleDirectories from './collections/CrowdInArticleDirectories'
import CrowdInGlobalTranslations from './collections/CrowdInTranslations'
import CrowdInCollectionTranslations from './collections/CrowdInCollectionTranslations'
import CrowdInGlobalTranslations from './collections/CrowdInGlobalTranslations'
import {
containsLocalizedFields,
} from './utilities'
Expand Down Expand Up @@ -74,8 +75,40 @@ export const crowdInSync =
],
endpoints: [
...(CrowdInGlobalTranslations.endpoints || []),
getReviewTranslationEndpoint(pluginOptions),
getReviewTranslationEndpoint(pluginOptions, 'update'),
getReviewTranslationEndpoint({
pluginOptions,
global: true,
}),
getReviewTranslationEndpoint({
pluginOptions,
global: true,
type: 'update'
}),
],
},
{
...CrowdInCollectionTranslations,
fields: [
...(CrowdInCollectionTranslations.fields || []),
{
name: 'excludeLocales',
type: 'select',
options: Object.keys(pluginOptions.localeMap),
hasMany: true,
admin: {
description: 'Select locales to exclude from translation synchronization.',
}
},
],
endpoints: [
...(CrowdInCollectionTranslations.endpoints || []),
getReviewTranslationEndpoint({
pluginOptions
}),
getReviewTranslationEndpoint({
pluginOptions,
type: 'update'
}),
],
},
],
Expand Down

0 comments on commit 15e4edb

Please sign in to comment.