Skip to content

Commit

Permalink
feat: vue ssr docs (#675)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 authored Sep 23, 2024
1 parent 8afb45e commit cc24ab8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
79 changes: 79 additions & 0 deletions js-sdk/integrations/vue/ssr.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
id: ssr
title: SSR support
sidebar_label: SSR support
---

To use Tolgee with your SSR framework such as Vike (vite-plugin-ssr), you can provide localization data imported as object
using `staticData` props of TolgeeProvider component, this data will be loaded directly into cache and immediately
available with first render for SSR. We also need to know user's locale already on the server and set it through TolgeeProvider `language` prop.

```ts
import { Tolgee, DevTools, VueTolgee, FormatSimple } from '@tolgee/vue';

const tolgee = Tolgee()
.use(DevTools())
.use(FormatSimple())
.init({
defaultLanguage: 'en',
});

...

app.use(VueTolgee, { tolgee, enableSSR: true });
```

and in the your root component

```html
<script setup>
import localeEn from 'i18n/en.json';
import localeDe from 'i18n/de.json';
const staticData = {
en: localeEn,
de: localeDe,
};
// we need to get these on server, which we'll need to implement
// differently for each framework
const language = ?;
</script>
...
<template>
<TolgeeProvider :language="language" :static-data="staticData" ...>
...
</TolgeeProvider>
</template>
```
With this approach we include all translations directly in the bundle regardless user locale. For smaller projects this is not a big issue, however it might be significant for large applications with many translations and languages.
For these cases we need to only provide statically the locale, that the user is currently using. We can also use async functions in `staticData` which will be used for fetching translations dynamically on client side (you can use this instead of having them in public folder).
```html
<script setup>
// we need to get these on server, which we'll need to implement
// differently for each framework
const language = ?;
const staticData = ?;
</script>
...
<template>
<TolgeeProvider :language="language" :static-data="staticData" ...>
...
</TolgeeProvider>
</template>
```
## Language changing
When we use SSR, we have to specify language in a way that is detectable by both client and server. Easiest way is to
include it directly in URL.
Then for language change we use the native way of the framework.
1 change: 1 addition & 0 deletions sidebarJsSdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ module.exports = {
'integrations/vue/translating',
'integrations/vue/component_interpolation',
'integrations/vue/switching_language',
'integrations/vue/ssr',
'integrations/vue/api',
],
},
Expand Down

0 comments on commit cc24ab8

Please sign in to comment.