一个使用 SvelteKit 和 Paraglide 构建的现代化国际化 Web 应用程序模板。
确保你已经安装了以下工具:
# 使用 bun,或使用 npm、pnpm
bun install
# 启动开发服务器,或使用 npm、pnpm
bun run dev
访问 http://localhost:5173 查看你的应用。
在 messages/
目录中创建新的语言文件:
touch messages/fr.json # 法语示例
在 project.inlang/settings.json
中添加新语言:
{
"locales": ["en", "zh", "fr"]
}
在语言文件中添加翻译内容:
{
"$schema": "https://inlang.com/schema/inlang-message-format",
"name": "Votre nom",
"welcome": "Bienvenue sur SvelteKit"
}
<script lang="ts">
import { m } from '$lib/paraglide/messages';
</script>
<h1>{m.welcome()}</h1><p>{m['about.description']({ name: 'SvelteKit' })}</p>
项目已经包含了语言切换功能,位于导航栏的下拉菜单中。你可以通过以下方式程序化切换语言:
import { setLocale } from '$lib/paraglide/runtime';
// 切换到中文
setLocale('zh');
// 切换到英文
setLocale('en');
Happy Coding! 🎉