@intlayer/docs 9.0.0-canary.2 → 9.0.0-canary.3

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.
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  createdAt: 2025-03-13
3
- updatedAt: 2025-12-13
3
+ updatedAt: 2026-06-21
4
4
  title: Sync JSON プラグイン
5
5
  description: Intlayerの辞書をサードパーティのi18n JSONファイル(i18next、next-intl、react-intl、vue-i18nなど)と同期します。既存のi18nを維持しながら、Intlayerでメッセージの管理、翻訳、テストを行えます。
6
6
  keywords:
@@ -24,6 +24,9 @@ slugs:
24
24
  - sync-json
25
25
  youtubeVideo: https://www.youtube.com/watch?v=MpGMxniDHNg
26
26
  history:
27
+ - version: 9.0.0
28
+ date: 2026-06-21
29
+ changes: "splitKeys オプションを追加(next-intl / react-intl の単一ファイルレイアウト向けに、トップレベルの名前空間キーごとに1つの辞書)"
27
30
  - version: 7.5.0
28
31
  date: 2025-12-13
29
32
  changes: "ICUおよびi18next形式のサポートを追加"
@@ -62,7 +65,63 @@ pnpm add -D @intlayer/sync-json-plugin
62
65
  npm i -D @intlayer/sync-json-plugin
63
66
  ```
64
67
 
65
- ## クイックスタート
68
+ ## Plugins
69
+
70
+ このパッケージは2つのプラグインを提供します:
71
+
72
+ - `loadJSON`: JSONファイルをIntlayer辞書にロードします。
73
+ - このプラグインは、ソースからJSONファイルをロードし、Intlayer辞書にロードするために使用されます。コードベース全体をスキャンし、特定のJSONファイルを検索できます。
74
+ このプラグインは以下の場合に使用できます。
75
+ - JSONのロードに特定の場所を課すi18nライブラリ(例:`next-intl`、`i18next`、`react-intl`、`vue-i18n`など)を使用しているが、コンテンツ宣言をコードベースの好きな場所に配置したい場合。
76
+ - リモートソース(例:CMS、APIなど)からメッセージをフェッチし、JSONファイルにメッセージを保存したい場合。
77
+
78
+ > 内部的には、このプラグインはコードベース全体をスキャンし、特定のJSONファイルを検索してIntlayer辞書にロードします。
79
+ > このプラグインは出力と翻訳をJSONファイルに書き戻さないことに注意してください。
80
+
81
+ - `syncJSON`: JSONファイルをIntlayer辞書と同期します。
82
+ - このプラグインは、JSONファイルをIntlayer辞書と同期するために使用されます。指定された場所をスキャンし、特定のJSONファイルのパターンに一致するJSONをロードできます。このプラグインは、別のi18nライブラリを使用しながらIntlayerの利点を得たい場合に便利です。
83
+
84
+ ## 両方のプラグインを使用する
85
+
86
+ ```ts fileName="intlayer.config.ts"
87
+ import { Locales, type IntlayerConfig } from "intlayer";
88
+ import { loadJSON, syncJSON } from "@intlayer/sync-json-plugin";
89
+
90
+ const config: IntlayerConfig = {
91
+ internationalization: {
92
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
93
+ defaultLocale: Locales.ENGLISH,
94
+ },
95
+
96
+ // 現在のJSONファイルをIntlayerの辞書と同期させる
97
+ plugins: [
98
+ /**
99
+ * src内のパターン {key}.i18n json に一致するすべてのJSONファイルをロードします
100
+ */
101
+ loadJSON({
102
+ source: ({ key }) => `./src/**/${key}.i18n.json`,
103
+ locale: Locales.ENGLISH,
104
+ priority: 1, // これらのJSONファイルが `./locales/en/${key}.json` のファイルよりも優先されるようにします
105
+ format: "intlayer", // JSONコンテンツのフォーマット
106
+ }),
107
+ /**
108
+ * ロケールディレクトリ内のJSONファイルに、出力と翻訳をロードして書き戻します
109
+ */
110
+ syncJSON({
111
+ format: "i18next",
112
+ source: ({ key, locale }) => `./locales/${locale}/${key}.json`,
113
+ priority: 0,
114
+ format: "i18next",
115
+ }),
116
+ ],
117
+ };
118
+
119
+ export default config;
120
+ ```
121
+
122
+ ## `syncJSON` plugin
123
+
124
+ ### クイックスタート
66
125
 
67
126
  `intlayer.config.ts`にプラグインを追加し、既存のJSON構造を指定します。
68
127
 
@@ -81,6 +140,7 @@ const config: IntlayerConfig = {
81
140
  syncJSON({
82
141
  // ロケールごと、名前空間ごとのレイアウト(例:next-intl、名前空間付きのi18next)
83
142
  source: ({ key, locale }) => `./locales/${locale}/${key}.json`,
143
+ format: "icu",
84
144
  }),
85
145
  ],
86
146
  };
@@ -91,14 +151,27 @@ export default config;
91
151
  代替案:ロケールごとに単一ファイル(i18next/react-intlのセットアップで一般的):
92
152
 
93
153
  ```ts fileName="intlayer.config.ts"
94
- plugins: [
95
- syncJSON({
96
- source: ({ locale }) => `./locales/${locale}.json`,
97
- }),
98
- ];
154
+ import { Locales, type IntlayerConfig } from "intlayer";
155
+ import { syncJSON } from "@intlayer/sync-json-plugin";
156
+
157
+ const config: IntlayerConfig = {
158
+ internationalization: {
159
+ locales: [Locales.ENGLISH, Locales.FRENCH],
160
+ defaultLocale: Locales.ENGLISH,
161
+ },
162
+ plugins: [
163
+ syncJSON({
164
+ format: "i18next",
165
+ source: ({ locale }) => `./locales/${locale}.json`,
166
+ format: "i18next",
167
+ }),
168
+ ],
169
+ };
170
+
171
+ export default config;
99
172
  ```
100
173
 
101
- ### 動作の仕組み
174
+ #### 動作の仕組み
102
175
 
103
176
  - 読み込み:プラグインは`source`ビルダーからJSONファイルを検出し、それらをIntlayerの辞書として読み込みます。
104
177
  - 書き込み:ビルドと補完の後、ローカライズされたJSONを同じパスに書き戻します(フォーマットの問題を避けるために末尾に改行を追加)。
@@ -112,6 +185,7 @@ syncJSON({
112
185
  location?: string, // オプションのラベル、デフォルト: "plugin"
113
186
  priority?: number, // コンフリクト解決のためのオプションの優先度、デフォルト: 0
114
187
  format?: 'intlayer' | 'icu' | 'i18next', // オプションのフォーマッター、Intlayerランタイム互換性のために使用
188
+ splitKeys?: boolean, // オプション、単一ファイルをトップレベルキーごとに1つの辞書に分割(自動検出)
115
189
  });
116
190
  ```
117
191
 
@@ -136,11 +210,43 @@ syncJSON({
136
210
  }),
137
211
  ```
138
212
 
139
- ## 複数の JSON ソースと優先度
213
+ #### `splitKeys` (boolean)
214
+
215
+ **ファーストレベルのキーが名前空間である**単一のJSONファイルを、ファイル全体を保持する単一の辞書ではなく、トップレベルキーごとに1つの辞書にするかどうかを制御します。
216
+
217
+ これは、`next-intl`や`react-intl`のようなライブラリの名前空間モデルと一致します。これらのライブラリでは、1つの`messages/{locale}.json`ファイルが、そのファーストレベルのキーによって複数の名前空間をグループ化し、それぞれが独立してアドレス指定されます(例:`useTranslations('Hero')`は`Hero`辞書に解決されます)。
218
+
219
+ - `undefined` (デフォルト): **自動検出** — `source`パターンに`{key}`セグメントがない場合(1つのファイルがすべての名前空間を保持する場合)にファイルが分割され、それ以外の場合(キーごとに1つのファイル)は単一の辞書として保持されます。
220
+ - `true`: 常に各トップレベルキーを独自の辞書に分割します。
221
+ - `false`: 分割しません。ファイル全体が単一の辞書になります。
222
+
223
+ 単一の`messages/{locale}.json`ファイルが与えられた場合:
224
+
225
+ ```json fileName="messages/en.json"
226
+ {
227
+ "Hero": { "title": "Full-stack developer" },
228
+ "Nav": { "work": "Work", "about": "About" },
229
+ "About": { "lead": "I build apps end to end." }
230
+ }
231
+ ```
232
+
233
+ ```ts fileName="intlayer.config.ts"
234
+ syncJSON({
235
+ format: "icu",
236
+ source: ({ locale }) => `./messages/${locale}.json`,
237
+ // splitKeys: true, // パターンに`{key}`セグメントがないため、暗黙的に有効
238
+ }),
239
+ ```
240
+
241
+ これにより、`Hero`、`Nav`、`About`の3つの辞書が生成され、`useTranslations('Hero')` (next-intl) が正しく解決されます。書き戻し時には、すべての名前空間が同じロケールごとのファイルに再結合されます。
242
+
243
+ > `source`に明示的な`{key}`セグメントを保持している場合(例:`./locales/${locale}/${key}.json`)、各ファイルはすでに1つの名前空間であるため、分割はデフォルトで無効になります。
244
+
245
+ ### 複数の JSON ソースと優先度
140
246
 
141
247
  複数の `syncJSON` プラグインを追加して異なる JSON ソースを同期することができます。これは、複数の i18n ライブラリや異なる JSON 構造をプロジェクトで使用している場合に便利です。
142
248
 
143
- ### 優先度システム
249
+ #### 優先度システム
144
250
 
145
251
  複数のプラグインが同じ辞書キーを対象とする場合、`priority` パラメータがどのプラグインが優先されるかを決定します:
146
252
 
@@ -189,73 +295,140 @@ const config: IntlayerConfig = {
189
295
  export default config;
190
296
  ```
191
297
 
192
- ### コンフリクト解決
298
+ ## Load JSON plugin
193
299
 
194
- 同じ翻訳キーが複数のJSONソースに存在する場合:
300
+ ### クイックスタート
195
301
 
196
- 1. 最も優先度の高いプラグインが最終的な値を決定します
197
- 2. 優先度の低いソースは、欠落しているキーのフォールバックとして使用されます
198
- 3. これにより、レガシー翻訳を維持しつつ、新しい構造へ段階的に移行できます
302
+ `intlayer.config.ts`にプラグインを追加して、既存のJSONファイルをIntlayer辞書として取り込みます。このプラグインは読み取り専用です(ディスクへの書き込みは行いません):
199
303
 
200
- ## 統合
304
+ ```ts fileName="intlayer.config.ts"
305
+ import { Locales, type IntlayerConfig } from "intlayer";
306
+ import { loadJSON } from "@intlayer/sync-json-plugin";
201
307
 
202
- 以下は一般的なマッピング例です。ランタイムは変更せず、プラグインのみを追加してください。
308
+ const config: IntlayerConfig = {
309
+ internationalization: {
310
+ locales: [Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH],
311
+ defaultLocale: Locales.ENGLISH,
312
+ },
203
313
 
204
- ### i18next
314
+ plugins: [
315
+ // ソースツリー内のどこにでも配置されたJSONメッセージを取り込む
316
+ loadJSON({
317
+ source: ({ key }) => `./src/**/${key}.i18n.json`,
318
+ // プラグインインスタンスごとに単一のロケールをロード(設定のdefaultLocaleにデフォルト設定)
319
+ locale: Locales.ENGLISH,
320
+ priority: 0,
321
+ }),
322
+ ],
323
+ };
205
324
 
206
- 典型的なファイル構成: `./public/locales/{locale}/{namespace}.json` または `./locales/{locale}/{namespace}.json`。
325
+ export default config;
326
+ ```
327
+
328
+ 代替案:ロケールごとのレイアウト、読み取り専用(選択されたロケールのみがロードされます):
207
329
 
208
330
  ```ts fileName="intlayer.config.ts"
209
- import { syncJSON } from "@intlayer/sync-json-plugin";
331
+ import { Locales, type IntlayerConfig } from "intlayer";
332
+ import { loadJSON } from "@intlayer/sync-json-plugin";
210
333
 
211
- export default {
334
+ const config: IntlayerConfig = {
335
+ internationalization: {
336
+ locales: [Locales.ENGLISH, Locales.FRENCH],
337
+ defaultLocale: Locales.ENGLISH,
338
+ },
212
339
  plugins: [
213
- syncJSON({
214
- format: "i18next",
340
+ loadJSON({
341
+ // このパターンからはLocales.FRENCHのファイルのみがロードされます
215
342
  source: ({ key, locale }) => `./locales/${locale}/${key}.json`,
343
+ locale: Locales.FRENCH,
216
344
  }),
217
345
  ],
218
346
  };
347
+
348
+ export default config;
219
349
  ```
220
350
 
221
- ### next-intl
351
+ ### 動作の仕組み
222
352
 
223
- ロケールごとのJSONメッセージ(多くの場合 `./messages/{locale}.json`)または名前空間ごと。
353
+ - 検出:`source`ビルダーからグロブを構築し、一致するJSONファイルを収集します。
354
+ - 取り込み:各JSONファイルを提供された`locale`を持つIntlayer辞書としてロードします。
355
+ - 読み取り専用:出力ファイルの書き込みやフォーマットは行いません。ラウンドトリップ同期が必要な場合は`syncJSON`を使用してください。
356
+ - 自動補完対応:`fill`パターンを定義し、`intlayer content fill`がCLI経由で不足しているキーを埋めることができます。
224
357
 
225
- ```ts fileName="intlayer.config.ts"
226
- plugins: [
227
- syncJSON({
228
- source: ({ locale, key }) => `./messages/${locale}/${key}.json`,
229
- }),
230
- ];
358
+ ### API
359
+
360
+ ```ts
361
+ loadJSON({
362
+ // JSONへのパスを構築します。構造にロケールセグメントがない場合、`locale`はオプションです
363
+ source: ({ key, locale }) => string,
364
+
365
+ // このプラグインインスタンスによってロードされる辞書のターゲットロケール
366
+ // デフォルトは configuration.internationalization.defaultLocale
367
+ locale?: Locale,
368
+
369
+ // ソースを識別するためのオプションのラベル
370
+ location?: string, // デフォルト: "plugin"
371
+
372
+ // 他のソースとのコンフリクト解決に使用される優先度
373
+ priority?: number, // デフォルト: 0
374
+
375
+ // JSONコンテンツのオプションのフォーマッター
376
+ format?: 'intlayer' | 'icu' | 'i18next', // デフォルト: 'intlayer'
377
+
378
+ // 単一ファイルをトップレベルキーごとに1つの辞書に分割(自動検出)
379
+ splitKeys?: boolean,
380
+ });
231
381
  ```
232
382
 
233
- 参照: `docs/ja/intlayer_with_next-intl.md`。
383
+ #### `format` ('intlayer' | 'icu' | 'i18next')
234
384
 
235
- ### react-intl
385
+ JSONファイルをロードする際に辞書コンテンツに使用するフォーマッターを指定します。これにより、さまざまなi18nライブラリと互換性のある異なるメッセージフォーマット構文を使用できます。
236
386
 
237
- ロケールごとに単一のJSONが一般的:
387
+ - `'intlayer'`: デフォルトのIntlayerフォーマッター(デフォルト)。
388
+ - `'icu'`: ICUメッセージフォーマットを使用します(react-intl、vue-i18nなどのライブラリと互換性があります)。
389
+ - `'i18next'`: i18nextメッセージフォーマットを使用します(i18next、next-i18next、Solid-i18nextと互換性があります)。
238
390
 
239
- ```ts fileName="intlayer.config.ts"
240
- plugins: [
241
- syncJSON({
242
- source: ({ locale }) => `./locales/${locale}.json`,
243
- }),
244
- ];
391
+ **例:**
392
+
393
+ ```ts
394
+ loadJSON({
395
+ source: ({ key }) => `./src/**/${key}.i18n.json`,
396
+ locale: Locales.ENGLISH,
397
+ format: "icu", // 互換性のためにICUフォーマットを使用
398
+ }),
245
399
  ```
246
400
 
247
- ### vue-i18n
401
+ #### `splitKeys` (boolean)
248
402
 
249
- ロケールごと、またはネームスペースごとに単一ファイルを使用する場合:
403
+ [`syncJSON`](#splitkeys-boolean) と同じ動作です。単一のJSONファイルがファーストレベルのキーによって複数の名前空間をグループ化する場合、各トップレベルキーが独自の辞書になります。
250
404
 
251
- ```ts fileName="intlayer.config.ts"
252
- plugins: [
253
- syncJSON({
254
- source: ({ key, locale }) => `./src/locales/${locale}/${key}.json`,
255
- }),
256
- ];
405
+ - `undefined` (デフォルト): **自動検出** — `source`パターンに`{key}`セグメントがない場合に分割され、それ以外の場合は単一の辞書になります。
406
+ - `true` / `false`: 分割を強制または無効にします。
407
+
408
+ ```ts
409
+ loadJSON({
410
+ source: ({ locale }) => `./messages/${locale}.json`,
411
+ format: "icu",
412
+ // splitKeys auto-enabled: `Hero`、`Nav`、`About`などがそれぞれ辞書になります
413
+ }),
257
414
  ```
258
415
 
416
+ ### Behavior and conventions
417
+
418
+ - `source`マスクにロケールプレースホルダーが含まれている場合、選択された`locale`のファイルのみが取り込まれます。
419
+ - マスクに`{key}`セグメントがない場合、ファイルの各トップレベルキーがデフォルトで独自の辞書になります([`splitKeys`](#splitkeys-boolean)を参照)。代わりにファイル全体を単一の`index`辞書として読み込むには、`splitKeys: false`を設定します。
420
+ - キーは、`source`ビルダーの`{key}`プレースホルダーを置換することでファイルパスから派生します。
421
+ - プラグインは検出されたファイルのみを使用し、不足しているロケールやキーを捏造することはありません。
422
+ - `fill`パスは`source`から推測され、CLI経由で不足している値を更新するために使用されます(オプトインした場合)。
423
+
424
+ ## Conflict resolution
425
+
426
+ 同じ翻訳キーが複数のJSONソースに存在する場合:
427
+
428
+ 1. 最も優先度の高いプラグインが最終的な値を決定します
429
+ 2. 優先度の低いソースは、欠落しているキーのフォールバックとして使用されます
430
+ 3. これにより、レガシー翻訳を維持しつつ、新しい構造へ段階的に移行できます
431
+
259
432
  ## CLI
260
433
 
261
434
  同期されたJSONファイルは他の `.content` ファイルとして扱われます。つまり、すべてのintlayerコマンドが同期されたJSONファイルに対して利用可能です。以下を含みます:
@@ -268,7 +441,7 @@ plugins: [
268
441
 
269
442
  See [Intlayer CLI](https://github.com/aymericzip/intlayer/blob/main/docs/docs/ja/cli/index.md) for more details.
270
443
 
271
- ## 制限事項(現状)
444
+ ## Limitations (current)
272
445
 
273
446
  - サードパーティライブラリを対象とする場合、挿入や複数形/ICUのサポートはありません。
274
447
  - 非Intlayerランタイム向けのビジュアルエディタはまだ利用できません。