From a068eaf68d89f22680c23bfcbd02d7bbcbc0cf89 Mon Sep 17 00:00:00 2001 From: Jesiel Viana Date: Wed, 1 May 2024 13:06:12 -0300 Subject: [PATCH] =?UTF-8?q?add=20tradu=C3=A7=C3=A3o=20para=20os=20campos?= =?UTF-8?q?=20de=20busca=20avan=C3=A7ada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/locales/en/advanced.json | 30 +++++++++++++++ public/locales/en/common.json | 29 +------------- public/locales/pt-BR/advanced.json | 30 +++++++++++++++ public/locales/pt-BR/common.json | 29 +------------- src/components/AdvancedSearchBox.tsx | 14 ++++--- src/components/SearchSanitization.ts | 46 +++++++++++++++++++++++ src/components/indicators/query/Query.tsx | 3 +- src/pages/api/search.ts | 6 ++- src/pages/dashboards/index.tsx | 4 +- src/pages/publications.tsx | 2 +- 10 files changed, 129 insertions(+), 64 deletions(-) create mode 100644 public/locales/en/advanced.json create mode 100644 public/locales/pt-BR/advanced.json create mode 100644 src/components/SearchSanitization.ts diff --git a/public/locales/en/advanced.json b/public/locales/en/advanced.json new file mode 100644 index 0000000..258dd28 --- /dev/null +++ b/public/locales/en/advanced.json @@ -0,0 +1,30 @@ +{ + "all": "All fields", + "title_text": "Title", + "publicationDate": "Year", + "author.name": "Autor", + "language": "Language", + "type": "Type", + "orgunit.name": "Institution", + "keyword_text": "Keyword", + "name_text": "Name", + "status": "Status", + "leader.name": "Leader", + "member.name": "Member", + "country": "Country", + "state": "State", + "city": "City", + "issn": "ISSN", + "issnl": "ISSN-L", + "qualis": "Qualis", + "espacenetTitle_text": "Title", + "inventor.name": "Inventor", + "depositDate": "Deposit year", + "kindCode": "Kind Code", + "countryCode": "Country code", + "lattesId": "Lattes", + "orcid": "Orcid", + "releaseYear": "Release year", + "registrationCountry": "Registration country", + "platform": "Platform" +} diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 4c76458..c5b3efe 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -168,31 +168,6 @@ "For searches involving a large number of records, the export file may not be immediately available for download. In such cases, the download link for the file will be sent via email shortly.": "For searches involving a large number of records, the export file may not be immediately available for download. In such cases, the download link for the file will be sent via email shortly.", "Enter your email in the field below:": "Enter your email in the field below:", "Export processing. The download link will be sent to the e-mail address provided when the file is ready.": "Export processing. The download link will be sent to the e-mail address provided when the file is ready.", - "title_text": "Title", - "publicationDate": "Year", - "author.name": "Autor", - "language": "Language", - "type": "Type", - "orgunit.name": "Institution", - "keyword_text": "Keyword", - "name_text": "Name", - "status": "Status", - "leader.name": "Leader", - "member.name": "Member", - "country": "Country", - "state": "State", - "city": "City", - "issn": "ISSN", - "issnl": "ISSN-L", - "qualis": "Qualis", - "espacenetTitle_text": "Title", - "inventor.name": "Inventor", - "depositDate": "Deposit year", - "kindCode": "Kind Code", - "countryCode": "Country code", - "lattesId": "Lattes", - "orcid": "Orcid", - "releaseYear": "Release year", - "registrationCountry": "Registration country", - "platform": "Platform" + "Academic and/or research institutions": "Academic and/or research institutions", + "Active postgraduate programs evaluated by CAPES": "Active postgraduate programs evaluated by CAPES." } diff --git a/public/locales/pt-BR/advanced.json b/public/locales/pt-BR/advanced.json new file mode 100644 index 0000000..e3a930a --- /dev/null +++ b/public/locales/pt-BR/advanced.json @@ -0,0 +1,30 @@ +{ + "all": "Todos os campos", + "title_text": "Título", + "publicationDate": "Ano", + "author.name": "Autor", + "language": "Idioma", + "type": "Tipo", + "orgunit.name": "Organização", + "keyword_text": "Palavra-chave", + "name_text": "Nome", + "status": "Status", + "leader.name": "Líder", + "member.name": "Membro", + "country": "País", + "state": "Estado", + "city": "Cidade", + "issn": "ISSN", + "issnl": "ISSN-L", + "qualis": "Qualis", + "espacenetTitle_text": "Título", + "inventor.name": "Inventor", + "depositDate": "Ano de depóstito", + "kindCode": "Tipo", + "countryCode": "Código do país", + "lattesId": "Lattes", + "orcid": "Orcid", + "releaseYear": "Ano de lançamento", + "registrationCountry": "País de registro", + "platform": "Plataforma" +} diff --git a/public/locales/pt-BR/common.json b/public/locales/pt-BR/common.json index 8b90c30..c847dc3 100644 --- a/public/locales/pt-BR/common.json +++ b/public/locales/pt-BR/common.json @@ -168,31 +168,6 @@ "For searches involving a large number of records, the export file may not be immediately available for download. In such cases, the download link for the file will be sent via email shortly.": "Para buscas que envolvem um grande volume de registros, o arquivo de exportação pode não estar prontamente disponível para download. Nessas situações, o link para baixar o arquivo será enviado por e-mail em breve.", "Enter your email in the field below:": "Insira seu email no campo abaixo:", "Export processing. The download link will be sent to the e-mail address provided when the file is ready.": "Exportação em processamento. O link para download será enviado para o e-mail informado quando o arquivo estiver pronto.", - "title_text": "Título", - "publicationDate": "Ano", - "author.name": "Autor", - "language": "Idioma", - "type": "Tipo", - "orgunit.name": "Instituição", - "keyword_text": "Palavra-chave", - "name_text": "Nome", - "status": "Status", - "leader.name": "Líder", - "member.name": "Membro", - "country": "País", - "state": "Estado", - "city": "Cidade", - "issn": "ISSN", - "issnl": "ISSN-L", - "qualis": "Qualis", - "espacenetTitle_text": "Título", - "inventor.name": "Inventor", - "depositDate": "Ano de depóstito", - "kindCode": "Tipo", - "countryCode": "Código do país", - "lattesId": "Lattes", - "orcid": "Orcid", - "releaseYear": "Ano de lançamento", - "registrationCountry": "País de registro", - "platform": "Plataforma" + "Academic and/or research institutions": "Instituições acadêmicas e/ou de pesquisa", + "Active postgraduate programs evaluated by CAPES": "Programas de pós-graduação ativos avaliados pela CAPES" } diff --git a/src/components/AdvancedSearchBox.tsx b/src/components/AdvancedSearchBox.tsx index 6b22276..96cf0d6 100644 --- a/src/components/AdvancedSearchBox.tsx +++ b/src/components/AdvancedSearchBox.tsx @@ -3,8 +3,10 @@ import { SearchBox, withSearch } from '@elastic/react-search-ui'; import { SearchContextState } from '@elastic/react-search-ui/lib/esm/withSearch'; import { useTranslation } from 'next-i18next'; + import { useEffect, useState } from 'react'; import { IoAdd, IoArrowUndoOutline, IoClose, IoSearch } from 'react-icons/io5'; + import ElasticSearchStatsService from '../services/ElasticSearchStatsService'; import styles from '../styles/AdvancedSearch.module.css'; import { QueryItem } from '../types/Entities'; @@ -23,12 +25,14 @@ const AdvancedSearchBox = ({ fieldNames, toogleAdvancedConfig, }: CustomSearchBoxProps) => { - const { t } = useTranslation('common'); + const { t } = useTranslation(['advanced', 'common']); const [docsCount, setDocsCount] = useState(localStorage.getItem(indexName)); const [query, setQuery] = useState(searchTerm); const [queryField, setQueryField] = useState('all'); const [inputs, setInputs] = useState([]); + fieldNames = fieldNames.map((field) => t(field)); + const addInput = () => { setInputs([...inputs, { value: '', field: 'all', operator: 'AND' }]); }; @@ -104,10 +108,10 @@ const AdvancedSearchBox = ({ > @@ -156,10 +160,10 @@ const AdvancedSearchBox = ({ onChange={(e) => handleChange({ field: e.target.value }, indice)} className="form-select" > - + {fieldNames.map((field) => ( ))} diff --git a/src/components/SearchSanitization.ts b/src/components/SearchSanitization.ts new file mode 100644 index 0000000..cbc46d4 --- /dev/null +++ b/src/components/SearchSanitization.ts @@ -0,0 +1,46 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment */ +// @ts-ignore + +import en from 'public/locales/en/advanced.json'; +// @ts-ignore +import ptBr from 'public/locales/pt-BR/advanced.json'; + +export function findPropertyByValue(value: string) { + for (const property in ptBr) { + //@ts-ignore + if (ptBr[property] === value) { + return property; + } + } + for (const property in en) { + //@ts-ignore + if (en[property] === value) { + return property; + } + } + return value; +} + +export function untranslatedFieldsNames(fullQuery: string) { + const regex = /\(([^:)]+):/g; // pega os nomes dos campos, palavra entre '(' e ':'. + + const names = []; + let name; + while ((name = regex.exec(fullQuery))) { + names.push(name[1]); + } + + const map = new Map(); + names.forEach((name) => { + console.log('name', name); + const untranslated = findPropertyByValue(name); + console.log('untranslated', untranslated); + map.set(name, untranslated); + }); + + for (const [key, value] of map) { + fullQuery = fullQuery.replaceAll(key, value); + } + console.log('fullQuery', fullQuery); + return fullQuery; +} diff --git a/src/components/indicators/query/Query.tsx b/src/components/indicators/query/Query.tsx index 1d97992..82fa4d7 100644 --- a/src/components/indicators/query/Query.tsx +++ b/src/components/indicators/query/Query.tsx @@ -2,6 +2,7 @@ import { QueryDslOperator, QueryDslQueryContainer } from '@elastic/elasticsearch/lib/api/types'; import { Filter, FilterValue } from '@elastic/search-ui'; import QueryFormat from '../../../services/QueryFormat'; +import { untranslatedFieldsNames } from '../../SearchSanitization'; type QueryProps = { size: number; indicadorName: string; @@ -50,7 +51,7 @@ export function formatedQuery( ): QueryDslQueryContainer { let query: QueryDslQueryContainer = {}; if (searchTerm.indexOf('(') >= 0) { - query = new QueryFormat().toElasticsearch(searchTerm, fields); + query = new QueryFormat().toElasticsearch(untranslatedFieldsNames(searchTerm), fields); } else { query = { bool: { diff --git a/src/pages/api/search.ts b/src/pages/api/search.ts index abec3a9..229161b 100644 --- a/src/pages/api/search.ts +++ b/src/pages/api/search.ts @@ -2,6 +2,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import ElasticsearchAPIConnector from '@elastic/search-ui-elasticsearch-connector'; import { NextApiRequest, NextApiResponse } from 'next'; +import { untranslatedFieldsNames } from '../../components/SearchSanitization'; import logger from '../../services/Logger'; import QueryFormat from '../../services/QueryFormat'; // https://docs.elastic.co/search-ui/api/connectors/elasticsearch#customise-the-elasticsearch-request-body @@ -23,7 +24,10 @@ function builConnector(index: string) { // @ts-ignore requestState.searchTerm = `(all:${requestState.searchTerm})`; } - const fullQuery = new QueryFormat().toElasticsearch(requestState.searchTerm, Object.keys(searchFields)); + console.log('requestState.searchTerm', requestState.searchTerm); + const query = untranslatedFieldsNames(requestState.searchTerm); + console.log('query', query); + const fullQuery = new QueryFormat().toElasticsearch(query, Object.keys(searchFields)); requestBody.query = fullQuery; return requestBody; } diff --git a/src/pages/dashboards/index.tsx b/src/pages/dashboards/index.tsx index e13f5c0..b2e8544 100644 --- a/src/pages/dashboards/index.tsx +++ b/src/pages/dashboards/index.tsx @@ -134,7 +134,7 @@ export default function Dashboards() { ícone de instituições
-

{t('Institutions')}

+

{t('Academic and/or research institutions')}

@@ -151,7 +151,7 @@ export default function Dashboards() { />
-

{t('Programs')}

+

{t('Active postgraduate programs evaluated by CAPES')}

diff --git a/src/pages/publications.tsx b/src/pages/publications.tsx index 6b3a8fa..247e830 100644 --- a/src/pages/publications.tsx +++ b/src/pages/publications.tsx @@ -35,7 +35,7 @@ type Props = { }; export const getServerSideProps: GetServerSideProps = async ({ locale }) => ({ props: { - ...(await serverSideTranslations(locale ?? 'en', ['common', 'navbar'])), + ...(await serverSideTranslations(locale ?? 'en', ['common', 'navbar', 'advanced'])), }, });