Skip to content

Commit deadcf2

Browse files
authored
i18n(ja): updatei18n.mdx (#3703)
1 parent b870fd1 commit deadcf2

File tree

2 files changed

+383
-25
lines changed

2 files changed

+383
-25
lines changed

docs/src/content/docs/ja/guides/i18n.mdx

Lines changed: 166 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ Starlightは、ルーティング、フォールバックコンテンツ、右
6666

6767
</Steps>
6868

69+
より高度なi18nシナリオの場合、[Astroの`i18n`設定](https://docs.astro.build/ja/guides/internationalization/#configure-i18n-routing)オプションを使用した国際化の設定も可能です。
70+
6971
### ルートロケールを使用する
7072

7173
「ルート」ロケールを使用すると、パスにi18nプレフィックスを付けずにある言語を提供できます。たとえば英語をルートロケールとすると、英語のページパスは`/en/about`ではなく`/about`のようになります。
@@ -142,6 +144,33 @@ Starlightは、すべての言語で同等のページが作成されること
142144

143145
ある言語の翻訳がまだである場合、Starlightはそのページのコンテンツを(`defaultLocale`で設定する)デフォルト言語で表示します。たとえば、概要(about)ページのフランス語版をまだ作成していない場合、デフォルト言語が英語であれば、`/fr/about`を訪れた人には未翻訳であるという通知とともに英語のコンテンツが表示されます。これにより、まずデフォルト言語にコンテンツを追加し、そして翻訳者が時間を掛けて徐々に翻訳していくことが可能となります。
144146

147+
## サイトタイトルを翻訳する
148+
149+
デフォルトでは、Starlightはすべての言語で同じサイトタイトルを使用します。各ロケールのタイトルをカスタマイズする必要がある場合は、Starlightのオプションで[`title`](/ja/reference/configuration/#title必須)にオブジェクトを渡すことができます。
150+
151+
```diff lang="js"
152+
// astro.config.mjs
153+
import { defineConfig } from 'astro/config';
154+
import starlight from '@astrojs/starlight';
155+
156+
export default defineConfig({
157+
integrations: [
158+
starlight({
159+
- title: 'My Docs',
160+
+ title: {
161+
+ en: 'My Docs',
162+
+ 'zh-CN': '我的文档',
163+
+ },
164+
defaultLocale: 'en',
165+
locales: {
166+
en: { label: 'English' },
167+
'zh-cn': { label: '简体中文', lang: 'zh-CN' },
168+
},
169+
}),
170+
],
171+
});
172+
```
173+
145174
## StarlightのUIを翻訳する
146175

147176
import LanguagesList from '~/components/languages-list.astro';
@@ -156,16 +185,17 @@ Starlightでは、読者が選択した言語でサイト全体を体験でき
156185

157186
<Steps>
158187

159-
1. 設定がまだであれば`src/content/config.ts``i18n`データコレクションを設定します。
188+
1. 現状が未設定の場合は`src/content.config.ts``i18n`データコレクションを設定します。
160189

161-
```diff lang="js" ins=/, (i18nSchema)/
162-
// src/content/config.ts
190+
```diff lang="js" ins=/, (i18nLoader|i18nSchema)/
191+
// src/content.config.ts
163192
import { defineCollection } from 'astro:content';
193+
import { docsLoader, i18nLoader } from '@astrojs/starlight/loaders';
164194
import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';
165195

166196
export const collections = {
167-
docs: defineCollection({ schema: docsSchema() }),
168-
+ i18n: defineCollection({ type: 'data', schema: i18nSchema() }),
197+
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
198+
+ i18n: defineCollection({ loader: i18nLoader(), schema: i18nSchema() }),
169199
};
170200
```
171201

@@ -187,7 +217,7 @@ Starlightでは、読者が選択した言語でサイト全体を体験でき
187217

188218
<UIStringsList />
189219

190-
Starlightのコードブロックは、[Expressive Code](https://github.com/expressive-code/expressive-code)ライブラリによって動作しています。`expressiveCode`キーを使用して、同じJSONファイルでUI文字列の翻訳について設定できます。
220+
Starlightのコードブロックは、[Expressive Code](https://expressive-code.com/)ライブラリによって動作しています。`expressiveCode`キーを使用して、同じJSONファイルでUI文字列の翻訳について設定できます。
191221

192222
```json
193223
{
@@ -221,14 +251,15 @@ Starlightでは、読者が選択した言語でサイト全体を体験でき
221251
`i18nSchema()`オプションの`extend`を設定することで、サイトの翻訳辞書にカスタムキーを追加できます。以下の例では、オプションの`custom.label`キーが新たにデフォルトのキーに追加されています。
222252

223253
```diff lang="js"
224-
// src/content/config.ts
254+
// src/content.config.ts
225255
import { defineCollection, z } from 'astro:content';
256+
import { docsLoader, i18nLoader } from '@astrojs/starlight/loaders';
226257
import { docsSchema, i18nSchema } from '@astrojs/starlight/schema';
227258

228259
export const collections = {
229-
docs: defineCollection({ schema: docsSchema() }),
260+
docs: defineCollection({ loader: docsLoader(), schema: docsSchema() }),
230261
i18n: defineCollection({
231-
type: 'data',
262+
loader: i18nLoader(),
232263
schema: i18nSchema({
233264
+ extend: z.object({
234265
+ 'custom.label': z.string().optional(),
@@ -238,4 +269,129 @@ export const collections = {
238269
};
239270
```
240271

241-
コンテンツコレクションのスキーマについて詳しくは、Astroドキュメントの[「コレクションスキーマの定義」](https://docs.astro.build/ja/guides/content-collections/#コレクションスキーマの定義)を参照してください。
272+
コンテンツコレクションのスキーマについて詳しくは、Astroドキュメントの[「コレクションスキーマの定義」](https://docs.astro.build/ja/guides/content-collections/#defining-the-collection-schema)を参照してください。
273+
274+
## UI翻訳を使用する
275+
276+
Starlightの[組み込みUI文字列](/ja/guides/i18n/#starlightのuiを翻訳する)[ユーザー定義のUI文字列](/ja/guides/i18n/#翻訳スキーマを拡張する)、および[プラグインが提供するUI文字列](/ja/reference/plugins/#injecttranslations)には、[i18next](https://www.i18next.com/)を利用した共通のAPIを通じてアクセスできます。この共通のAPIは、[文字列補間](https://www.i18next.com/translation-function/interpolation)[複数形対応](https://www.i18next.com/translation-function/plurals)などの機能もサポートしています。
277+
278+
Astroコンポーネントにおいて、この共通のAPIは[グローバル`Astro`オブジェクト](https://docs.astro.build/ja/reference/api-reference/#locals)の一部として`Astro.locals.t`から利用できます。
279+
280+
```astro title="example.astro"
281+
<p dir={Astro.locals.t.dir()}>
282+
{Astro.locals.t('404.text')}
283+
</p>
284+
```
285+
286+
[エンドポイント](https://docs.astro.build/ja/guides/endpoints/)でもこの共通のAPIを使用できます。`locals`オブジェクトは[エンドポイントコンテキスト](https://docs.astro.build/ja/reference/api-reference/#locals)の一部として利用できます。
287+
288+
```ts title="src/pages/404.ts"
289+
export const GET = (context) => {
290+
return new Response(context.locals.t('404.text'));
291+
};
292+
```
293+
294+
Starlightプラグインのコンテキストでは、[`useTranslations()`](/ja/reference/plugins/#usetranslations)ヘルパーを使用して、特定の言語に対するこの共通のAPIにアクセスできます。詳しくは、[プラグインリファレンス](/ja/reference/plugins/)を参照してください。
295+
296+
### UI文字列をレンダリングする
297+
298+
`locals.t()`関数を使用してUI文字列をレンダリングします。これはi18nextの`t()`関数のインスタンスで、最初の引数としてUI文字列のキーを受け取り、現在の言語に対応する翻訳を返します。
299+
300+
たとえば、以下の内容のカスタム翻訳ファイルがあるとします。
301+
302+
```json title="src/content/i18n/en.json"
303+
{
304+
"link.astro": "Astro documentation",
305+
"link.astro.custom": "Astro documentation for {{feature}}"
306+
}
307+
```
308+
309+
最初のUI文字列は、`t()`関数に`'link.astro'`を渡すことでレンダリングできます。
310+
311+
```astro {3}
312+
<!-- src/components/Example.astro -->
313+
<a href="https://docs.astro.build/">
314+
{Astro.locals.t('link.astro')}
315+
</a>
316+
<!-- レンダリング結果: <a href="...">Astro documentation</a> -->
317+
```
318+
319+
2番目のUI文字列は、`{{feature}}`のプレースホルダーにi18nextの[補間構文](https://www.i18next.com/translation-function/interpolation)を使用しています。`feature`の値は、`t()`の第2引数として渡すオプションオブジェクトで設定する必要があります。
320+
321+
```astro {3}
322+
<!-- src/components/Example.astro -->
323+
<a href="https://docs.astro.build/en/guides/astro-db/">
324+
{Astro.locals.t('link.astro.custom', { feature: 'Astro DB' })}
325+
</a>
326+
<!-- レンダリング結果: <a href="...">Astro documentation for Astro DB</a> -->
327+
```
328+
329+
補間やフォーマットなどの`t()`関数の使い方について詳しくは、[i18nextのドキュメント](https://www.i18next.com/overview/api#t)を参照してください。
330+
331+
### 高度なAPI
332+
333+
#### `t.all()`
334+
335+
`locals.t.all()`関数は、現在のロケールで利用可能なすべてのUI文字列を含むオブジェクトを返します。
336+
337+
```astro
338+
---
339+
// src/components/Example.astro
340+
const allStrings = Astro.locals.t.all();
341+
// ^
342+
// {
343+
// "skipLink.label": "Skip to content",
344+
// "search.label": "Search",
345+
// …
346+
// }
347+
---
348+
```
349+
350+
#### `t.exists()`
351+
352+
翻訳キーが存在するかどうかを確認するには、`locals.t.exists()`関数の最初の引数に翻訳キーを渡します。特定のロケールに対する翻訳が存在するかどうかを確認する必要がある場合は、オプションの第2引数を渡します。
353+
354+
```astro
355+
---
356+
// src/components/Example.astro
357+
const keyExists = Astro.locals.t.exists('a.key');
358+
// ^ true
359+
const keyExistsInFrench = Astro.locals.t.exists('other.key', { lngs: ['fr'] });
360+
// ^ false
361+
---
362+
```
363+
364+
詳しくは、[i18nextドキュメントの`exists()`リファレンス](https://www.i18next.com/overview/api#exists)を参照してください。
365+
366+
#### `t.dir()`
367+
368+
`locals.t.dir()`関数は、現在のロケールまたは指定したロケールのテキスト方向を返します。
369+
370+
```astro
371+
---
372+
// src/components/Example.astro
373+
const currentDirection = Astro.locals.t.dir();
374+
// ^
375+
// 'ltr'
376+
const arabicDirection = Astro.locals.t.dir('ar');
377+
// ^
378+
// 'rtl'
379+
---
380+
```
381+
382+
詳しくは、[i18nextドキュメントの`dir()`リファレンス](https://www.i18next.com/overview/api#dir)を参照してください。
383+
384+
## 現在のロケールにアクセスする
385+
386+
`.astro`コンポーネントで現在のロケールを読み取るためには、[`Astro.currentLocale`](https://docs.astro.build/ja/reference/api-reference/#currentlocale)を使用できます。
387+
388+
以下の例では、現在のロケールを読み取り、[`getRelativeLocaleUrl()`](https://docs.astro.build/ja/reference/modules/astro-i18n/#getrelativelocaleurl)ヘルパーと組み合わせて、現在の言語での概要ページへのリンクを生成しています。
389+
390+
```astro
391+
---
392+
// src/components/AboutLink.astro
393+
import { getRelativeLocaleUrl } from 'astro:i18n';
394+
---
395+
396+
<a href={getRelativeLocaleUrl(Astro.currentLocale ?? 'en', 'about')}>About</a>
397+
```

0 commit comments

Comments
 (0)