Skip to content

Commit 776eef3

Browse files
committed
Update docs
1 parent cf00c04 commit 776eef3

File tree

9 files changed

+77
-162
lines changed

9 files changed

+77
-162
lines changed

README.md

+21-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ export default component$(() => {
3535
);
3636
});
3737
```
38+
You can also avoid handling the keys, and only pass the default values by enabling the automatic key generation option:
39+
```tsx
40+
import { inlineTranslate } from 'qwik-speak';
41+
42+
export default component$(() => {
43+
const t = inlineTranslate();
44+
45+
return (
46+
<>
47+
<h1>{t('Qwik Speak')}</h1> {/* Qwik Speak */}
48+
<p>{t('Hi! I am {{name}}', { name: 'Qwik Speak' })}</p> {/* Hi! I am Qwik Speak */}
49+
</>
50+
);
51+
});
52+
```
53+
See [Translate](./docs/translate.md) and [Automatic key generation](./docs/translate.md#automatic-key-generation) for more details.
54+
3855
### Getting dates, relative time & numbers
3956
```tsx
4057
import { useFormatDate, useRelativeTime, useFormatNumber } from 'qwik-speak';
@@ -53,6 +70,7 @@ export default component$(() => {
5370
);
5471
});
5572
```
73+
See [Localize](./docs/translate.md#localize) for more details.
5674

5775
## Static translations
5876
Translation are loaded and inlined in chunks sent to the browser during the build.
@@ -65,9 +83,9 @@ To extract translations directly from the components, a command is available tha
6583
See [Qwik Speak Extract](./docs/extract.md) for more information on how to use it.
6684

6785
## Automatic translation
68-
To automatically translate files, an external command is available that uses OpenAI GPT Chat Completions API.
69-
70-
See [GPT Translate JSON](./docs/gpt-translate-json.md) for more information on how to use it.
86+
To automatically translate files, the following external packages are available:
87+
- [GPT Translate JSON](https://github.com/robisim74/gpt-translate-json)
88+
- [Qwik Speak DeepL](https://www.npmjs.com/package/@tegonal/qwik-speak-deepl)
7189

7290
## Speak context
7391
```mermaid

SUMMARY.md

-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,3 @@
1515

1616
* [Qwik Speak Extract](docs/extract.md)
1717
* [Qwik Speak Inline Vite plugin](docs/inline.md)
18-
19-
## External Tools​
20-
* [GPT Translate JSON](docs/gpt-translate-json.md)

docs/extract.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Available options:
5050
- `format` The format of the translation files. Default to `'json'`
5151
- `filename` Filename for not scoped translations. Default is `'app'`
5252
- `fallback` Optional function to implement a fallback strategy
53+
- `autoKeys` Automatically handle keys for each string. Default is false
5354
- `keySeparator` Separator of nested keys. Default is `'.'`
5455
- `keyValueSeparator` Key-value separator. Default is `'@@'`
5556

docs/gpt-translate-json.md

-151
This file was deleted.

docs/inline.md

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Available options:
8181
- `assetsPath` Path to translation files: `[basePath]/[assetsPath]/[lang]/*.json`. Default to `'i18n'`
8282
- `outDir` The build output directory. Default to `'dist'`
8383
- `loadAssets` Optional function to load asset by lang
84+
- `autoKeys` Automatically handle keys for each string. Default is false
8485
- `keySeparator` Separator of nested keys. Default is `'.'`
8586
- `keyValueSeparator` Key-value separator. Default is `'@@'`
8687

docs/translate.md

+50-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ When you run the extraction tool, it creates the Intl API plural rules for each
9494
}
9595
}
9696
```
97-
There is no default value for `inlinePlural` function, so you must add the translation in each language, keeping in mind that the counter is optionally interpolated with the `value` parameter:
97+
It is possible to set the default value passing a _valid stringified_ json, keeping in mind that the counter is optionally interpolated with the `value` parameter:
98+
```tsx
99+
p(1, 'devs@@{"one": "{{ value }} software developer","other": "{{ value }} software developers"}')
100+
```
101+
Will result in:
98102
```json
99103
{
100104
"devs": {
@@ -165,6 +169,51 @@ export default component$(() => {
165169
```
166170
You can also extract the language directly into the function, through the request (cookies, params), instead of passing it as a parameter.
167171

172+
## Automatic key generation
173+
If you don't want to handle the keys inside the translation functions, but only the default values, you can enable automatic key generation:
174+
- Extraction tool: add `--autoKeys=true` to the script
175+
- Inline Vite plugin: add `autoKeys: true` to the options
176+
177+
> Note. You can enable this option, even if you use the syntax `key@@[default value]`.
178+
179+
If you enable this option, you can only pass the default value to the translation functions:
180+
```tsx
181+
export default component$(() => {
182+
const t = inlineTranslate();
183+
const p = inlinePlural();
184+
185+
return (
186+
<>
187+
<h1>{t('app.title@@{{name}} demo', { name: 'Qwik Speak' })}</h1>
188+
189+
<h3>{t('New strings without existing keys')}</h3>
190+
<p class="counter">{p(
191+
1,
192+
'{"one": "{{ value }} {{ color }} zebra","other": "{{ value }} {{ color }} zebras"}',
193+
{
194+
color: t('black and white')
195+
}
196+
)}</p>
197+
</>
198+
);
199+
});
200+
```
201+
If you run the extractor, you will get json files like this:
202+
```json
203+
{
204+
"app": {
205+
"title": "Qwik Speak demo"
206+
},
207+
"autoKey_3c909eb27a10640be9495cff142f601c": {
208+
"one": "{{ value }} {{ color }} zebra",
209+
"other": "{{ value }} {{ color }} zebras"
210+
},
211+
"autoKey_8e4c0598319b3b04541df2fc36cb6fc5": "New strings without existing keys",
212+
"autoKey_cbe370e60f10f92d4dd8b3e9c267b1fa": "black and white"
213+
}
214+
```
215+
Then the Inline plugin will manage the self-assigned keys.
216+
168217
# Localize
169218
## useFormatDate
170219
`useFormatDate` returns a functions that uses [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) API to format dates:

packages/qwik-speak/src/inline-plural.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export const inlinePlural = (): InlinePluralFn => {
4242
if (key) {
4343
[key, defaultValues] = separateKeyValue(key, config.keyValueSeparator);
4444

45-
if (!defaultValues && /^[[{].*[\]}]$/.test(key)) {
45+
if (!defaultValues && /^{.*}$/.test(key)) {
4646
defaultValues = key;
4747
key = undefined;
4848
}

packages/qwik-speak/tools/extract/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export async function qwikSpeakExtract(options: QwikSpeakExtractOptions) {
174174
if (key) {
175175
[key, defaultValue] = separateKeyValue(key, resolvedOptions.keyValueSeparator);
176176

177-
if (!defaultValue && /^[[{].*[\]}]$/.test(key)) {
177+
if (!defaultValue && /^{.*}$/.test(key)) {
178178
defaultValue = key;
179179
key = undefined;
180180
}

packages/qwik-speak/tools/inline/plugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ export function addKeyToPlural(
383383
if (keyOrDefaultValue) {
384384
[key, defaultValue] = separateKeyValue(keyOrDefaultValue, opts.keyValueSeparator);
385385

386-
if (!defaultValue && /^[[{].*[\]}]$/.test(key)) {
386+
if (!defaultValue && /^{.*}$/.test(key)) {
387387
defaultValue = key;
388388
key = undefined;
389389
}
@@ -637,7 +637,7 @@ export function transpilePluralFn(
637637
if (key) {
638638
[key, defaultValues] = separateKeyValue(key, opts.keyValueSeparator);
639639

640-
if (!defaultValues && /^[[{].*[\]}]$/.test(key)) {
640+
if (!defaultValues && /^{.*}$/.test(key)) {
641641
defaultValues = key;
642642
key = undefined;
643643
}

0 commit comments

Comments
 (0)