Astro国际化

问题描述 投票:0回答:1

有没有办法通过检测父文件夹名称来知道当前的语言?

src
└── pages
    ├── fr <-- THIS IS THE PARENT FOLDER
    |   └── index.astro 
    └── index.astro

我尝试过的事情:

  • Astro 集成提供的集成 –> 还不稳定
  • 为每个页面定义一个变量并将其作为 props 传递给每个子组件 - > 非常乏味
  • 在页面外部创建全局变量并在每个页面内部更改它 – > 在开发模式下不起作用

我也想从子组件访问语言

i18next astrojs
1个回答
1
投票

最简单的方法可能是查看页面或组件中的

Astro.url

---
// The pathname of the current page, e.g. '/fr/'.
const { pathname } = Astro.url;

// The language (parent folder name) will be the second
// item if we split the path at every forward slash.
const lang = pathname.split('/')[1];
---

<p>Language is {lang}.</p>
© www.soinside.com 2019 - 2024. All rights reserved.