Skip to content

SIPC/Tastro

Repository files navigation

Tastro

SSR-first i18n for Astro.

English | 中文

演示

sipc.ink 进入后在控制台输入

setLang("en")
setLang("zh")

安装

pnpm i "@sipc.ink/tastro"

使用

1. 配置

---
import { initTastro } from '@sipc.ink/tastro'

initTastro({
  defaultLang: 'zh',
  languages: {
    zh: () => import('./locales/zh.json'),
    en: () => import('./locales/en.json'),
  },
})
---

2. 创建翻译文件

./locales/zh.json

{
  "welcome": "欢迎",
  "login-time": "上次登录时间:{time}",
}

./locales/en.json

{
  "welcome": "Welcome",
  "login-time": "Last login time: {time}",
}

3. 在 Astro 中使用

---
import { useTastro } from '@sipc.ink/tastro'

const { t, lang } = useTastro(Astro.request)
// 引入 request 是为了自动判断请求中的 Accept-Language
---

// 普通
<h1>{t('welcome')}</h1>

// 插值
<h1>{t('login-time',{ time: "114514" })}</h1>

// 浏览器语言
<p>当前语言: {lang}</p>

4. 手动切换语言

<script>
    function setLang(lang) {
        document.cookie = `lang=${lang}; path=/; max-age=31536000`
        location.reload()
    }
</script>

API

  • initTastro(config) - 初始化配置
  • useTastro(request) - 创建运行时
    • t(key, vars) - 翻译文本
    • lang - 当前语言

特性

  • 🚀 SSR 优先
  • ⚡ 零依赖,轻量
  • 📦 自动缓存
  • 🍪 智能语言检测(Cookie → Accept-Language → 默认)

About

Astro i18n

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors