|
| 1 | +# Machine Translation |
| 2 | + |
| 3 | +This Kirby plugin allows you to automatically translate pages using the DeepL API. All field types are supported. |
| 4 | + |
| 5 | +**If you are using machine translation, you should inform your users of this fact.**\ |
| 6 | +For example, you could display a message like this (Text available via `t('tobiaswolf.machine-translation.info')`)\ |
| 7 | +*This page has been machine translated. Despite the high quality of machine translation, the translation may contain errors.* |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +### Download |
| 12 | + |
| 13 | +Download and copy this repository to `/site/plugins/machine-translation`. |
| 14 | + |
| 15 | +### Git submodule |
| 16 | + |
| 17 | +``` |
| 18 | +git submodule add https://github.com/tobiasfabian/machine-translation.git site/plugins/machine-translation |
| 19 | +``` |
| 20 | + |
| 21 | +### Composer |
| 22 | + |
| 23 | +``` |
| 24 | +composer require tobiasfabian/machine-translation |
| 25 | +``` |
| 26 | + |
| 27 | +## Requirements |
| 28 | + |
| 29 | +- Kirby 4.0+ |
| 30 | +- Authentication Key for DeepL API ([DeepL API for developers](https://www.deepl.com/de/pro#developer)) |
| 31 | + |
| 32 | +## Setup |
| 33 | + |
| 34 | +To use this plugin you have to set your personal *Authentication Key for DeepL API*. |
| 35 | +[API Access / Authentication – DeepL Documentation](https://www.deepl.com/docs-api/api-access/authentication/) |
| 36 | + |
| 37 | +```php |
| 38 | +// config/config.php |
| 39 | +return [ |
| 40 | + 'tobiaswolf.machine-translation.deepl.authKey' => '279a2e9d-83b3-c416-7e2d-f721593e42a0:fx', |
| 41 | +]; |
| 42 | +``` |
| 43 | + |
| 44 | +### DeepL Options |
| 45 | + |
| 46 | +You can set several options, provided by DeepL. Read more about the options in the [API Documentation](https://www.deepl.com/docs-api/translate-text/translate-text/) of DeepL. Each option has to be prefixed. `tobiaswolf.machine-translation.deepl.{Name}` |
| 47 | + |
| 48 | +| Name | Type | Default | Description |
| 49 | +|---------------------------|-----------|---------------|-------- |
| 50 | +| `authKey` **(required)** | `string` | `null` | You can find your Auth key on your account page. [DeepL Documentation](https://www.deepl.com/docs-api/api-access/authentication/) |
| 51 | +| `split_sentences` | `string` | `nonewlines` | Possible values are: `0` no splitting at all, whole input is treated as one sentence. `1` splits on punctuation and on newlines. `nonewlines` splits on punctuation only, ignoring newlines |
| 52 | +| `preserve_formatting` | `bool` | `false` | Sets whether the translation engine should respect the original formatting, even if it would usually correct some aspects. |
| 53 | +| `formality` | `string` | `default` | You can use one of these options: `default` (default), `more` for a more formal language, `less` for a more informal language, `prefer_more` for a more formal language if available, otherwise fallback to default formality, `prefer_less` for a more informal language if available, otherwise fallback to default formality. |
| 54 | +| `glossary_id` | `string` | `null` | Specify the glossary to use for the translation. The language pair of the glossary has to match the language pair of the request. |
| 55 | +| `tag_handling` | `string` | `html` | Sets which kind of tags should be handled. Options currently available: `xml`, `html` |
| 56 | +| `outline_detection` | `bool` | `true` | [API Documentation](https://www.deepl.com/docs-api/translate-text/translate-text/) |
| 57 | +| `non_splitting_tags` | `array` | `null` | List of XML or HTML tags. |
| 58 | +| `splitting_tags` | `array` | `null` | List of XML or HTML tags. |
| 59 | +| `ignore_tags` | `array` | `null` | List of XML or HTML tags. |
| 60 | + |
| 61 | + |
| 62 | +## Usage |
| 63 | + |
| 64 | +### Blueprint section |
| 65 | + |
| 66 | +Add the section `machine-translate` to your blueprint to get the interface to translate the page. |
| 67 | + |
| 68 | +```yaml |
| 69 | +sections: |
| 70 | + machineTranslate: |
| 71 | + type: machine-translate |
| 72 | +``` |
| 73 | + |
| 74 | +After the page is translated an object field `machineTranslated` with `date` and `showInfo` is saved to the translated page content. This can be used to detect machine translated pages and display a notice/warning on the frontend that the text is machine translated. You can add this object field to any fields section (optional). |
| 75 | + |
| 76 | +```yaml |
| 77 | +sections: |
| 78 | + fields: |
| 79 | + type: fields |
| 80 | + fields: |
| 81 | + machineTranslated: |
| 82 | + extends: fields/machineTranslated |
| 83 | +``` |
| 84 | + |
| 85 | +### API endpoint |
| 86 | + |
| 87 | +This plugin provides an API endpoint `/api/machine-translate/pages/(:any)` that can be used to translate an entire page. Read [Kirby’s API Guide](https://getkirby.com/docs/guide/api/introduction) to learn more about using the API. |
| 88 | + |
| 89 | +The endpoint allows `get` and `post` requests. The endpoint requires a `language` (target language) query. When making a `post` request, `sourceLang` and `forceOverwrite` can be added. By default `sourceLang` is the default language. If `forceOverwrite` is false or not specified, only fields where the target field does not exist or is empty will be translated. |
| 90 | + |
| 91 | +```JavaScript |
| 92 | +const pageId = 'test'; |
| 93 | +const targetLang = 'es'; |
| 94 | +const csrf = '…'; |
| 95 | +fetch(`/api/machine-translate/pages/{ pageId }?language={ targetLang }`, { |
| 96 | + method: 'post', |
| 97 | + body: JSON.stringify({ |
| 98 | + sourceLang: 'en', |
| 99 | + forceOverwrite: true, |
| 100 | + }), |
| 101 | + headers: { |
| 102 | + 'x-csrf': csrf, |
| 103 | + }, |
| 104 | +}); |
| 105 | +``` |
| 106 | + |
| 107 | +```php |
| 108 | +$pageId = 'test'; |
| 109 | + |
| 110 | +kirby()->api()->call('machine-translate/pages/' . $pageId, 'POST', [ |
| 111 | + 'query' => [ |
| 112 | + 'language' => 'en', |
| 113 | + ], |
| 114 | + 'body' => [ |
| 115 | + 'sourceLang' => 'de', |
| 116 | + 'forceOverwrite' => true, |
| 117 | + ], |
| 118 | +]); |
| 119 | +``` |
| 120 | + |
| 121 | +### Field method |
| 122 | + |
| 123 | +The field method `$field->translate($targetLang, $blueprintField)` translates the field value and returns the field with the translated value. All field types are supported. The type of field is specified via `$blueprintField['type']`. |
| 124 | + |
| 125 | +If you want to save the translated field, you can do this like this. |
| 126 | + |
| 127 | +```php |
| 128 | +$targetLang = 'de' |
| 129 | +$text = $page->text(); // e.g. Hello World |
| 130 | +$translatedText = $text->translate($targetLang); // returns the field with the translated text (Hallo Welt) |
| 131 | +$page->update([ |
| 132 | + 'text' => $translatedText, |
| 133 | +], $targetLang); |
| 134 | +``` |
| 135 | + |
| 136 | +### Page method |
| 137 | + |
| 138 | +The page method `$page->translate($targetLang, $sourceLang, $force)` allows you to translate the content of a page into a target language. By default already translated fields will not be overwritten. By setting `$force` to `true` all fields will be translated, existing fields will be overwritten. |
| 139 | + |
| 140 | +An object field `machineTranslated` with `date` and `showInfo` is added to the translated page content. This can be used to detect machine translated pages and display a notice/warning on the frontend that the text is machine translated. |
| 141 | + |
| 142 | +### Translate Class |
| 143 | + |
| 144 | +If you want to, you can use the static method of the `Translate` class. Use the `translate($text, $targetLang, $sourceLang)` method to translate text. Make sure you pass an array as the first parameter (you can translate multiple texts at once). You can omit the third parameter to have the source language automatically detected. |
| 145 | + |
| 146 | +```php |
| 147 | +use Tobiaswolf\MachineTranslation\Translate; |
| 148 | + |
| 149 | +$sourceTexts = ['Hello World', 'Greetings Earthlings']; |
| 150 | +$translatedTexts = Translate::translate($sourceTexts, 'es'); |
| 151 | + |
| 152 | +var_dump($translatedTexts); |
| 153 | +// array(2) { [0]=> array(2) { ["detected_source_language"]=> string(2) "EN" ["text"]=> string(10) "Hola Mundo" } [1]=> array(2) { ["detected_source_language"]=> string(2) "EN" ["text"]=> string(19) "Saludos terrícolas" } } |
| 154 | +``` |
| 155 | + |
| 156 | +### Cache |
| 157 | + |
| 158 | +Each request to DeepL is cached. This has the advantage that if the same text appears more than once on the website, a new request is not always made. This saves you *Character usage* of your DeepL plan. |
| 159 | + |
| 160 | +You can disable the cache via config. |
| 161 | + |
| 162 | +```php |
| 163 | +// config/config.php |
| 164 | +return [ |
| 165 | + 'cache.tobiaswolf.machine-translation.translate' => false, // default true |
| 166 | +] |
| 167 | +``` |
| 168 | + |
| 169 | +## License |
| 170 | + |
| 171 | +MIT |
| 172 | + |
| 173 | +## Credits |
| 174 | + |
| 175 | +- [Tobias Wolf](https://github.com/tobiasfabian) |
0 commit comments