Skip to content

Commit d6b0cbb

Browse files
Merge #915
915: Implement language translations for handling multi-language plugin r=brunoocasali a=mimartinez # Pull Request ## Related issue Fixes #85 ## What does this PR do? - Implement language translations for handling multi-language plugin - Added translation files `en.json` and `es.json` ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Michel Díaz Martínez <[email protected]> Co-authored-by: Michel Díaz Martínez <[email protected]>
2 parents ffcf43c + 5bbc698 commit d6b0cbb

File tree

16 files changed

+263
-67
lines changed

16 files changed

+263
-67
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,11 @@ playground/src/plugins
129129

130130
# Sqlite Database from the playground
131131
data.db
132+
133+
############################
134+
# Local
135+
############################
136+
.nvmrc
137+
.prettierignore
138+
.prettierrc.js
139+
.vscode

admin/src/Hooks/useAlert.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
import { useNotification } from '@strapi/helper-plugin'
2+
import { useI18n } from './useI18n'
23

34
export function useAlert() {
45
const toggleNotification = useNotification() // HERE
6+
const { i18n } = useI18n()
7+
58
/**
69
* @param {object} options
710
* @param {string} [options.type='info']
811
* @param {string} [options.message='SomethingoccuredinMeilisearch']
912
* @param {object} [options.link]
1013
* @param {boolean} [options.blockTransition]
1114
*/
12-
const handleNotification = ({
15+
function handleNotification({
1316
type = 'info',
14-
message = 'Something occured in Meilisearch',
17+
message = i18n(
18+
'plugin.message.something',
19+
'Something occured in Meilisearch',
20+
),
1521
link,
1622
blockTransition = true,
1723
title,
18-
}) => {
24+
}) {
1925
toggleNotification({
2026
// optional
2127
title,
@@ -40,9 +46,12 @@ export function useAlert() {
4046
const status = response?.payload?.error?.status
4147
if (status && status === 403) {
4248
handleNotification({
43-
title: 'Forbidden',
49+
title: i18n('plugin.message.forbidden.title', 'Forbidden'),
4450
type: 'warning',
45-
message: 'You do not have permission to do this action',
51+
message: i18n(
52+
'plugin.message.forbidden.description',
53+
'You do not have permission to do this action',
54+
),
4655
blockTransition: false,
4756
})
4857
}

admin/src/Hooks/useCollection.js

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import { useState, useEffect } from 'react'
22
import { request } from '@strapi/helper-plugin'
33
import pluginId from '../pluginId'
44
import useAlert from './useAlert'
5-
6-
const hookingTextRendering = ({ indexed, listened }) => {
7-
if (indexed && !listened) return 'Reload needed'
8-
if (!indexed && listened) return 'Reload needed'
9-
if (indexed && listened) return 'Hooked'
10-
if (!indexed && !listened) return '/'
11-
}
5+
import { useI18n } from './useI18n'
126

137
export function useCollection() {
148
const [collections, setCollections] = useState([])
@@ -17,10 +11,20 @@ export function useCollection() {
1711
const [realTimeReports, setRealTimeReports] = useState(false)
1812

1913
const { handleNotification, checkForbiddenError } = useAlert()
14+
const { i18n } = useI18n()
2015

2116
const refetchCollection = () =>
2217
setRefetchIndex(prevRefetchIndex => !prevRefetchIndex)
2318

19+
const hookingTextRendering = ({ indexed, listened }) => {
20+
if (indexed && listened)
21+
return i18n('plugin.table.td.hookingText.hooked', 'Hooked')
22+
23+
if (!indexed && !listened) return '/'
24+
25+
return i18n('plugin.table.td.hookingText.reload', 'Reload needed')
26+
}
27+
2428
const fetchCollections = async () => {
2529
const { data, error } = await request(`/${pluginId}/content-type/`, {
2630
method: 'GET',
@@ -41,7 +45,9 @@ export function useCollection() {
4145
return collection
4246
})
4347
const reload = collections.find(
44-
col => col.reloadNeeded === 'Reload needed',
48+
col =>
49+
col.reloadNeeded ===
50+
i18n('plugin.table.td.hookingText.reload', 'Reload needed'),
4551
)
4652

4753
const isIndexing = collections.find(col => col.isIndexing === true)
@@ -74,7 +80,10 @@ export function useCollection() {
7480
refetchCollection()
7581
handleNotification({
7682
type: 'success',
77-
message: 'Request to delete content-type is successful',
83+
message: i18n(
84+
'plugin.message.success.delete',
85+
'Request to delete content-type is successful',
86+
),
7887
blockTransition: false,
7988
})
8089
}
@@ -101,7 +110,10 @@ export function useCollection() {
101110
refetchCollection()
102111
handleNotification({
103112
type: 'success',
104-
message: 'Request to add a content-type is successful',
113+
message: i18n(
114+
'plugin.message.success.add',
115+
'Request to add a content-type is successful',
116+
),
105117
blockTransition: false,
106118
})
107119
}
@@ -129,7 +141,10 @@ export function useCollection() {
129141
refetchCollection()
130142
handleNotification({
131143
type: 'success',
132-
message: 'Request to update content-type is successful',
144+
message: i18n(
145+
'plugin.message.success.update',
146+
'Request to update content-type is successful',
147+
),
133148
blockTransition: false,
134149
})
135150
}

admin/src/Hooks/useCredential.js

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { useState, useEffect } from 'react'
22
import { request } from '@strapi/helper-plugin'
33
import pluginId from '../pluginId'
44
import useAlert from './useAlert'
5+
import { useI18n } from './useI18n'
56

67
export function useCredential() {
78
const [credentials, setCredentials] = useState({
@@ -13,36 +14,36 @@ export function useCredential() {
1314
const [refetchIndex, setRefetchIndex] = useState(true)
1415
const [host, setHost] = useState('')
1516
const [apiKey, setApiKey] = useState('')
16-
const { handleNotification, checkForbiddenError } = useAlert()
17+
const { handleNotification } = useAlert()
18+
const { i18n } = useI18n()
1719

1820
const refetchCredentials = () =>
1921
setRefetchIndex(prevRefetchIndex => !prevRefetchIndex)
2022

2123
const updateCredentials = async () => {
22-
try {
23-
const { error } = await request(`/${pluginId}/credential`, {
24-
method: 'POST',
25-
body: {
26-
apiKey: apiKey,
27-
host: host,
28-
},
24+
const { error } = await request(`/${pluginId}/credential`, {
25+
method: 'POST',
26+
body: {
27+
apiKey: apiKey,
28+
host: host,
29+
},
30+
})
31+
if (error) {
32+
handleNotification({
33+
type: 'warning',
34+
message: error.message,
35+
link: error.link,
36+
})
37+
} else {
38+
refetchCredentials()
39+
handleNotification({
40+
type: 'success',
41+
message: i18n(
42+
'plugin.message.success.credentials',
43+
'Credentials sucessfully updated!',
44+
),
45+
blockTransition: false,
2946
})
30-
if (error) {
31-
handleNotification({
32-
type: 'warning',
33-
message: error.message,
34-
link: error.link,
35-
})
36-
} else {
37-
refetchCredentials()
38-
handleNotification({
39-
type: 'success',
40-
message: 'Credentials sucessfully updated!',
41-
blockTransition: false,
42-
})
43-
}
44-
} catch (error) {
45-
checkForbiddenError(error)
4647
}
4748
}
4849

admin/src/Hooks/useI18n.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useIntl } from 'react-intl'
2+
import pluginId from '../pluginId'
3+
4+
export const useI18n = () => {
5+
const { formatMessage } = useIntl()
6+
7+
const i18n = (key, defaultMessage) => {
8+
return formatMessage({
9+
id: `${pluginId}.${key}`,
10+
defaultMessage,
11+
})
12+
}
13+
14+
return {
15+
i18n,
16+
}
17+
}

admin/src/containers/Collection/CollectionColumn.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
Tr,
99
Typography,
1010
} from '@strapi/design-system'
11+
import { useI18n } from '../../Hooks/useI18n'
1112
import { CheckPermissions } from '@strapi/helper-plugin'
1213
import { PERMISSIONS } from '../../constants'
1314

@@ -17,6 +18,7 @@ const CollectionColumn = ({
1718
addCollection,
1819
updateCollection,
1920
}) => {
21+
const { i18n } = useI18n()
2022
return (
2123
<Tr key={entry.contentType}>
2224
<CheckPermissions permissions={PERMISSIONS.createAndDelete}>
@@ -39,13 +41,17 @@ const CollectionColumn = ({
3941
{/* // IN MEILISEARCH */}
4042
<Td>
4143
<Typography textColor="neutral800">
42-
{entry.indexed ? 'Yes' : 'No'}
44+
{entry.indexed
45+
? i18n('plugin.table.td.yes', 'Yes')
46+
: i18n('plugin.table.td.no', 'No')}
4347
</Typography>
4448
</Td>
4549
{/* // INDEXING */}
4650
<Td>
4751
<Typography textColor="neutral800">
48-
{entry.isIndexing ? 'Yes' : 'No'}
52+
{entry.isIndexing
53+
? i18n('plugin.table.td.yes', 'Yes')
54+
: i18n('plugin.table.td.no', 'No')}
4955
</Typography>
5056
</Td>
5157
{/* // INDEX NAME */}
@@ -74,7 +80,7 @@ const CollectionColumn = ({
7480
size="S"
7581
variant="secondary"
7682
>
77-
Update
83+
{i18n('plugin.update', 'Update')}
7884
</Button>
7985
)}
8086
</Box>

admin/src/containers/Collection/CollectionTable.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import CollectionTableHeader from './CollectionTableHeader'
55
import CollectionColumn from './CollectionColumn'
66
import useCollection from '../../Hooks/useCollection'
77
import pluginId from '../../pluginId'
8+
import { useI18n } from '../../Hooks/useI18n'
89

910
const Collection = () => {
1011
const {
@@ -19,6 +20,8 @@ const Collection = () => {
1920
useAutoReloadOverlayBlocker()
2021
const [reload, setReload] = useState(false)
2122

23+
const { i18n } = useI18n()
24+
2225
const ROW_COUNT = 6
2326
const COL_COUNT = 10
2427

@@ -65,7 +68,9 @@ const Collection = () => {
6568
</Table>
6669
{reloadNeeded && (
6770
<Box padding={5}>
68-
<Button onClick={() => setReload(true)}>Reload server</Button>
71+
<Button onClick={() => setReload(true)}>
72+
{i18n('plugin.reload-server', 'Reload server')}
73+
</Button>
6974
</Box>
7075
)}
7176
</Box>

admin/src/containers/Collection/CollectionTableHeader.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import {
66
Typography,
77
VisuallyHidden,
88
} from '@strapi/design-system'
9+
import { useI18n } from '../../Hooks/useI18n'
910
import { CheckPermissions } from '@strapi/helper-plugin'
1011
import { PERMISSIONS } from '../../constants'
1112

1213
const CollectionTableHeader = () => {
14+
const { i18n } = useI18n()
1315
return (
1416
<Thead>
1517
<Tr>
@@ -19,22 +21,34 @@ const CollectionTableHeader = () => {
1921
</Th>
2022
</CheckPermissions>
2123
<Th>
22-
<Typography variant="sigma">NAME</Typography>
24+
<Typography variant="sigma">
25+
{i18n('plugin.table.header.name', 'NAME')}
26+
</Typography>
2327
</Th>
2428
<Th>
25-
<Typography variant="sigma">IN MEILISEARCH ?</Typography>
29+
<Typography variant="sigma">
30+
{i18n('plugin.table.header.in-meilisearch', 'IN MEILISEARCH ?')}
31+
</Typography>
2632
</Th>
2733
<Th>
28-
<Typography variant="sigma">INDEXING ?</Typography>
34+
<Typography variant="sigma">
35+
{i18n('plugin.table.header.indexing', 'INDEXING ?')}
36+
</Typography>
2937
</Th>
3038
<Th>
31-
<Typography variant="sigma">INDEX NAME</Typography>
39+
<Typography variant="sigma">
40+
{i18n('plugin.table.header.index-name', 'INDEX NAME')}
41+
</Typography>
3242
</Th>
3343
<Th>
34-
<Typography variant="sigma">DOCUMENTS</Typography>
44+
<Typography variant="sigma">
45+
{i18n('plugin.table.header.documents', 'DOCUMENTS')}
46+
</Typography>
3547
</Th>
3648
<Th>
37-
<Typography variant="sigma">HOOKS</Typography>
49+
<Typography variant="sigma">
50+
{i18n('plugin.table.header.hooks', 'HOOKS')}
51+
</Typography>
3852
</Th>
3953
<CheckPermissions permissions={PERMISSIONS.update}>
4054
<Th>

admin/src/containers/PluginHeader/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import React, { memo } from 'react'
22
import { Box, Link, BaseHeaderLayout } from '@strapi/design-system'
33
import { ArrowLeft } from '@strapi/icons'
4+
import { useI18n } from '../../Hooks/useI18n'
45

56
const PluginHeader = () => {
7+
const { i18n } = useI18n()
8+
69
return (
710
<Box background="neutral100">
811
<BaseHeaderLayout
912
navigationAction={
1013
<Link startIcon={<ArrowLeft />} to="/">
11-
Go back
14+
{i18n('plugin.go-back', 'Go Back')}
1215
</Link>
1316
}
14-
title="Meilisearch"
15-
subtitle="strapi-plugin-meilisearch"
17+
title={i18n('plugin.name', 'Meilisearch')}
18+
subtitle={i18n(
19+
'plugin.description',
20+
'Search in your content-types with the Meilisearch plugin',
21+
)}
1622
as="h2"
1723
/>
1824
</Box>

0 commit comments

Comments
 (0)