soon-i18n-solid
install
npm install soon-i18n-solid
full example
npx degit https://github.com/leafio/soon-i18n/packages/soon-i18n-solid/demo
instance usage
create an instance
import { createI18n } from "soon-i18n-solid";
const global_locales = { zh: { g_welcome: "全局:欢迎 {name}" }, en: { g_welcome: "Global: Welcome {name}" },} as const;type Lang = "zh" | "en";
export const { tLocales, lang, setLang } = createI18n( { lang: "zh" as Lang, fallbacks: ["en"] }, global_locales);
use in js/ts
import { tLocales } from "../lang";export const showToast = () => { const t = tLocales({ zh: { tip: "哈哈,一条中文提醒!!!" }, en: { tip: "Aha, an English tip" }, }); alert(t("tip"));};
use in components
import { tLocales } from "../lang";
const Content = () => { const t = tLocales({ zh: { hello: "你好" }, en: { hello: "Hello" }, }); return <div>{t("hello")}</div>;};export default Content;
change lang
import { lang, setLang } from "../lang";
const SwitchLang = () => { const handleToggle = () => { setLang(lang() === "en" ? "zh" : "en"); }; return <button onClick={handleToggle}>{lang()}</button>;};export default SwitchLang;