svelte-i18n icon indicating copy to clipboard operation
svelte-i18n copied to clipboard

How to use this library with typescript

Open sejr1996 opened this issue 3 years ago • 1 comments

How can I use internationalization in a typescript (.ts) file where I make http requests to display a success or error message.

sejr1996 avatar Mar 09 '22 17:03 sejr1996

This question might be best suited to StackOverflow, but here goes:

To get translated strings imperatively, that is, outside of a Svelte component, you'll need either to subscribe to the appropriate store, or to get the value using Svelte's get() accessor, like so:

import { _ } from "svelte-i18n";
import { get } from "svelte/store";

const str = get(_)("foo.bar"); // value is "baz"

This assumes you've elsewhere loaded locale strings from a file like so:

{
	"foo": {
		"bar": "baz"
	}
}

AverageHelper avatar Aug 10 '22 03:08 AverageHelper