npm install soon-i18n-svelte
npx degit https://github.com/leafio/soon-i18n/packages/soon-i18n-svelte/demo
import { createI18n } from "soon-i18n-svelte";const en_global = { welcome: "Welcome {name}",}const zh_global = { welcome: "欢迎 {name}",} const global_locales = { zh: zh_global, en: en_global,};export const { tLocales, lang } = createI18n( { lang: "zh", fallbacks: ["en"] }, global_locales);
import { get } from "svelte/store";import { tLocales } from "../lang";export const showToast = () => { const t = get( tLocales({ zh: { tip: "哈哈,一条中文提醒!!!" }, en: { tip: "Aha, an English tip" }, }) ); alert(t("tip"));};
<script>import { tLocales } from "../lang";const t = tLocales({ zh: { hello: "你好" }, en: { hello: "Hello" },});</script> <div>{$t("hello")}</div>
<script>import { lang } from "../lang";const t = tLocales({ zh: { hello: "你好" }, en: { hello: "Hello" },});const handleToggle = () => { lang.update((_lang) => (_lang === "en" ? "zh" : "en"));};</script> <button on:click={handleToggle}>{ $lang }</button>