@intlayer/docs 7.0.4-canary.0 → 7.0.4
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/blog/ar/intlayer_with_i18next.md +68 -106
- package/blog/ar/intlayer_with_next-i18next.md +84 -288
- package/blog/ar/intlayer_with_next-intl.md +58 -337
- package/blog/ar/intlayer_with_react-i18next.md +68 -290
- package/blog/ar/intlayer_with_react-intl.md +63 -266
- package/blog/de/intlayer_with_i18next.md +77 -97
- package/blog/de/intlayer_with_next-i18next.md +69 -296
- package/blog/de/intlayer_with_next-intl.md +59 -340
- package/blog/de/intlayer_with_react-i18next.md +68 -290
- package/blog/de/intlayer_with_react-intl.md +62 -264
- package/blog/en/intlayer_with_i18next.md +36 -1638
- package/blog/en/intlayer_with_next-i18next.md +22 -847
- package/blog/en/intlayer_with_next-intl.md +32 -1053
- package/blog/en/intlayer_with_react-i18next.md +38 -764
- package/blog/en/intlayer_with_react-intl.md +42 -1018
- package/blog/en-GB/intlayer_with_i18next.md +67 -103
- package/blog/en-GB/intlayer_with_next-i18next.md +71 -292
- package/blog/en-GB/intlayer_with_next-intl.md +58 -337
- package/blog/en-GB/intlayer_with_react-i18next.md +67 -289
- package/blog/en-GB/intlayer_with_react-intl.md +61 -264
- package/blog/es/intlayer_with_i18next.md +67 -103
- package/blog/es/intlayer_with_next-i18next.md +71 -296
- package/blog/es/intlayer_with_next-intl.md +57 -338
- package/blog/es/intlayer_with_react-i18next.md +68 -290
- package/blog/es/intlayer_with_react-intl.md +62 -265
- package/blog/fr/intlayer_with_i18next.md +66 -104
- package/blog/fr/intlayer_with_next-i18next.md +82 -285
- package/blog/fr/intlayer_with_next-intl.md +57 -338
- package/blog/fr/intlayer_with_react-i18next.md +67 -289
- package/blog/fr/intlayer_with_react-intl.md +61 -264
- package/blog/hi/intlayer_with_i18next.md +68 -104
- package/blog/hi/intlayer_with_next-i18next.md +74 -299
- package/blog/hi/intlayer_with_next-intl.md +57 -239
- package/blog/hi/intlayer_with_react-i18next.md +69 -291
- package/blog/hi/intlayer_with_react-intl.md +65 -268
- package/blog/id/intlayer_with_i18next.md +126 -0
- package/blog/id/intlayer_with_next-i18next.md +142 -0
- package/blog/id/intlayer_with_next-intl.md +113 -0
- package/blog/id/intlayer_with_react-i18next.md +124 -0
- package/blog/id/intlayer_with_react-intl.md +122 -0
- package/blog/it/intlayer_with_i18next.md +67 -103
- package/blog/it/intlayer_with_next-i18next.md +71 -296
- package/blog/it/intlayer_with_next-intl.md +57 -338
- package/blog/it/intlayer_with_react-i18next.md +68 -290
- package/blog/it/intlayer_with_react-intl.md +62 -265
- package/blog/ja/intlayer_with_i18next.md +68 -103
- package/blog/ja/intlayer_with_next-i18next.md +85 -283
- package/blog/ja/intlayer_with_next-intl.md +58 -336
- package/blog/ja/intlayer_with_react-i18next.md +68 -290
- package/blog/ja/intlayer_with_react-intl.md +62 -264
- package/blog/ko/intlayer_with_i18next.md +80 -96
- package/blog/ko/intlayer_with_next-i18next.md +85 -287
- package/blog/ko/intlayer_with_next-intl.md +68 -327
- package/blog/ko/intlayer_with_react-i18next.md +68 -290
- package/blog/ko/intlayer_with_react-intl.md +64 -266
- package/blog/pl/intlayer_with_i18next.md +126 -0
- package/blog/pl/intlayer_with_next-i18next.md +142 -0
- package/blog/pl/intlayer_with_next-intl.md +111 -0
- package/blog/pl/intlayer_with_react-i18next.md +124 -0
- package/blog/pl/intlayer_with_react-intl.md +122 -0
- package/blog/pt/intlayer_with_i18next.md +67 -103
- package/blog/pt/intlayer_with_next-i18next.md +72 -293
- package/blog/pt/intlayer_with_next-intl.md +57 -256
- package/blog/pt/intlayer_with_react-i18next.md +104 -78
- package/blog/pt/intlayer_with_react-intl.md +62 -266
- package/blog/ru/intlayer_with_i18next.md +66 -104
- package/blog/ru/intlayer_with_next-i18next.md +71 -296
- package/blog/ru/intlayer_with_next-intl.md +58 -337
- package/blog/ru/intlayer_with_react-i18next.md +68 -290
- package/blog/ru/intlayer_with_react-intl.md +62 -265
- package/blog/tr/intlayer_with_i18next.md +71 -107
- package/blog/tr/intlayer_with_next-i18next.md +72 -297
- package/blog/tr/intlayer_with_next-intl.md +58 -339
- package/blog/tr/intlayer_with_react-i18next.md +69 -291
- package/blog/tr/intlayer_with_react-intl.md +63 -285
- package/blog/vi/intlayer_with_i18next.md +126 -0
- package/blog/vi/intlayer_with_next-i18next.md +142 -0
- package/blog/vi/intlayer_with_next-intl.md +111 -0
- package/blog/vi/intlayer_with_react-i18next.md +124 -0
- package/blog/vi/intlayer_with_react-intl.md +122 -0
- package/blog/zh/intlayer_with_i18next.md +67 -102
- package/blog/zh/intlayer_with_next-i18next.md +72 -296
- package/blog/zh/intlayer_with_next-intl.md +58 -336
- package/blog/zh/intlayer_with_react-i18next.md +68 -290
- package/blog/zh/intlayer_with_react-intl.md +63 -106
- package/docs/ar/plugins/sync-json.md +244 -0
- package/docs/de/plugins/sync-json.md +244 -0
- package/docs/en/intlayer_cli.md +25 -0
- package/docs/en/intlayer_with_nextjs_14.md +2 -0
- package/docs/en/intlayer_with_nextjs_15.md +2 -0
- package/docs/en/intlayer_with_nextjs_16.md +2 -0
- package/docs/en/plugins/sync-json.md +1 -1
- package/docs/en-GB/plugins/sync-json.md +244 -0
- package/docs/es/plugins/sync-json.md +244 -0
- package/docs/fr/plugins/sync-json.md +244 -0
- package/docs/hi/plugins/sync-json.md +244 -0
- package/docs/id/plugins/sync-json.md +244 -0
- package/docs/it/plugins/sync-json.md +244 -0
- package/docs/ja/plugins/sync-json.md +244 -0
- package/docs/ko/plugins/sync-json.md +244 -0
- package/docs/pl/plugins/sync-json.md +244 -0
- package/docs/pt/plugins/sync-json.md +244 -0
- package/docs/ru/plugins/sync-json.md +244 -0
- package/docs/tr/plugins/sync-json.md +245 -0
- package/docs/vi/plugins/sync-json.md +244 -0
- package/docs/zh/plugins/sync-json.md +244 -0
- package/package.json +14 -14
|
@@ -1,392 +1,114 @@
|
|
|
1
1
|
---
|
|
2
2
|
createdAt: 2025-01-02
|
|
3
|
-
updatedAt: 2025-
|
|
4
|
-
title: Intlayer
|
|
5
|
-
description:
|
|
6
|
-
keywords:
|
|
7
|
-
- next-intl
|
|
8
|
-
- Intlayer
|
|
9
|
-
- 国際化
|
|
10
|
-
- ドキュメンテーション
|
|
11
|
-
- Next.js
|
|
12
|
-
- JavaScript
|
|
13
|
-
- React
|
|
3
|
+
updatedAt: 2025-10-29
|
|
4
|
+
title: Intlayerを使ってnext-intlのJSON翻訳を自動化する方法
|
|
5
|
+
description: Next.jsアプリケーションの国際化を強化するために、Intlayerとnext-intlを使ってJSON翻訳を自動化する方法。
|
|
14
6
|
slugs:
|
|
15
7
|
- blog
|
|
16
8
|
- intlayer-with-next-intl
|
|
9
|
+
history:
|
|
10
|
+
- version: 7.0.0
|
|
11
|
+
date: 2025-10-29
|
|
12
|
+
changes: syncJSONプラグインへの変更
|
|
17
13
|
---
|
|
18
14
|
|
|
19
|
-
#
|
|
15
|
+
# Intlayerを使ってnext-intlのJSON翻訳を自動化する方法
|
|
20
16
|
|
|
21
|
-
|
|
17
|
+
## Intlayerとは?
|
|
22
18
|
|
|
23
|
-
|
|
19
|
+
**Intlayer**は、従来のi18nソリューションの欠点を解決するために設計された革新的なオープンソースの国際化ライブラリです。Next.jsアプリケーションにおけるコンテンツ管理に対してモダンなアプローチを提供します。
|
|
24
20
|
|
|
25
|
-
|
|
26
|
-
- Intlayer では `content declaration file` と呼ばれ、構造化データをエクスポートする JSON、JS、または TS ファイルです。詳細は [Intlayer documentation](https://intlayer.org/fr/doc/concept/content) を参照してください。
|
|
27
|
-
- next-intl では `messages` または `locale messages` と呼ばれ、通常は JSON ファイルにあります。詳細は [next-intl documentation](https://github.com/amannn/next-intl) を参照してください。
|
|
21
|
+
next-intlとの具体的な比較については、当社のブログ記事[ next-i18next vs. next-intl vs. Intlayer](https://github.com/aymericzip/intlayer/blob/main/docs/blog/ja/next-i18next_vs_next-intl_vs_intlayer.md)をご覧ください。
|
|
28
22
|
|
|
29
|
-
|
|
23
|
+
## なぜIntlayerをnext-intlと組み合わせるのか?
|
|
30
24
|
|
|
31
|
-
|
|
25
|
+
Intlayerは優れた単独のi18nソリューションを提供します(当社の[Next.js統合ガイド](https://github.com/aymericzip/intlayer/blob/main/docs/docs/ja/intlayer_with_nextjs_16.md)を参照)が、いくつかの理由でnext-intlと組み合わせたい場合があります。
|
|
32
26
|
|
|
33
|
-
|
|
27
|
+
1. **既存のコードベース**: 既に確立されたnext-intlの実装があり、Intlayerの改善された開発者体験に徐々に移行したい場合。
|
|
28
|
+
2. **レガシー要件**: プロジェクトが既存のnext-intlプラグインやワークフローとの互換性を必要とする場合。
|
|
29
|
+
3. **チームの慣れ親しみ**: チームがnext-intlに慣れているが、より良いコンテンツ管理を望んでいる場合。
|
|
34
30
|
|
|
35
|
-
Intlayer
|
|
31
|
+
**そのために、Intlayerはnext-intlのアダプターとして実装でき、CLIやCI/CDパイプラインでのJSON翻訳の自動化、翻訳のテストなどを支援します。**
|
|
36
32
|
|
|
37
|
-
|
|
33
|
+
このガイドでは、next-intlとの互換性を維持しながら、Intlayerの優れたコンテンツ宣言システムを活用する方法を示します。
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
## 目次
|
|
40
36
|
|
|
41
|
-
|
|
37
|
+
<TOC/>
|
|
42
38
|
|
|
43
|
-
|
|
39
|
+
## next-intlとIntlayerをセットアップするステップバイステップガイド
|
|
44
40
|
|
|
45
|
-
|
|
41
|
+
### ステップ1: 依存関係のインストール
|
|
46
42
|
|
|
47
|
-
|
|
48
|
-
.
|
|
49
|
-
└── src
|
|
50
|
-
└── components
|
|
51
|
-
└── MyComponent
|
|
52
|
-
├── index.content.ts # コンテンツ宣言ファイル
|
|
53
|
-
└── index.tsx
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
```bash codeFormat="esm"
|
|
57
|
-
.
|
|
58
|
-
└── src
|
|
59
|
-
└── components
|
|
60
|
-
└── MyComponent
|
|
61
|
-
├── index.content.mjs # コンテンツ宣言ファイル
|
|
62
|
-
└── index.mjx
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
```bash codeFormat="cjs"
|
|
66
|
-
.
|
|
67
|
-
└── src
|
|
68
|
-
└── components
|
|
69
|
-
└── MyComponent
|
|
70
|
-
├── index.content.cjs # コンテンツ宣言ファイル
|
|
71
|
-
└── index.cjx
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
```bash codeFormat="json"
|
|
75
|
-
.
|
|
76
|
-
└── src
|
|
77
|
-
└── components
|
|
78
|
-
└── MyComponent
|
|
79
|
-
├── index.content.json # コンテンツ宣言ファイル
|
|
80
|
-
└── index.jsx
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
2. **中央集約型の翻訳**: Intlayer はすべての翻訳を単一のコンテンツ宣言に保存し、翻訳が失われることはありません。TypeScript プロジェクトでは、失われた翻訳が自動的に型エラーとしてフラグされ、開発者に即座にフィードバックを提供します。
|
|
84
|
-
|
|
85
|
-
### インストール
|
|
86
|
-
|
|
87
|
-
Intlayer と next-intl を一緒に使用するには、両方のライブラリをインストールします:
|
|
43
|
+
必要なパッケージをインストールします:
|
|
88
44
|
|
|
89
45
|
```bash packageManager="npm"
|
|
90
|
-
npm install intlayer
|
|
46
|
+
npm install intlayer @intlayer/sync-json-plugin
|
|
91
47
|
```
|
|
92
48
|
|
|
93
|
-
```bash packageManager="
|
|
94
|
-
|
|
49
|
+
```bash packageManager="pnpm"
|
|
50
|
+
pnpm add intlayer @intlayer/sync-json-plugin
|
|
95
51
|
```
|
|
96
52
|
|
|
97
|
-
```bash packageManager="
|
|
98
|
-
|
|
53
|
+
```bash packageManager="yarn"
|
|
54
|
+
yarn add intlayer @intlayer/sync-json-plugin
|
|
99
55
|
```
|
|
100
56
|
|
|
101
|
-
|
|
57
|
+
**パッケージの説明:**
|
|
102
58
|
|
|
103
|
-
|
|
59
|
+
- **intlayer**: 国際化管理、コンテンツ宣言、およびビルドのためのコアライブラリ
|
|
60
|
+
- **@intlayer/sync-json-plugin**: Intlayerのコンテンツ宣言をnext-intl互換のJSON形式にエクスポートするプラグイン
|
|
104
61
|
|
|
105
|
-
|
|
62
|
+
### ステップ 2: JSONをラップするためのIntlayerプラグインの実装
|
|
106
63
|
|
|
107
|
-
|
|
108
|
-
import { Locales, type IntlayerConfig } from "intlayer";
|
|
64
|
+
サポートするロケールを定義するためのIntlayer設定ファイルを作成します:
|
|
109
65
|
|
|
110
|
-
|
|
111
|
-
internationalization: {
|
|
112
|
-
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
113
|
-
defaultLocale: Locales.ENGLISH,
|
|
114
|
-
},
|
|
115
|
-
content: {
|
|
116
|
-
dictionaryOutput: ["next-intl"], // next-intl 出力を使用する
|
|
117
|
-
nextIntlMessagesDir: "./intl/messages", // next-intl メッセージを保存する場所
|
|
118
|
-
},
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
export default config;
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
```javascript fileName="intlayer.config.mjs" codeFormat="esm"
|
|
125
|
-
import { Locales } from "intlayer";
|
|
126
|
-
|
|
127
|
-
/** @type {import('intlayer').IntlayerConfig} */
|
|
128
|
-
const config = {
|
|
129
|
-
internationalization: {
|
|
130
|
-
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
131
|
-
defaultLocale: Locales.ENGLISH,
|
|
132
|
-
},
|
|
133
|
-
content: {
|
|
134
|
-
dictionaryOutput: ["react-intl"],
|
|
135
|
-
nextIntlMessagesDir: "./intl/messages",
|
|
136
|
-
},
|
|
137
|
-
};
|
|
66
|
+
**next-intl用のJSON辞書もエクスポートしたい場合は、`syncJSON`プラグインを追加してください:**
|
|
138
67
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
```javascript fileName="intlayer.config.cjs" codeFormat="commonjs"
|
|
143
|
-
const { Locales } = require("intlayer");
|
|
68
|
+
```typescript fileName="intlayer.config.ts"
|
|
69
|
+
import { Locales, type IntlayerConfig } from "intlayer";
|
|
70
|
+
import { syncJSON } from "@intlayer/sync-json-plugin";
|
|
144
71
|
|
|
145
|
-
|
|
146
|
-
const config = {
|
|
72
|
+
const config: IntlayerConfig = {
|
|
147
73
|
internationalization: {
|
|
148
74
|
locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
|
|
149
75
|
defaultLocale: Locales.ENGLISH,
|
|
150
76
|
},
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
},
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
module.exports = config;
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
### コンテンツ宣言
|
|
161
|
-
|
|
162
|
-
以下は、複数のフォーマットのコンテンツ宣言ファイルの例です。Intlayer はこれらを next-intl が消費できるメッセージファイルにコンパイルします。
|
|
163
|
-
|
|
164
|
-
```typescript fileName="**/*.content.ts" contentDeclarationFormat="typescript"
|
|
165
|
-
import { t, type Dictionary } from "intlayer";
|
|
166
|
-
|
|
167
|
-
const content = {
|
|
168
|
-
key: "my-component",
|
|
169
|
-
content: {
|
|
170
|
-
helloWorld: t({
|
|
171
|
-
en: "Hello World",
|
|
172
|
-
es: "Hola Mundo",
|
|
173
|
-
fr: "Bonjour le monde",
|
|
174
|
-
}),
|
|
175
|
-
},
|
|
176
|
-
} satisfies Dictionary;
|
|
177
|
-
|
|
178
|
-
export default content;
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
```javascript fileName="**/*.content.mjs" contentDeclarationFormat="esm"
|
|
182
|
-
import { t } from "intlayer";
|
|
183
|
-
|
|
184
|
-
/** @type {import('intlayer').Dictionary} */
|
|
185
|
-
const content = {
|
|
186
|
-
key: "my-component",
|
|
187
|
-
content: {
|
|
188
|
-
helloWorld: t({
|
|
189
|
-
en: "Hello World",
|
|
190
|
-
es: "Hola Mundo",
|
|
191
|
-
fr: "Bonjour le monde",
|
|
77
|
+
plugins: [
|
|
78
|
+
syncJSON({
|
|
79
|
+
source: ({ key, locale }) => `./messages/${locale}/${key}.json`,
|
|
192
80
|
}),
|
|
193
|
-
|
|
81
|
+
],
|
|
194
82
|
};
|
|
195
83
|
|
|
196
|
-
export default
|
|
84
|
+
export default config;
|
|
197
85
|
```
|
|
198
86
|
|
|
199
|
-
|
|
200
|
-
const { t } = require("intlayer");
|
|
87
|
+
`syncJSON` プラグインは JSON を自動的にラップします。コンテンツの構造を変更することなく、JSON ファイルの読み書きを行います。
|
|
201
88
|
|
|
202
|
-
|
|
203
|
-
key: "my-component",
|
|
204
|
-
content: {
|
|
205
|
-
helloWorld: t({
|
|
206
|
-
en: "Hello World",
|
|
207
|
-
es: "Hola Mundo",
|
|
208
|
-
fr: "Bonjour le monde",
|
|
209
|
-
}),
|
|
210
|
-
},
|
|
211
|
-
};
|
|
212
|
-
```
|
|
89
|
+
JSON と intlayer のコンテンツ宣言ファイル(`.content` ファイル)を共存させたい場合、Intlayer は以下の手順で処理します:
|
|
213
90
|
|
|
214
|
-
|
|
215
|
-
{
|
|
216
|
-
"$schema": "https://intlayer.org/schema.json",
|
|
217
|
-
"key": "my-component",
|
|
218
|
-
"content": {
|
|
219
|
-
"helloWorld": {
|
|
220
|
-
"nodeType": "translation",
|
|
221
|
-
"translation": {
|
|
222
|
-
"en": "Hello World",
|
|
223
|
-
"fr": "Bonjour le monde",
|
|
224
|
-
"es": "Hola Mundo"
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
```
|
|
230
|
-
|
|
231
|
-
### next-intl メッセージのビルド
|
|
91
|
+
1. JSON ファイルとコンテンツ宣言ファイルの両方を読み込み、intlayer の辞書に変換します。
|
|
232
92
|
|
|
233
|
-
|
|
93
|
+
2. JSONとコンテンツ宣言ファイル間に競合がある場合、Intlayerはすべての辞書をマージします。プラグインの優先順位やコンテンツ宣言ファイルの優先順位に応じて処理されます(すべて設定可能です)。
|
|
234
94
|
|
|
235
|
-
|
|
236
|
-
npx intlayer dictionaries build
|
|
237
|
-
```
|
|
95
|
+
CLIを使用してJSONの翻訳を行った場合やCMSを使用した場合、Intlayerは新しい翻訳でJSONファイルを更新します。
|
|
238
96
|
|
|
239
|
-
|
|
240
|
-
yarn intlayer build
|
|
241
|
-
```
|
|
97
|
+
## Git設定
|
|
242
98
|
|
|
243
|
-
|
|
244
|
-
pnpm intlayer build
|
|
245
|
-
```
|
|
99
|
+
自動生成されたIntlayerファイルは無視することを推奨します:
|
|
246
100
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
.
|
|
251
|
-
└── intl
|
|
252
|
-
└── messages
|
|
253
|
-
└── /ja
|
|
254
|
-
└── my-content.json
|
|
255
|
-
└── /fr
|
|
256
|
-
└── my-content.json
|
|
257
|
-
└── /es
|
|
258
|
-
└── my-content.json
|
|
101
|
+
```plaintext fileName=".gitignore"
|
|
102
|
+
# Intlayerによって生成されたファイルを無視
|
|
103
|
+
.intlayer
|
|
259
104
|
```
|
|
260
105
|
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
### 次.js アプリで next-intl を使用する
|
|
264
|
-
|
|
265
|
-
> 詳細については、公式の [next-intl usage docs](https://github.com/amannn/next-intl#readme) を参照してください。
|
|
266
|
-
|
|
267
|
-
1. **ミドルウェアを作成する (オプション)**:
|
|
268
|
-
自動的なロケール検出またはリダイレクションを管理したい場合、next-intl の [createMiddleware](https://github.com/amannn/next-intl#createMiddleware) を使用してください。
|
|
269
|
-
|
|
270
|
-
```typescript fileName="middleware.ts"
|
|
271
|
-
import createMiddleware from "next-intl/middleware";
|
|
272
|
-
import { NextResponse } from "next/server";
|
|
273
|
-
|
|
274
|
-
export default createMiddleware({
|
|
275
|
-
locales: ["ja", "fr", "es"],
|
|
276
|
-
defaultLocale: "ja",
|
|
277
|
-
});
|
|
278
|
-
|
|
279
|
-
export const config = {
|
|
280
|
-
matcher: ["/((?!api|_next|.*\\..*).*)"],
|
|
281
|
-
};
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
2. **メッセージを読み込むための `layout.tsx` または `_app.tsx` を作成する:**
|
|
285
|
-
App Router(Next.js 13+)を使用している場合は、レイアウトを作成します。
|
|
286
|
-
|
|
287
|
-
```typescript fileName="app/[locale]/layout.tsx"
|
|
288
|
-
import { NextIntlClientProvider } from 'next-intl';
|
|
289
|
-
import { notFound } from 'next/navigation';
|
|
290
|
-
import React, { ReactNode } from 'react';
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
export default async function RootLayout({
|
|
294
|
-
children,
|
|
295
|
-
params
|
|
296
|
-
}: {
|
|
297
|
-
children: ReactNode;
|
|
298
|
-
params: { locale: string };
|
|
299
|
-
}) {
|
|
300
|
-
let messages;
|
|
301
|
-
try {
|
|
302
|
-
messages = (await import(`../../intl/messages/${params.locale}.json`)).default;
|
|
303
|
-
} catch (error) {
|
|
304
|
-
notFound();
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
return (
|
|
308
|
-
<html lang={params.locale}>
|
|
309
|
-
<body>
|
|
310
|
-
<NextIntlClientProvider locale={params.locale} messages={messages}>
|
|
311
|
-
{children}
|
|
312
|
-
</NextIntlClientProvider>
|
|
313
|
-
</body>
|
|
314
|
-
</html>
|
|
315
|
-
);
|
|
316
|
-
}
|
|
317
|
-
```
|
|
318
|
-
|
|
319
|
-
Pages Router(Next.js 12 以下)を使用している場合は、`_app.tsx` でメッセージを読み込みます。
|
|
320
|
-
|
|
321
|
-
```typescript fileName="pages/_app.tsx"
|
|
322
|
-
import type { AppProps } from 'next/app';
|
|
323
|
-
import { NextIntlProvider } from 'next-intl';
|
|
324
|
-
|
|
325
|
-
function MyApp({ Component, pageProps }: AppProps) {
|
|
326
|
-
return (
|
|
327
|
-
<NextIntlProvider locale={pageProps.locale} messages={pageProps.messages}>
|
|
328
|
-
<Component {...pageProps} />
|
|
329
|
-
</NextIntlProvider>
|
|
330
|
-
);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
export default MyApp;
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
3. **サーバーサイドでメッセージを取得する (Pages Router の例):**
|
|
337
|
-
|
|
338
|
-
```typescript fileName="pages/index.tsx"
|
|
339
|
-
import { GetServerSideProps } from "next";
|
|
340
|
-
import HomePage from "../components/HomePage";
|
|
341
|
-
|
|
342
|
-
export default HomePage;
|
|
343
|
-
|
|
344
|
-
export const getServerSideProps: GetServerSideProps = async ({ locale }) => {
|
|
345
|
-
const messages = (await import(`../intl/messages/${locale}.json`)).default;
|
|
346
|
-
|
|
347
|
-
return {
|
|
348
|
-
props: {
|
|
349
|
-
locale,
|
|
350
|
-
messages,
|
|
351
|
-
},
|
|
352
|
-
};
|
|
353
|
-
};
|
|
354
|
-
```
|
|
355
|
-
|
|
356
|
-
### Next.js コンポーネントでのコンテンツの使用
|
|
357
|
-
|
|
358
|
-
メッセージが next-intl に読み込まれると、`useTranslations()` フックを介してコンポーネント内で使用できます:
|
|
359
|
-
|
|
360
|
-
```typescript fileName="src/components/MyComponent/index.tsx" codeFormat="typescript"
|
|
361
|
-
import type { FC } from "react";
|
|
362
|
-
import { useTranslations } from 'next-intl';
|
|
363
|
-
|
|
364
|
-
const MyComponent: FC = () => {
|
|
365
|
-
const t = useTranslations('my-component');
|
|
366
|
-
// 'my-component' は Intlayer 内のコンテンツキーに対応します
|
|
367
|
-
|
|
368
|
-
return (
|
|
369
|
-
<div>
|
|
370
|
-
<h1>{t('helloWorld')}</h1>
|
|
371
|
-
</div>
|
|
372
|
-
);
|
|
373
|
-
};
|
|
374
|
-
|
|
375
|
-
export default MyComponent;
|
|
376
|
-
```
|
|
106
|
+
これらのファイルはビルドプロセス中に再生成可能であり、バージョン管理にコミットする必要はありません。
|
|
377
107
|
|
|
378
|
-
|
|
379
|
-
import { useTranslations } from "next-intl";
|
|
108
|
+
### VS Code拡張機能
|
|
380
109
|
|
|
381
|
-
|
|
382
|
-
const t = useTranslations("my-component");
|
|
110
|
+
開発者体験を向上させるために、公式の**Intlayer VS Code拡張機能**をインストールしてください:
|
|
383
111
|
|
|
384
|
-
|
|
385
|
-
<div>
|
|
386
|
-
<h1>{t("helloWorld")}</h1>
|
|
387
|
-
</div>
|
|
388
|
-
);
|
|
389
|
-
}
|
|
390
|
-
```
|
|
112
|
+
[VS Codeマーケットプレイスからインストール](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|
|
391
113
|
|
|
392
|
-
|
|
114
|
+
[VS Codeマーケットプレイスからインストール](https://marketplace.visualstudio.com/items?itemName=intlayer.intlayer-vs-code-extension)
|