Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
sargreal committed Feb 6, 2025
2 parents d0484c4 + 966d628 commit 57603eb
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 22 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "strapi-plugin-translate",
"version": "2.0.0-next.1",
"version": "1.3.1",
"description": "Strapi plugin for managing and automating translation of content",
"keywords": [
"strapi",
Expand Down
1 change: 1 addition & 0 deletions plugin/server/src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
translatedFieldTypes: [
{ type: 'string', format: 'plain' },
{ type: 'text', format: 'plain' },
{ type: 'blocks', format: 'jsonb' },
{ type: 'richtext', format: 'markdown' },
'component',
'dynamiczone',
Expand Down
10 changes: 9 additions & 1 deletion providers/deepl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@ module.exports = {
// use uppercase here!
EN: 'EN-US',
},
// Optional: Pass glossaries on translation. The correct glossary for each translation is selected by the target_lang and source_lang properties
glossaries: [
{
id: "your-glossary-id",
target_lang: "DE",
source_lang: "EN",
}
],
apiOptions: {
// see <https://github.com/DeepLcom/deepl-node#text-translation-options> for supported options.
// note that tagHandling Mode cannot be set this way.
// note that tagHandling Mode and glossary cannot be set this way.
// use with caution, as non-default values may break translation of markdown
formality: 'default',
// ...
Expand Down
4 changes: 2 additions & 2 deletions providers/deepl/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "strapi-provider-translate-deepl",
"version": "1.3.0-next.1",
"version": "1.2.2",
"description": "DeepL provider for translate plugin in Strapi 4",
"keywords": [
"strapi",
Expand Down Expand Up @@ -66,7 +66,7 @@
},
"peerDependencies": {
"lodash": "*",
"strapi-plugin-translate": "2.0.0-next.1"
"strapi-plugin-translate": "1.3.1"
},
"engines": {
"node": ">=18 <=22",
Expand Down
108 changes: 108 additions & 0 deletions providers/deepl/src/lib/__tests__/deepl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,112 @@ describe('deepl provider', () => {
})
})
})

describe('glossaries', () => {
const glossaryHandler: HttpResponseResolver<
PathParams,
DefaultBodyType,
undefined
> = async ({ request }) => {
const body = await request.text()
const params = new URLSearchParams(body)
if (isAuthenticated(request)) {
let text = params.getAll('text')
if (text.length == 0) {
return new HttpResponse(null, { status: 400 })
}
if (text.length > 50) {
return new HttpResponse(null, { status: 413 })
}
let targetLang = params.get('target_lang')
if (!targetLang) {
return new HttpResponse(null, { status: 400 })
}
let glossary = params.get('glossary_id')
if (glossary) {
return HttpResponse.json({
translations: text.map((t) => ({
detected_source_language: 'EN',
text: `${t} (glossary: ${glossary})`,
})),
})
}
return HttpResponse.json({
translations: text.map((t) => ({
detected_source_language: 'EN',
text: t,
})),
})
}
return new HttpResponse(null, { status: 403 })
}

beforeEach(() => {
server.use(
http.post(`${DEEPL_FREE_API}/translate`, glossaryHandler),
http.post(`${DEEPL_PAID_API}/translate`, glossaryHandler)
)
})

it('uses the correct glossary for the given locale pair', async () => {
const deeplProvider = provider.init({
apiKey: authKey,
glossaries: [
{
id: 'glossary1',
source_lang: 'EN',
target_lang: 'DE',
},
{
id: 'glossary2',
source_lang: 'EN',
target_lang: 'FR',
},
],
})

const params = {
sourceLocale: 'en',
targetLocale: 'de',
text: 'Some text',
}

const result = await deeplProvider.translate(params)

expect(result).toEqual(['Some text (glossary: glossary1)'])
})

it('ignores glossary in apiOptions and logs a warning', async () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation()

const deeplProvider = provider.init({
apiKey: authKey,
glossaries: [
{
id: 'glossary1',
source_lang: 'EN',
target_lang: 'DE',
},
],
apiOptions: {
glossary: 'someOtherGlossary',
},
})

const params = {
sourceLocale: 'en',
targetLocale: 'de',
text: 'Some text',
}

const result = await deeplProvider.translate(params)

expect(result).toEqual(['Some text (glossary: glossary1)'])
expect(consoleWarnSpy).toHaveBeenCalledWith(
'Glossary provided in apiOptions will be ignored and overwritten by the actual glossary that should be used for this translation.'
)

consoleWarnSpy.mockRestore()
})
})
})
30 changes: 19 additions & 11 deletions providers/deepl/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getService } from './get-service'
export type DeepLProviderOptions = {
apiKey?: string
apiUrl?: string
glossaries?: { id: string; source_lang: string; target_lang: string }[]
localeMap?: Record<string, string>
apiOptions?: Record<string, string>
}
Expand All @@ -33,6 +34,10 @@ export default {
typeof providerOptions.apiOptions === 'object'
? providerOptions.apiOptions
: {}
const glossaries =
Array.isArray(providerOptions.glossaries)
? providerOptions.glossaries
: []

const client = new Translator(apiKey, {
serverUrl: apiUrl,
Expand Down Expand Up @@ -93,6 +98,17 @@ export default {
maxByteSize: DEEPL_API_ROUGH_MAX_REQUEST_SIZE,
})

const parsedSourceLocale = parseLocale(sourceLocale, localeMap, 'source')
const parsedTargetLocale = parseLocale(targetLocale, localeMap, 'target')

const glossary = glossaries.find(
(g) => g.target_lang === parsedTargetLocale && g.source_lang === parsedSourceLocale
)?.id

if (apiOptions.glossary) {
console.warn('Glossary provided in apiOptions will be ignored and overwritten by the actual glossary that should be used for this translation.')
}

const result = reduceFunction(
await Promise.all(
chunks.map(async (texts) => {
Expand All @@ -104,17 +120,9 @@ export default {
: DEEPL_PRIORITY_DEFAULT,
},
texts,
parseLocale(
sourceLocale,
localeMap,
'source'
) as SourceLanguageCode,
parseLocale(
targetLocale,
localeMap,
'target'
) as TargetLanguageCode,
{ ...apiOptions, tagHandling }
parsedSourceLocale as SourceLanguageCode,
parsedTargetLocale as TargetLanguageCode,
{ ...apiOptions, tagHandling, glossary }
)
return Array.isArray(result)
? result.map((value) => value.text)
Expand Down
4 changes: 2 additions & 2 deletions providers/libretranslate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "strapi-provider-translate-libretranslate",
"version": "1.2.0-next.1",
"version": "1.1.1",
"description": "Libretranslate provider for translate plugin in Strapi 4",
"keywords": [
"strapi",
Expand Down Expand Up @@ -67,7 +67,7 @@
},
"peerDependencies": {
"lodash": "*",
"strapi-plugin-translate": "2.0.0-next.1"
"strapi-plugin-translate": "1.3.1"
},
"engines": {
"node": ">=18 <=22",
Expand Down

0 comments on commit 57603eb

Please sign in to comment.