Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nikonikoniko opened this issue Sep 19, 2024 · 0 comments

Comments

@nikonikoniko
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant