@better-translate/astro 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +104 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,5 +1,106 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Astro
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Use `@better-translate/astro` when your Astro app needs request-aware translations.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## 1. Install the packages
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @better-translate/core @better-translate/astro astro
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 2. Create the translator
|
|
12
|
+
|
|
13
|
+
Create `src/lib/i18n.ts`:
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { configureTranslations } from "@better-translate/core";
|
|
17
|
+
|
|
18
|
+
const en = {
|
|
19
|
+
home: {
|
|
20
|
+
title: "Hello from Astro",
|
|
21
|
+
},
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
const es = {
|
|
25
|
+
home: {
|
|
26
|
+
title: "Hola desde Astro",
|
|
27
|
+
},
|
|
28
|
+
} as const;
|
|
29
|
+
|
|
30
|
+
export const translator = await configureTranslations({
|
|
31
|
+
availableLocales: ["en", "es"] as const,
|
|
32
|
+
defaultLocale: "en",
|
|
33
|
+
fallbackLocale: "en",
|
|
34
|
+
messages: { en, es },
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## 3. Expose the request config
|
|
39
|
+
|
|
40
|
+
Create `src/lib/request.ts`:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import { getRequestConfig } from "@better-translate/astro";
|
|
44
|
+
|
|
45
|
+
import { translator } from "./i18n";
|
|
46
|
+
|
|
47
|
+
export const requestConfig = getRequestConfig(async () => ({
|
|
48
|
+
translator,
|
|
49
|
+
}));
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## 4. Set the request locale in middleware
|
|
53
|
+
|
|
54
|
+
Create `src/middleware.ts`:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import { createBetterTranslateMiddleware } from "@better-translate/astro/middleware";
|
|
58
|
+
|
|
59
|
+
import { requestConfig } from "./lib/request";
|
|
60
|
+
|
|
61
|
+
export const onRequest = createBetterTranslateMiddleware(requestConfig, {
|
|
62
|
+
resolveLocale: ({ params }) => params?.lang,
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## 5. Read translations in a page
|
|
67
|
+
|
|
68
|
+
Create `src/lib/server.ts`:
|
|
69
|
+
|
|
70
|
+
```ts
|
|
71
|
+
import { createServerHelpers } from "@better-translate/astro";
|
|
72
|
+
|
|
73
|
+
import { requestConfig } from "./request";
|
|
74
|
+
|
|
75
|
+
export const { getLocale, getTranslations } =
|
|
76
|
+
createServerHelpers(requestConfig);
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Create `src/pages/[lang]/index.astro`:
|
|
80
|
+
|
|
81
|
+
```astro
|
|
82
|
+
---
|
|
83
|
+
import { getLocale, getTranslations } from "../../lib/server";
|
|
84
|
+
|
|
85
|
+
const locale = await getLocale();
|
|
86
|
+
const t = await getTranslations();
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
<html lang={locale}>
|
|
90
|
+
<body>
|
|
91
|
+
<h1>{t("home.title")}</h1>
|
|
92
|
+
</body>
|
|
93
|
+
</html>
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Optional next step
|
|
97
|
+
|
|
98
|
+
If your Astro docs or blog posts live in localized content collections, add Astro content helpers or the MDX adapter after this basic setup is working.
|
|
99
|
+
|
|
100
|
+
## Generate locale files automatically
|
|
101
|
+
|
|
102
|
+
Instead of writing every translation by hand, use the CLI to extract strings and generate locale files: [CLI guide](https://better-translate.com/en/docs/cli)
|
|
103
|
+
|
|
104
|
+
## Examples
|
|
105
|
+
|
|
106
|
+
- [astro-example](https://github.com/jralvarenga/better-translate/tree/main/examples/astro-example)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-translate/astro",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Astro integration package for Better Translate.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -45,12 +45,12 @@
|
|
|
45
45
|
"url": "git+https://github.com/jralvarenga/better-translate.git",
|
|
46
46
|
"directory": "packages/astro"
|
|
47
47
|
},
|
|
48
|
-
"homepage": "https://
|
|
48
|
+
"homepage": "https://www.npmjs.com/package/@better-translate/astro",
|
|
49
49
|
"bugs": {
|
|
50
50
|
"url": "https://github.com/jralvarenga/better-translate/issues"
|
|
51
51
|
},
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@better-translate/core": "^2.
|
|
53
|
+
"@better-translate/core": "^2.2.0"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
56
|
"astro": "^5.0.0 || ^6.0.0"
|