Skip to content

Accessing translations on the server: in api responses, scripts, and more. #257

@nikonikoniko

Description

@nikonikoniko

I have been trying to access our translations in our server responses: outside of the svelte store lifecycle. I have figured out that I can do something similar to this:

import { _, addMessages } from 'svelte-i18n';

import en from '../../../../i18n/en.json';
import ukr from '../../../../i18n/ukr.json';

addMessages('en', en);
addMessages('ukr', ukr);

let translate = (message: string, options: object) => 'translator not set up yet';

_.subscribe((f) => {
    translate = f;
});

const translation = translate('app.page_not_found', { locale: 'ukr' });

console.log(translation);

This gets the correct translations out of the provided files. I keep thinking there must be an easier way to do this, But I can't find anything in the documentation. It seems to me like the svelte-i18n api should expose a simple function to get a translation, if it has already been registered, outside of the store and front-end lifecycle. In our case we can get the locale from the database, the user's preferences, or read the cookies in the api response.

Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions