Skip to content

Commit 1623a10

Browse files
authored
feat: tolgee SDK v6 (#793)
1 parent c5a32e9 commit 1623a10

File tree

100 files changed

+5980
-211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+5980
-211
lines changed

blog/2025-01-06-sdk-v6.mdx

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
slug: sdk-v6
3+
title: 'Tolgee SDK Version 6 released!'
4+
description: "Supercharge your localization with Tolgee SDK V6! Simplified Next.js integration, smarter translation fetching, and minimal breaking changes for a seamless transition!"
5+
image: /img/blog/sdk-v6/SDK6-light.webp
6+
authors: [sgranat]
7+
tags: ['tolgee', 'sdk', 'release']
8+
---
9+
10+
import ThemedImage from '@theme/ThemedImage';
11+
import useBaseUrl from '@docusaurus/useBaseUrl';
12+
13+
<ThemedImage
14+
alt="SDK v6"
15+
sources={{
16+
light: useBaseUrl(
17+
'/img/blog/sdk-v6/SDK6-light.webp'
18+
),
19+
dark: useBaseUrl(
20+
'/img/blog/sdk-v6/SDK6-dark.webp'
21+
),
22+
}}
23+
/>
24+
25+
With Tolgee SDK Version 6, fetching translations for your Next.js applications is easier than ever. You can prefetch translation data on the server and seamlessly pass it to the client, ensuring smooth and efficient localization.
26+
27+
<!-- truncate-->
28+
29+
### Fetching Required Translations with `loadRequired`
30+
31+
The `loadRequired` method fetches translations based on the current language and namespace settings. Use it on the server to prefetch data and on the client to avoid extra fetches:
32+
33+
```ts
34+
// Server-side
35+
const translations = JSON.stringify(await tolgee.loadRequired());
36+
37+
// Client-side
38+
tolgee.addStaticData(JSON.parse(translations));
39+
```
40+
41+
This approach ensures the client has the translations ready without additional requests.
42+
Check the [next.js integration guide](/js-sdk/integrations/react/next/app-router) to see how it's used.
43+
44+
### Advanced Prefetching with `loadMatrix`
45+
46+
For greater control, use `loadMatrix` to prefetch specific languages and namespaces.
47+
48+
```ts
49+
// Fetch translations for multiple languages and namespaces
50+
const translations = await tolgee.loadMatrix({
51+
languages: ['en', 'cs'],
52+
namespaces: ['common', 'info']
53+
});
54+
```
55+
56+
To prefetch all namespaces for a language:
57+
58+
```ts
59+
await tolgee.loadMatrix({
60+
languages: ['en'],
61+
namespaces: 'all'
62+
});
63+
t('app_title', { ns: 'info' });
64+
```
65+
66+
These tools make it simple to fetch and render translations, streamlining the localization process for server-rendered and server components.
67+
68+
## Breaking changes
69+
70+
Version 6 is just a mild update, so breaking changes are minimal.
71+
72+
Read the [migration guide](/js-sdk/migration_to_v6) to see what has changed.
73+
74+
[![React banner](/img/blog/blog-banners/banner-demo.webp)](https://app.tolgee.io/sign_up)
75+

docs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const docs = [
3030
versions: {
3131
current: {
3232
banner: 'none',
33-
label: '5.x.x',
33+
label: '6.x.x',
3434
},
3535
},
3636
},

js-sdk/api/core_package/events.mdx

+18-11
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,71 @@ Tolgee events which can be listened through `tolgee.on` method.
1111
Emitted on language change.
1212

1313
```ts
14-
tolgee.on('language', handler: ListenerHandler<string>)
14+
tolgee.on('language', handler: Handler<LanguageEvent>)
1515
```
1616

1717
### pendingLanguage
1818

1919
Emitted on language change. Before languages are loaded (when tolgee is running).
2020

2121
```ts
22-
tolgee.on('pendingLanguage', handler: ListenerHandler<string>)
22+
tolgee.on('pendingLanguage', handler: Handler<PendingLanguageEvent>)
2323
```
2424

2525
### loading
2626

2727
Emitted on loading change. Changes when tolgee is loading some data for the first time.
2828

2929
```ts
30-
tolgee.on('loading', handler: ListenerHandler<boolean>)
30+
tolgee.on('loading', handler: Handler<LoadingEvent>)
3131
```
3232

3333
### fetching
3434

3535
Emitted on fetching change. Changes when tolgee is fetching any data.
3636

3737
```ts
38-
tolgee.on('fetching', handler: ListenerHandler<boolean>)
38+
tolgee.on('fetching', handler: Handler<FetchingEvent>)
3939
```
4040

4141
### initialLoad
4242

4343
Emitted when `run` method finishes.
4444

4545
```ts
46-
tolgee.on('initialLoad', handler: ListenerHandler<void>)
46+
tolgee.on('initialLoad', handler: Handler<InitialLoadEvent>)
4747
```
4848

4949
### running
5050

5151
Emitted when internal `running` state changes.
5252

5353
```ts
54-
tolgee.on('initialLoad', handler: ListenerHandler<boolean>)
54+
tolgee.on('running', handler: Handler<RunningEvent>)
5555
```
5656

5757
### cache
5858

5959
Emitted when cache changes.
6060

6161
```ts
62-
tolgee.on('cache', handler: ListenerHandler<CacheDescriptorWithKey>)
62+
tolgee.on('cache', handler: Handler<CacheEvent>)
6363
```
6464

6565
### update
6666

67-
Emitted when any key needs (or might need) to be re-rendered. Similar to `tolgee.onNsUpdate`,
68-
except we intercept all events, not just selection.
67+
Emitted when any key needs (or might need) to be re-rendered. It's derrived from other events
68+
(`initialLoad`, `language`, `cache`), the events are grouped when multiple happen at the same time,
69+
so it only results to one `update` event being emitted.
6970

7071
```ts
71-
tolgee.on('update', handler: ListenerHandler<void>)
72+
tolgee.on('update', handler: CombinedHandler<UpdateEvent>)
73+
```
74+
75+
You can check what events triggered this change event from the first argument of the handler.
76+
77+
```ts
78+
tolgee.on('update', (events) => events.forEach(e => ...))
7279
```
7380

7481
### error
@@ -79,6 +86,6 @@ Emitted when there is an error. You can intercept different types of errors, con
7986
- `LanguageStorageError` - error when loading/saving language through language storage plugin
8087

8188
```ts
82-
tolgee.on('error', handler: ListenerHandler<TolgeeError>)
89+
tolgee.on('error', handler: Handler<ErrorEvent>)
8390
```
8491

js-sdk/api/core_package/options.mdx

+10-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ projectId: number | string;
6868

6969
### availableLanguages
7070

71-
Languages which can be used for language detection
72-
and also limits which values can be stored. Is derrived from `staticData` keys if not provided.
71+
Specify all available languages. Required for language detection or loading all languages at once ([loadMatrix](./tolgee.mdx#loadmatrix)).
72+
It also limits which values can be stored. Is derrived from `staticData` keys if not provided.
7373

7474
```ts
7575
availableLanguages: string[];
@@ -99,6 +99,14 @@ Default namespace when no namespace defined (default: empty string)
9999
defaultNs: string;
100100
```
101101

102+
### availableNs
103+
104+
Specify all available namespaces. Required for loading all namespaces at once ([loadMatrix](./tolgee.mdx#loadmatrix)).
105+
106+
```ts
107+
availableNs: string[];
108+
```
109+
102110
### staticData
103111

104112
These data go directly to cache or you can specify async

js-sdk/api/core_package/tolgee.mdx

+67-30
Original file line numberDiff line numberDiff line change
@@ -102,27 +102,6 @@ listener.unsubscribe()
102102

103103
Check [Events](./events) for details.
104104

105-
### onNsUpdate
106-
107-
Allows you to listen to key changes of namespaces that you pick.
108-
109-
```ts
110-
tolgee.onNsUpdate(handler: ListenerHandler): ListenerSelective
111-
```
112-
113-
Returns object, which allows you to subscribe to namespaces.
114-
115-
```ts
116-
const listener = tolgee.onNsUpdate(...)
117-
// subscribes namespace
118-
listener.subscribeNs(ns)
119-
// unsubscribe completely
120-
listener.unsubscribe()
121-
```
122-
123-
Use this when you want to make sure, you always update translation when it's necessary.
124-
It internally stores list of namespaces that you are interested in and calls the handler, when necessary.
125-
126105
### t
127106

128107
Returns formatted translation based on current language. If `Observer` is present and tolgee is running, `wraps` the result to be identifiable in the DOM.
@@ -274,15 +253,16 @@ when you need to find out if tolgee has all the necessary data before `run` func
274253
tolgee.isLoaded(ns?: FallbackNsTranslation): boolean
275254
```
276255

277-
### getRequiredRecords
256+
### getRequiredDescriptors
278257

279-
Returns records needed for instance to be `loaded`
258+
Returns descriptors of records needed for instance to be `loaded`
280259

281260
```ts
282-
tolgee.getRequiredRecords(): CacheDescriptor[]
261+
tolgee.getRequiredDescriptors(lang?: string, ns?: NsFallback): CacheDescriptorInternal[]
283262
```
284263

285-
- `ns`: `string` | `string[]` - optional list of namespaces that you are interested in
264+
- `lang`: `string` - optional language (if not present, takes current language)
265+
- `ns`: `string` | `string[]` - optional list of namespaces that you are interested in (if not present takes current active namespaces)
286266

287267
### isDev
288268

@@ -335,24 +315,81 @@ Returns wrapped translation info.
335315
tolgee.unwrap(text: string): Unwrapped
336316
```
337317

338-
### loadRecord
339318

340-
Manually load record from `Backend` (or `DevBackend` when in dev mode).
319+
### loadRequired
320+
321+
Load records required by current settings (influenced by current language and active namespaces).
341322

342323
```ts
343-
tolgee.loadRecord(descriptors: CacheDescriptor): Promise<TranslationsFlat>
324+
loadRequired(options?: LoadRequiredOptions): Promise<CacheInternalRecord[]>
344325
```
345326

327+
In most cases can be called without any options and will return records which would be loaded
328+
by `run` method initially.
329+
330+
Options:
331+
- `language`: `string` - optionally override instance language (can be used when you don't know the language at the initialization time)
332+
- `noDev`: `boolean` - load production translations only - skips DevBackend (false by default)
333+
- `useCache`: `boolean` - skip fetching already loaded records (false by default)
334+
335+
### loadMatrix
336+
337+
Loads all combinations of specified languages and namespaces.
338+
339+
```ts
340+
loadRequired(options?: LoadRequiredOptions): Promise<CacheInternalRecord[]>
341+
```
342+
343+
Example usage:
344+
```ts
345+
const result = await tolgee.loadMatrix({
346+
languages: ['en', 'cs'],
347+
namespaces: ['']
348+
})
349+
// loads `en` and `cs` languages for empty namespace
350+
351+
const result = await tolgee.loadMatrix({
352+
languages: ['en', 'cs'],
353+
namespaces: ['common', 'info']
354+
})
355+
// loads `en:common`, `en:info`, `cs:common` and `cs:info` records
356+
```
357+
358+
Options:
359+
- `languages`: `string[] | 'all'` - languages that should be loaded
360+
- use `all` value to load all [`availableLanguages`](./options.mdx#availablelanguages)
361+
- `namespaces`: `string[] | 'all'` - namespaces that should be loaded
362+
- use `all` value to load all [`availableNs`](./options.mdx#availablens)
363+
- `noDev`: `boolean` - load production translations only - skips DevBackend (false by default)
364+
- `useCache`: `boolean` - skip fetching already loaded records (false by default)
365+
366+
346367
### loadRecords
347368

348-
Manually load multiple records from `Backend` (or `DevBackend` when in dev mode).
369+
Load records by specifying it's descriptors (language and namespace).
349370

350371
```ts
351-
tolgee.loadRecords(descriptors: CacheDescriptor[]): Promise<TranslationsFlat[]>
372+
tolgee.loadRecords(descriptors: CacheDescriptor[], options?: LoadOptions): Promise<TranslationsFlat[]>
352373
```
353374

354375
It loads data together and adds them to cache in one operation, to prevent partly loaded state.
355376

377+
Options:
378+
- `noDev`: `boolean` - load production translations only - skips DevBackend (false by default)
379+
- `useCache`: `boolean` - skip fetching already loaded records (false by default)
380+
381+
### loadRecord
382+
383+
Load record by specifying it's descriptor (language and namespace).
384+
385+
```ts
386+
tolgee.loadRecord(descriptor: CacheDescriptor, options?: LoadOptions): Promise<TranslationsFlat>
387+
```
388+
389+
Options:
390+
- `noDev`: `boolean` - load production translations only - skips DevBackend (false by default)
391+
- `useCache`: `boolean` - skip fetching already loaded records (false by default)
392+
356393
### getRecord
357394

358395
Get record from cache.

js-sdk/api/web_package/tools.mdx

-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ slug: tools
88

99
As `DevTools` are automatically omitted on production, `@tolgee/web/tools` can be used to include it unconditionally.
1010

11-
:::info
12-
Tools were moved from `@tolgee/web` in version 5.2.0.
13-
:::
14-
1511
## `InContextTools`
1612

1713

0 commit comments

Comments
 (0)