Skip to main content
Version: 5.x.x

Translating (Svelte)

Check our Svelte example application

There are two ways how to translate with Tolgee.

getTranslate method

Other option how to translate is using of getTranslate function. You can use it this way:

<script>
import { getTranslate } from "@tolgee/svelte";

// t is store
const { t } = getTranslate();
</script>

<!-- use the translation function -->
{$t('key', 'Default value')}

t function

This function allows you to render the actual translation.

Simple usage

t('app_title')

The t function looks into your translations finds an appropriate key and renders the translation for currently selected language

If you use Typescript read about typed keys

Default value

You can pass the default value as second parameter, which will be rendered if the key doesn't exist yet.

t('non_existant_key', 'Default value')

Parameter interpolation

You can supply a variable parameters which will then be rendered in your translations.

t('user_points', 'User has {count} points', { count: 10 })

// -> User has 10 points

There are more options you can do with the variable parameters, but it depends on selected formatter plugin.

To get the full t function specification check it's API

The T component

An alternative way is to use T component, which has a very similar use case and API to the t function mentioned above.

<script>
import { T } from "@tolgee/svelte";
</script>

<T
keyName="translation_key"
defaultValue="User has {count} points"
params={{ count: 10 }}
/>

Using namespaces

The getTranslate function can be also used for loading additional namespaces.

const { t, isLoading } = getTranslate('common');

This example will load common namespace which can be then used with t function.

Read more about namespaces here.