2021-10-17 18:26:00 +02:00
|
|
|
---
|
2022-09-29 23:43:00 +02:00
|
|
|
title: insertText
|
2021-10-17 18:26:00 +02:00
|
|
|
---
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-29 23:43:00 +02:00
|
|
|
The `insertText` lifecycle hook is called when text is about to be inserted
|
|
|
|
during rendering.
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-29 23:43:00 +02:00
|
|
|
It is typically used for translation, as is the case
|
|
|
|
in [our i18n plugin](/reference/plugins/i18n/).
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-29 23:43:00 +02:00
|
|
|
## Signature
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-29 23:43:00 +02:00
|
|
|
```js
|
|
|
|
string hook(string locale='en', string text)
|
|
|
|
```
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-29 23:43:00 +02:00
|
|
|
## Example
|
|
|
|
|
|
|
|
```js
|
|
|
|
// Let' get LOUD by turning everything into UPPERCASE
|
|
|
|
pattern.on(
|
|
|
|
'insertText',
|
|
|
|
(locale, text) => text.toUpperCase()
|
|
|
|
)
|
|
|
|
```
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-09-29 23:43:00 +02:00
|
|
|
## Notes
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-02-20 14:35:50 +01:00
|
|
|
When we say that _this hook is called when text is about to be inserted_, that is a simplified view.
|
2021-08-25 16:09:31 +02:00
|
|
|
In reality, this hook is called:
|
|
|
|
|
2022-09-29 23:43:00 +02:00
|
|
|
- For every string of text added to a given Point or Path
|
2022-02-20 14:44:38 +01:00
|
|
|
- For the combined result of these values, joined together with spaces
|
2021-08-25 16:09:31 +02:00
|
|
|
|
|
|
|
Let's use an example to clarify things:
|
|
|
|
|
|
|
|
```js
|
|
|
|
points.example
|
2022-09-29 23:43:00 +02:00
|
|
|
.addText("seamAllowance")
|
2022-12-26 07:00:16 -08:00
|
|
|
.addText(": 1 cm")
|
2021-08-25 16:09:31 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
For the example point above, the `insertText` hook will end up being called 3 times:
|
|
|
|
|
2022-02-20 14:44:38 +01:00
|
|
|
- First it will pass `seamAllowance` to the plugin
|
2022-12-26 07:00:16 -08:00
|
|
|
- Then it will pass `: 1 cm` to the plugin
|
|
|
|
- Finally it will pass `seamAllowance : 1 cm` to the plugin
|
2021-08-25 16:09:31 +02:00
|
|
|
|
2022-12-26 07:00:16 -08:00
|
|
|
Having the `insertText` hook only run once with `Seam allowance: 1 cm` would be problematic because
|
2021-08-25 16:09:31 +02:00
|
|
|
the seam allowance may differ, or perhaps we're using imperial units, and so on.
|
|
|
|
|
|
|
|
Instead, you can (and should) divide your text into chunks that need translating, and chunks that do not.
|
|
|
|
|
2022-02-19 08:04:25 +01:00
|
|
|
This is also why we're not inserting **Seam allowance** but rather **seamAllowance**;
|
2021-08-25 16:09:31 +02:00
|
|
|
It is merely a key to indicate what translation we want to replace this text with.
|