This plugin adds translation capabilities to the Jodit editor, allowing users to translate selected text directly within the editor. It provides a toolbar button for translation and supports different translation providers.
Copy<!doctype html> <html> <head> <title>Translate Editor</title> <meta charset="utf-8" /> <!-- css --> <link rel="stylesheet" href="./node_modules/jodit-pro/build/jodit.css" /> <link rel="stylesheet" href="./node_modules/jodit-pro/build/plugins/translate/translate.css" /> </head> <body> <!-- element --> <textarea id="entry">...</textarea> <!-- js --> <script src="./node_modules/jodit-pro/build/jodit.js"></script> <script src="./node_modules/jodit-pro/build/config.js"></script> <script src="./node_modules/jodit-pro/build/plugins/translate/translate.js"></script> <!-- call --> <script> Jodit.make('#entry', { extraPlugins: ['translate'], translate: { provider: 'google', googleProviderOptions: { key: '{YOUR_GOOGLE_API_KEY}' } } }); </script> </body> </html>
'google' | ITranslateProviderFactory
'google'
true
Translation engine provider. You can use:
'google'
- Google Translate APIITranslateProviderFactory
interface (see Custom Translate Provider example)CopyJodit.make('#editor', { translate: { provider: 'google' } });
string
''
(empty string, which means auto-detection)false
Default source language for translation. Use language ISO-639-1 codes. If not specified, the plugin will try to auto-detect the source language.
CopyJodit.make('#editor', { translate: { defaultSourceLang: 'en' } });
string
'de'
true
Default target language for translation. Use language ISO-639-1 codes.
CopyJodit.make('#editor', { translate: { defaultTargetLang: 'fr' } });
object
Copy{ translateSelection: ['ctrl+shift+o', 'cmd+shift+o'], translateOptions: ['ctrl+shift+p', 'cmd+shift+p'] }
Keyboard shortcuts for translation functions.
CopyJodit.make('#editor', { translate: { hotkeys: { translateSelection: ['ctrl+alt+t'], translateOptions: ['ctrl+alt+o'] } } });
object
Copy{ url: 'https://translation.googleapis.com/language/translate/v2', key: '' }
Options for Google Translate API.
string
''
true
(when using Google Translate provider)API key for using Google Translate engine. Get API key
CopyJodit.make('#editor', { translate: { provider: 'google', googleProviderOptions: { key: 'YOUR_GOOGLE_API_KEY' } } });
string
'https://translation.googleapis.com/language/translate/v2'
false
URL for Google Translate API. You typically don't need to change this unless you're using a proxy or a different endpoint.
CopyJodit.make('#editor', { translate: { provider: 'google', googleProviderOptions: { url: 'https://your-proxy.com/google-translate', key: 'YOUR_GOOGLE_API_KEY' } } });
You can create your own translation provider by implementing the ITranslateProviderFactory
interface. This function should return an object with two methods:
translate(text, from, to)
- Translates text from one language to anothersupportedLanguages()
- Returns a list of supported languagesCopyimport { JoditPro } from 'jodit-pro'; JoditPro.make('#editor', { language: 'en', buttons: ['translate.translate'], extraPlugins: ['translate'], translate: { provider: (jodit) => { return { supportedLanguages() { return Promise.resolve({ langs: { English: 'en', Russian: 'ru', German: 'de' } }); }, translate(text, from, to) { return fetch( 'https://your-translate-service.com/?text=' + encodeURIComponent(text) + '&from=' + encodeURIComponent(from || jodit.o.language) + '&to=' + encodeURIComponent(to) ) .then((resp) => resp.json()) .then((data) => { return { text: data.translation.text }; }); } }; } } });
Copyimport { JoditPro } from 'jodit-pro'; JoditPro.make('#editor', { language: 'en', buttons: ['translate.translate'], extraPlugins: ['translate'], translate: { provider: 'google', defaultSourceLang: 'en', defaultTargetLang: 'fr', googleProviderOptions: { key: 'YOUR_GOOGLE_API_KEY' } } });
Copyimport { JoditPro } from 'jodit-pro'; JoditPro.make('#editor', { language: 'en', buttons: ['translate.translate'], extraPlugins: ['translate'], translate: { provider: (jodit) => { return { supportedLanguages() { // Return only the languages your service supports return Promise.resolve({ langs: { English: 'en', Spanish: 'es', French: 'fr', German: 'de', Italian: 'it' } }); }, translate(text, from, to) { // Use your own translation API return fetch(`https://api.your-translation-service.com/translate`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_API_KEY' }, body: JSON.stringify({ text, source_language: from || 'auto', target_language: to }) }) .then(response => response.json()) .then(data => { return { text: data.translated_text }; }); } }; }, defaultTargetLang: 'es' } });
Copyimport { JoditPro } from 'jodit-pro'; JoditPro.make('#editor', { buttons: ['translate.translate'], extraPlugins: ['translate'], translate: { provider: 'google', googleProviderOptions: { key: 'YOUR_GOOGLE_API_KEY' }, hotkeys: { translateSelection: ['alt+t'], translateOptions: ['alt+o'] } } });
This plugin is particularly useful for multilingual content creation and editing, allowing users to quickly translate portions of text without leaving the editor.