Internationalization i18n nuxt内容布局和检索子文件夹内容列表的策略
我正试图找出如何以最有效的方式布局一个复杂的nuxt i18n网站。其设置以nuxt-i18n和nuxt内容为中心。我对“所有语言环境前缀”有强烈的偏见,因此每个翻译在url中都会收到自己的前缀。来自hugo,我对PageBundles的布局非常感兴趣,因此我尝试创建一个设置,其中特定路线/页面的所有本地资产、文本和翻译都位于/content/下的同一文件夹中。这是我的“内容”目录的一个示例:Internationalization i18n nuxt内容布局和检索子文件夹内容列表的策略,internationalization,nuxt.js,nuxt-i18n,nuxt-content,Internationalization,Nuxt.js,Nuxt I18n,Nuxt Content,我正试图找出如何以最有效的方式布局一个复杂的nuxt i18n网站。其设置以nuxt-i18n和nuxt内容为中心。我对“所有语言环境前缀”有强烈的偏见,因此每个翻译在url中都会收到自己的前缀。来自hugo,我对PageBundles的布局非常感兴趣,因此我尝试创建一个设置,其中特定路线/页面的所有本地资产、文本和翻译都位于/content/下的同一文件夹中。这是我的“内容”目录的一个示例: - content/ - articles/ ./index.en.md
- content/
- articles/
./index.en.md
./index.pt.md
./index.it.md
- one-article/
./index.en.md
./index.pt.md
./index.it.md
./cover.jpg
./slides/
./slide-01.jpg
./slide-02.jpg
./slide-03.jpg
- another-article/
./index.en.md
./index.pt.md
./index.it.md
./cover.jpg
./slides/
./slide-01.jpg
./slide-02.jpg
./gallery/
./lovely-sunset.jpg
./great-sunset.jpg
./awesome-sunset.jpg
./...
我能够通过以下查询在“/pages/articles/index.vue”中检索正确的本地化翻译:
async asyncData({ error, $content, i18n }) {
try {
const page = await $content('articles/index.' + i18n.locale)
.fetch()
return { page }
} catch (err) {
error({
statusCode: 404,
message: 'Ops!',
})
}
},
但是,除了索引内容之外,我需要显示此文件夹中的文章列表。起初,我想我可以添加这样的内容:
...
const articles = await $content('articles')
.only(['title', 'slug'])
.sortBy('createdAt', 'asc')
.where({
slug: 'index.' + i18n.locale,
})
.fetch()
return { page, articles } // adding 'articles' to return
让我惊讶的是,它只返回主文件夹中的.md文件列表(在/articles/的正下方),而且,它确实有意义。因此,我需要了解如何在所有子文件夹下检索属于当前区域设置的所有.md文件的列表
在my_slug.vue中,我通过以下查询过滤掉了其他翻译:
async asyncData({ $content, i18n, params }) {
const article = await $content('articles', params.slug).where({
slug: 'index.' + i18n.locale,
})
fetch()
return { article }
},
而且它工作得很好
希望一切都清楚!;)