An open API service indexing awesome lists of open source software.

https://github.com/openscript/astro-loader-i18n

An Astro glob content loader for i18n files and folder structures. ๐ŸŒ
https://github.com/openscript/astro-loader-i18n

astro content-loader i18n

Last synced: 7 months ago
JSON representation

An Astro glob content loader for i18n files and folder structures. ๐ŸŒ

Awesome Lists containing this project

README

          

# astro-loader-i18n

This package is a simple content loader for i18n files and folder structures, partially built on top of the [`glob()` loader](https://docs.astro.build/en/reference/content-loader-reference/#glob-loader).

```bash
npm install astro-loader-i18n
```

```bash
yarn add astro-loader-i18n
```

```bash
pnpm add astro-loader-i18n
```

## Overview

For example, given the following i18n structure:

```
. (project root)
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ src
โ””โ”€โ”€ content
โ””โ”€โ”€ pages
โ”œโ”€โ”€ de-CH
โ”‚ โ”œโ”€โ”€ about.mdx
โ”‚ โ””โ”€โ”€ projects.mdx
โ””โ”€โ”€ zh-CN
โ”œโ”€โ”€ about.mdx
โ””โ”€โ”€ projects.mdx
```

Define a collection in your `content.config.ts` using the `astro-loader-i18n` file:

```typescript
import { i18nFolderLoader, z } from 'astro-loader-i18n';
import { defineCollection } from 'astro:content';

const pages = defineCollection({
loader: i18nFolderLoader({
pattern: "**/*.mdx",
base: "src/content/pages",
schema: z.object({
title: z.string(),
}),
}),
schema: /* USUALLY YOU DON'T WANT TO OVERRIDE THE LOADERS SCHEMA */
});

export const collections = { pages };
```

> [!CAUTION]
> The `schema` should be defined in the loader, not in the collection definition. This is because the loader is responsible for parsing the content and the collection is responsible for filtering and sorting the content.

Retrieve the collection and filter by locale:

```ts
import { getCollection } from "astro:content";

const pages = await getCollection("pages", (entry) => { entry.locale === "de-CH"});
```

## Usage

This loader supports differently structured localized content:

### Folder via `i18nFolderLoader`

The content is structured into locales by folders:

```
. (project root)
โ”œโ”€โ”€ README.md
โ””โ”€โ”€ src
โ””โ”€โ”€ content
โ””โ”€โ”€ pages
โ”œโ”€โ”€ de-CH
โ”‚ โ”œโ”€โ”€ about.mdx
โ”‚ โ””โ”€โ”€ projects.mdx
โ””โ”€โ”€ zh-CN
โ”œโ”€โ”€ about.mdx
โ””โ”€โ”€ projects.mdx
```

- The locale is determined by the folder name.
- Localized subfolders can be used to structure the content further.
- Page translations need to be separately mapped.
- Useful for:
- Individual pages with multiple translations
- Flat content structures

### Files via `i18nFilesLoader`

```
. (project root)
โ””โ”€โ”€ src
โ””โ”€โ”€ content
โ””โ”€โ”€ pages
โ”œโ”€โ”€ about.de-CH.mdx
โ”œโ”€โ”€ about.zh-CN.mdx
โ”œโ”€โ”€ projects.de-CH.mdx
โ””โ”€โ”€ projects.zh-CN.mdx
```

- The locale is determined by the file name suffix.
- Subfolders can be used to structure the content further.
- Subfolder names need to be separately translated.
- Useful for:
- Blogs
- News
- Articles
- Notes
- ...

### Infile via standard `glob()` loader

```
. (project root)
โ””โ”€โ”€ src
โ””โ”€โ”€ content
โ””โ”€โ”€ navigation
โ”œโ”€โ”€ footer.yml
โ””โ”€โ”€ main.yml
```

```yaml
# src/content/navigation/main.yml
de-CH:
- path: /projekte
title: Projekte
- path: /ueber-mich
title: รœber mich
zh-CN:
- path: /zh/projects
title: ้กน็›ฎ
- path: /zh/about-me
title: ๅ…ณไบŽๆˆ‘
```

- The locale is determined by the key in the YAML file.
- The content is structured in a single file.
- This allows sharing untranslated content across locales.
- Useful for:
- Menus
- Galleries
- ...

## Improvements

- [ ] Export `GlobOptions` type from Astro