You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Gets the plural by a number using [Intl.PluralRules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/PluralRules) API
142
148
143
149
#### Localize
144
-
-`formatDate(value: Date | number | string, options?: Intl.DateTimeFormatOptions, lang?: string, timeZone?: string)`
150
+
-`useFormatDate: () => (value: Date | number | string, options?: Intl.DateTimeFormatOptions, lang?: string, timeZone?: string)`
145
151
Formats a date using [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) API
146
152
147
-
-`relativeTime(value: number | string, unit: Intl.RelativeTimeFormatUnit, options?: Intl.RelativeTimeFormatOptions, lang?: string)`
Formats a relative time using [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat) API
149
155
150
-
-`formatNumber(value: number | string, options?: Intl.NumberFormatOptions, lang?: string, currency?: string)`
Returns the translation of language, region, script or currency display names using [Intl.DisplayNames](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames) API
Copy file name to clipboardExpand all lines: docs/inline.md
+16-6
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
# Qwik Speak Inline Vite plugin
2
2
3
-
> Inline Qwik Speak `$translate`, `$inlineTranslate` and `$plural` functions at compile time
3
+
> Inline Qwik Speak `useTranslate`, `inlineTranslate` and `usePNrlural` functions at compile time
4
4
5
5
## How it works
6
-
In development mode, using Qwik Speak translation happens _at runtime_: `assets` are loaded during SSR or on client, and the lookup also happens at runtime.
6
+
In development mode, translation happens _at runtime_: `assets` are loaded during SSR or on client, and the lookup also happens at runtime.
7
7
8
8
In production mode, `assets` are loaded only during SSR, and to get the translations on the client as well you have to use _Qwik Speak Inline_ Vite plugin.
9
9
@@ -80,11 +80,11 @@ Each contains only its own translation:
80
80
81
81
_dist/build/en-US/q-*.js_
82
82
```javascript
83
-
/* @__PURE__ */pr("h2", null, null, `Translate your Qwik apps into any language`, 1, null)
83
+
/* @__PURE__ */Nr("h2", null, null, `Translate your Qwik apps into any language`, 1, null)
84
84
```
85
85
_dist/build/it-IT/q-*.js_
86
86
```javascript
87
-
/* @__PURE__ */pr("h2", null, null, `Traduci le tue app Qwik in qualsiasi lingua`, 1, null)
87
+
/* @__PURE__ */Nr("h2", null, null, `Traduci le tue app Qwik in qualsiasi lingua`, 1, null)
88
88
```
89
89
90
90
At the end of the build, in root folder a `qwik-speak-inline.log` file is generated which contains:
@@ -95,7 +95,7 @@ At the end of the build, in root folder a `qwik-speak-inline.log` file is genera
95
95
> Note. Currently, only `json` files are supported as assets
96
96
97
97
## Qwik Speak Inline Vite plugin & runtime
98
-
When there are translations with dynamic keys or params, that is, they use an identifier, you can manage them in separate files, and add them to `runtimeAssets`:
98
+
When there are translations with dynamic keys or params, you have to use separate files, and add them to `runtimeAssets`:
We have added the Speak config and the implementation of the `loadTranslation$` function. `loadTranslation$` is a customizable function, with which you can load the translation files in the way you prefer.
48
+
We have added the Speak config and the implementation of the `loadTranslation$` function to load translation files.
49
+
50
+
> `loadTranslation$` is a customizable QRL function: you can load the translation files in the way you prefer
49
51
50
52
## Adding Qwik Speak
51
53
Just wrap Qwik City provider with `QwikSpeakProvider` component in `root.tsx` and pass it the configuration and the translation functions:
@@ -78,13 +80,17 @@ Finally we add an `index.tsx` with some translation:
Here we have used the `Speak` component to add scoped translations to the home page. This means that in addition to the `app` asset that comes with the configuration, the home page will also use the `home` asset. To distinguish them, `app` asset keys start with `app` and home asset keys start with `home`.
123
+
Here we have used the `Speak` component to add scoped translations to the `Home` component:
124
+
-`Home` component will use the `home` asset, in addition to the `app` asset that comes with the configuration
125
+
-`home` asset keys start with `home`
113
126
114
127
We are also providing default values for each translation: `key@@[default value]`.
115
128
116
-
> `Speak` component is a `Slot` component: because Qwik renders `Slot` components and direct children in isolation, translations are not immediately available in direct children, and we need to use a component for the `Home` page. It is generally not necessary to use more than one `Speak` component per page
129
+
> `Speak` component is a `Slot` component: because Qwik renders `Slot` components and direct children in isolation, translations are not immediately available in direct children, and we need to use a component for the `Home` page. It is not necessary to use more than one `Speak` component per page
130
+
131
+
## Head metas
132
+
You may have noticed, that in `index.tsx` we have provided the meta title and description with only the keys. Since the Qwik City `DocumentHead` is out of context, we need to do the translations directly in `router-head.tsx`:
We can resolve the locale to use in two ways: passing the `locale` parameter to the `QwikSpeakProvider` component, or assigning it to the `locale` handled by Qwik. Create `plugin.ts` in the root of the `src/routes` directory:
@@ -151,9 +192,11 @@ Now we want to change locale. Let's create a `ChangeLocale` component:
`app` asset and `home` asset for each language, initialized with the default values we provided.
209
253
254
+
_translations skipped due to dynamic keys_ are meta title and description keys, because those keys are passed as dynamic parameters. We have to add them manually in a new file that we will call `runtime`:
'runtime'// Translations with dynamic keys or parameters
283
+
]
284
+
```
285
+
210
286
We can translate the `it-IT` files, and run the app:
211
287
```shell
212
288
npm start
213
289
```
214
290
215
-
## Production: [Qwik Speak Inline Vite plugin](./inline.md)
291
+
See [Qwik Speak Extract](./extract.md) for more details.
292
+
293
+
## Production
216
294
In production mode, `assets` are loaded only during SSR, and to get the translations on the client as well it is required to inline the translations in chucks sent to the browser.
217
295
218
296
Add `qwikSpeakInline` Vite plugin in `vite.config.ts`:
@@ -253,10 +331,19 @@ export default function (opts: RenderToStreamOptions) {
253
331
});
254
332
}
255
333
```
256
-
257
334
Build the production app in preview mode:
258
335
```shell
259
336
npm run preview
260
337
```
338
+
Inspect the `qwik-speak-inline.log` file in root folder:
339
+
340
+
```
341
+
client: root.tsx
342
+
dynamic key: t(head.title) - skip
343
+
dynamic key: t(m.content) - skip
344
+
```
345
+
It contains the non-inlined dynamic keys that we added in the `runtime.json` file.
261
346
262
347
> The app will have the same behavior as you saw in dev mode, but now the translations are inlined as you can verify by inspecting the production files, reducing resource usage at runtime
348
+
349
+
See [Qwik Speak Inline Vite plugin](./inline.md) for more details.
@@ -57,7 +61,7 @@ test(`[Home Component]: Should render the component`, async () => {
57
61
});
58
62
```
59
63
60
-
Optionally, if you need to test the translated texts in different languages, you have to provide `loadTranslation$` to ensure translations are loaded in test environment and the locale to use:
64
+
Optionally, if you need to test the translated texts in different languages, you have to provide `loadTranslation$` to ensure translations are loaded in test environment, and the locale to use:
61
65
62
66
```tsx
63
67
test(`[Home Component]: Should render translated texts in Italian`, async () => {
0 commit comments