Skip to content

Commit

Permalink
🌐(frontend) add localization to editor
Browse files Browse the repository at this point in the history
Currently, when you change language the editor does not change. So we add this
functionality
  • Loading branch information
PanchoutNathan committed Oct 22, 2024
1 parent 6012085 commit ab10fcd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to
## Added

- 📝Contributing.md #352
- 🌐(frontend) add localization to editor #268

## Fixed

Expand Down
35 changes: 35 additions & 0 deletions src/frontend/apps/e2e/__tests__/app-impress/doc-editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ test.beforeEach(async ({ page }) => {
});

test.describe('Doc Editor', () => {
test('it check translations of the slash menu when changing language', async ({
page,
browserName,
}) => {
await createDoc(page, 'doc-toolbar', browserName, 1);

const header = page.locator('header').first();
const editor = page.locator('.ProseMirror');
// Trigger slash menu to show english menu
await editor.click();
await editor.fill('/');
await expect(page.getByText('Headings', { exact: true })).toBeVisible();
await header.click();
await expect(page.getByText('Headings', { exact: true })).toBeHidden();

// Reset menu
await editor.click();
await editor.fill('');

// Change language to French
await header.click();
await header.getByRole('combobox').getByText('English').click();
await header.getByRole('option', { name: 'Français' }).click();
await expect(
header.getByRole('combobox').getByText('Français'),
).toBeVisible();

// Trigger slash menu to show french menu
await editor.click();
await editor.fill('/');
await expect(page.getByText('Titres', { exact: true })).toBeVisible();
await header.click();
await expect(page.getByText('Titres', { exact: true })).toBeHidden();
});

test('it checks default toolbar buttons are displayed', async ({
page,
browserName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { BlockNoteEditor as BlockNoteEditorCore } from '@blocknote/core';
import { locales } from '@blocknote/core';
import '@blocknote/core/fonts/inter.css';
import { BlockNoteView } from '@blocknote/mantine';
import '@blocknote/mantine/style.css';
import { useCreateBlockNote } from '@blocknote/react';
import { HocuspocusProvider } from '@hocuspocus/provider';
import React, { useCallback, useEffect, useMemo } from 'react';
import React, { useCallback, useEffect } from 'react';
import { useTranslation } from 'react-i18next';

import { Box, TextErrors } from '@/components';
import { mediaUrl } from '@/core';
Expand Down Expand Up @@ -113,6 +115,8 @@ export const BlockNoteContent = ({
error: errorAttachment,
} = useCreateDocAttachment();
const { setHeadings, resetHeadings } = useHeadingStore();
const { i18n } = useTranslation();
const lang = i18n.language;

const uploadFile = useCallback(
async (file: File) => {
Expand All @@ -129,12 +133,8 @@ export const BlockNoteContent = ({
[createDocAttachment, doc.id],
);

const editor = useMemo(() => {
if (storedEditor) {
return storedEditor;
}

return BlockNoteEditorCore.create({
const editor = useCreateBlockNote(
{
collaboration: {
provider,
fragment: provider.document.getXmlFragment('document-store'),
Expand All @@ -143,9 +143,11 @@ export const BlockNoteContent = ({
color: randomColor(),
},
},
dictionary: locales[lang as keyof typeof locales],
uploadFile,
});
}, [provider, storedEditor, uploadFile, userData?.email]);
},
[provider, uploadFile, userData?.email, lang],
);

useEffect(() => {
setStore(storeId, { editor });
Expand Down Expand Up @@ -176,7 +178,7 @@ export const BlockNoteContent = ({
)}

<BlockNoteView
editor={editor}
editor={storedEditor ?? editor}
formattingToolbar={false}
editable={!readOnly}
theme="light"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@blocknote/react';
import { forEach, isArray } from 'lodash';
import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next';

type Block = {
type: string;
Expand Down Expand Up @@ -42,6 +43,7 @@ export function MarkdownButton() {
const editor = useBlockNoteEditor();
const Components = useComponentsContext();
const selectedBlocks = useSelectedBlocks(editor);
const { t } = useTranslation();

const handleConvertMarkdown = () => {
const blocks = editor.getSelection()?.blocks;
Expand Down Expand Up @@ -75,7 +77,7 @@ export function MarkdownButton() {

return (
<Components.FormattingToolbar.Button
mainTooltip="Convert Markdown"
mainTooltip={t('Convert Markdown')}
onClick={handleConvertMarkdown}
>
M
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/apps/impress/src/i18n/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
"accessibility-dinum-services": "<strong>DINUM</strong> s'engage à rendre accessibles ses services numériques, conformément à l'article 47 de la loi n° 2005-102 du 11 février 2005.",
"accessibility-form-defenseurdesdroits": "Écrire un message au<1>DĂ©fenseur des droits</1>",
"accessibility-not-audit": "<strong>docs.numerique.gouv.fr</strong> n'est pas en conformité avec le RGAA 4.1. Le site n'a <strong>pas encore été audité.</strong>",
"you have reported to the website manager a lack of accessibility that prevents you from accessing content or one of the services of the portal and you have not received a satisfactory response.": "vous avez signalĂ© au responsable du site internet un dĂ©faut d'accessibilitĂ© qui vous empĂȘche d'accĂ©der Ă  un contenu ou Ă  un des services du portail et vous n'avez pas obtenu de rĂ©ponse satisfaisante."
"you have reported to the website manager a lack of accessibility that prevents you from accessing content or one of the services of the portal and you have not received a satisfactory response.": "vous avez signalĂ© au responsable du site internet un dĂ©faut d'accessibilitĂ© qui vous empĂȘche d'accĂ©der Ă  un contenu ou Ă  un des services du portail et vous n'avez pas obtenu de rĂ©ponse satisfaisante.",
}
}
}

0 comments on commit ab10fcd

Please sign in to comment.