このページのコンテンツはAIを使用して翻訳されました。
英語の元のコンテンツの最新バージョンを見るIntlayerとViteおよびPreactを使用した国際化(i18n)の開始
このパッケージは開発中です。詳細についてはこの問題をご覧ください。Preact向けのIntlayerに興味がある場合は、この問題に「いいね」をしてください。
GitHub上のアプリケーションテンプレートをご覧ください。
Intlayerとは?
Intlayerは、モダンなウェブアプリケーションで多言語サポートを簡素化するために設計された革新的なオープンソースの国際化(i18n)ライブラリです。
Intlayerを使用すると、以下が可能です:
- コンポーネントレベルで宣言的な辞書を使用して翻訳を簡単に管理。
- メタデータ、ルート、およびコンテンツを動的にローカライズ。
- 自動生成された型でTypeScriptサポートを確保し、オートコンプリートとエラー検出を向上。
- 動的なロケール検出と切り替えなどの高度な機能を活用。
ViteとPreactアプリケーションでのIntlayerセットアップのステップバイステップガイド
ステップ1: 依存関係のインストール
npmを使用して必要なパッケージをインストールします:
npm install intlayer preact-intlayer vite-intlayer
intlayer
preact-intlayer IntlayerをPreactアプリケーションに統合するパッケージ。Preactの国際化のためのコンテキストプロバイダーとフックを提供します。
vite-intlayer ViteバンドラーとIntlayerを統合するためのViteプラグインを含み、ユーザーの優先ロケールを検出し、クッキーを管理し、URLリダイレクトを処理するミドルウェアを提供します。
ステップ2: プロジェクトの設定
アプリケーションの言語を設定するための設定ファイルを作成します:
import { Locales, type IntlayerConfig } from "intlayer";const config: IntlayerConfig = { internationalization: { locales: [ Locales.ENGLISH, Locales.FRENCH, Locales.SPANISH, // 他のロケール ], defaultLocale: Locales.ENGLISH, },};export default config;
この設定ファイルを通じて、ローカライズされたURL、ミドルウェアリダイレクト、クッキー名、コンテンツ宣言の場所と拡張子、コンソールでのIntlayerログの無効化などを設定できます。利用可能なパラメータの完全なリストについては、設定ドキュメントを参照してください。
ステップ3: Vite設定へのIntlayerの統合
設定にintlayerプラグインを追加します。
import { defineConfig } from "vite";import preact from "@preact/preset-vite";import { intlayerPlugin } from "vite-intlayer";// https://vitejs.dev/config/export default defineConfig({ plugins: [preact(), intlayerPlugin()],});
intlayerPlugin() Viteプラグインは、IntlayerをViteと統合するために使用されます。これにより、コンテンツ宣言ファイルのビルドが確保され、開発モードで監視されます。また、Viteアプリケーション内でIntlayer環境変数を定義し、パフォーマンスを最適化するエイリアスを提供します。
ステップ4: コンテンツの宣言
翻訳を保存するためのコンテンツ宣言を作成および管理します:
import { t, type Dictionary } from "intlayer";const appContent = { key: "app", content: { viteLogo: t({ ja: "Viteロゴ", en: "Vite logo", fr: "Logo Vite", es: "Logo Vite", }), preactLogo: t({ ja: "Preactロゴ", en: "Preact logo", fr: "Logo Preact", es: "Logo Preact", }), title: "Vite + Preact", count: t({ ja: "カウントは ", en: "count is ", fr: "le compte est ", es: "el recuento es ", }), edit: t({ ja: "<code>src/app.tsx</code>を編集して保存し、HMRをテストしてください", en: "Edit src/app.tsx and save to test HMR", fr: "Éditez src/app.tsx et enregistrez pour tester HMR", es: "Edita src/app.tsx y guarda para probar HMR", }), readTheDocs: t({ ja: "ViteとPreactのロゴをクリックして詳細を確認してください", en: "Click on the Vite and Preact logos to learn more", fr: "Cliquez sur les logos Vite et Preact pour en savoir plus", es: "Haga clic en los logotipos de Vite y Preact para obtener más información", }), },} satisfies Dictionary;export default appContent;
コンテンツ宣言は、contentDirディレクトリ(デフォルトでは./src)に含まれている限り、アプリケーション内のどこにでも定義できます。また、コンテンツ宣言ファイルの拡張子(デフォルトでは.content.{json,ts,tsx,js,jsx,mjs,mjx,cjs,cjx})と一致する必要があります。
詳細については、コンテンツ宣言ドキュメントを参照してください。
コンテンツファイルにTSXコードが含まれている場合、import { h } from "preact";をインポートするか、JSXプラグマがPreact用に正しく設定されていることを確認する必要があります。
ステップ5: コード内でIntlayerを利用する
アプリケーション全体でコンテンツ辞書にアクセスします:
import { useState } from "preact/hooks";import type { FunctionalComponent } from "preact";import preactLogo from "./assets/preact.svg"; // preact.svgを使用していると仮定import viteLogo from "/vite.svg";import "./app.css"; // CSSファイル名がapp.cssであると仮定import { IntlayerProvider, useIntlayer } from "preact-intlayer";const AppContent: FunctionalComponent = () => { const [count, setCount] = useState(0); const content = useIntlayer("app"); return ( <> <div> <a href="https://vitejs.dev" target="_blank"> <img src={viteLogo} class="logo" alt={content.viteLogo.value} /> </a> <a href="https://preactjs.com" target="_blank"> <img src={preactLogo} class="logo preact" alt={content.preactLogo.value} /> </a> </div> <h1>{content.title}</h1> <div class="card"> <button onClick={() => setCount((count) => count + 1)}> {content.count} {count} </button> <p>{content.edit}</p> </div> <p class="read-the-docs">{content.readTheDocs}</p> </> );};const App: FunctionalComponent = () => ( <IntlayerProvider> <AppContent /> </IntlayerProvider>);export default App;
alt、title、href、aria-labelなどのstring属性でコンテンツを使用する場合、関数の値を呼び出す必要があります:
jsx<img src={content.image.src.value} alt={content.image.value} />
注: Preactでは、classNameは通常classとして記述されます。
useIntlayerフックの詳細については、ドキュメントを参照してください(preact-intlayerのAPIも同様です)。
(オプション)ステップ6: コンテンツの言語を変更する
コンテンツの言語を変更するには、useLocaleフックが提供するsetLocale関数を使用します。この関数を使用して、アプリケーションのロケールを設定し、コンテンツを更新できます。
import type { FunctionalComponent } from "preact";import { Locales } from "intlayer";import { useLocale } from "preact-intlayer";const LocaleSwitcher: FunctionalComponent = () => { const { setLocale } = useLocale(); return ( <button onClick={() => setLocale(Locales.ENGLISH)}> 言語を英語に変更import { Locales } from "intlayer";import { useLocale } from "preact-intlayer";const LocaleSwitcher = () => { const { setLocale } = useLocale(); return ( <button onClick={() => setLocale(Locales.ENGLISH)}> 言語を英語に変更 </button> );};export default LocaleSwitcher;
useLocale フックの詳細については、ドキュメント を参照してください(preact-intlayer の API は同様です)。
(オプション)ステップ7: アプリケーションにローカライズされたルーティングを追加する
このステップの目的は、各言語に固有のルートを作成することです。これはSEOやSEOフレンドリーなURLに役立ちます。 例:
- https://example.com/about- https://example.com/es/about- https://example.com/fr/about
デフォルトでは、デフォルトロケールのルートにはプレフィックスが付きません。デフォルトロケールにプレフィックスを付けたい場合は、設定で middleware.prefixDefault オプションを true に設定できます。詳細は設定ドキュメントを参照してください。
アプリケーションにローカライズされたルーティングを追加するには、アプリケーションのルートをラップし、ロケールベースのルーティングを処理する LocaleRouter コンポーネントを作成できます。以下は preact-iso を使用した例です。
まず、preact-iso をインストールします:
npm install preact-iso
import { type Locales, configuration, getPathWithoutLocale } from "intlayer";import { ComponentChildren, FunctionalComponent } from "preact";import { IntlayerProvider } from "preact-intlayer";import { LocationProvider, useLocation } from "preact-iso";import { useEffect } from "preact/hooks";const { internationalization, middleware } = configuration;const { locales, defaultLocale } = internationalization;const Navigate: FunctionalComponent<{ to: string; replace?: boolean }> = ({ to, replace,}) => { const { route } = useLocation(); useEffect(() => { route(to, replace); }, [to, replace, route]); return null;};/** * ローカリゼーションを処理し、適切なロケールコンテキストで子要素をラップするコンポーネント。 * URLベースのロケール検出と検証を管理します。 */const AppLocalized: FunctionalComponent<{ children: ComponentChildren; locale?: Locales;}> = ({ children, locale }) => { const { path: pathname, url } = useLocation(); if (!url) { return null; } const search = url.substring(pathname.length); // 現在のロケールを決定し、指定されていない場合はデフォルトにフォールバック const currentLocale = locale ?? defaultLocale; // ロケールプレフィックスを削除してベースパスを構築 const pathWithoutLocale = getPathWithoutLocale( pathname // 現在のURLパス ); /** * middleware.prefixDefault が true の場合、デフォルトロケールには常にプレフィックスが付く必要があります。 */ if (middleware.prefixDefault) { // ロケールを検証 if (!locale || !locales.includes(locale)) { // 更新されたパスでデフォルトロケールにリダイレクト return ( <Navigate to={`/${defaultLocale}/${pathWithoutLocale}${search}`} replace // 現在の履歴エントリを新しいものに置き換え /> ); } // 子要素を IntlayerProvider でラップし、現在のロケールを設定 return ( <IntlayerProvider locale={currentLocale}>{children}</IntlayerProvider> ); } else { /** * middleware.prefixDefault が false の場合、デフォルトロケールにはプレフィックスが付きません。 * 現在のロケールが有効であり、デフォルトロケールではないことを確認します。 */ if ( currentLocale.toString() !== defaultLocale.toString() && !locales .filter( (loc) => loc.toString() !== defaultLocale.toString() // デフォルトロケールを除外 ) .includes(currentLocale) // 現在のロケールが有効なロケールリストに含まれているか確認 ) { // ロケールプレフィックスなしのパスにリダイレクト return <Navigate to={`${pathWithoutLocale}${search}`} replace />; } // 子要素を IntlayerProvider でラップし、現在のロケールを設定 return ( <IntlayerProvider locale={currentLocale}>{children}</IntlayerProvider> ); }};const RouterContent: FunctionalComponent<{ children: ComponentChildren;}> = ({ children }) => { const { path } = useLocation(); if (!path) { return null; } const pathLocale = path.split("/")[1] as Locales; const isLocaleRoute = locales .filter((locale) => middleware.prefixDefault || locale !== defaultLocale) .some((locale) => locale.toString() === pathLocale); if (isLocaleRoute) { return <AppLocalized locale={pathLocale}>{children}</AppLocalized>; } return ( <AppLocalized locale={!middleware.prefixDefault ? defaultLocale : undefined} > {children} </AppLocalized> );};/** * ロケール固有のルートを設定するルーターコンポーネント。 * preact-iso を使用してナビゲーションを管理し、ローカライズされたコンポーネントをレンダリングします。 */export const LocaleRouter: FunctionalComponent<{ children: ComponentChildren;}> = ({ children }) => ( <LocationProvider> <RouterContent>{children}</RouterContent> </LocationProvider>);
次に、アプリケーションでLocaleRouterコンポーネントを使用します。
import { LocaleRouter } from "./components/LocaleRouter";import type { FunctionalComponent } from "preact";// ... AppContentコンポーネント (ステップ5で定義)const App: FunctionalComponent = () => ( <LocaleRouter> <AppContent /> </LocaleRouter>);export default App;
並行して、intLayerMiddlewarePluginを使用して、アプリケーションにサーバーサイドルーティングを追加することもできます。このプラグインは、URLに基づいて現在のロケールを自動的に検出し、適切なロケールクッキーを設定します。ロケールが指定されていない場合、プラグインはユーザーのブラウザ言語設定に基づいて最適なロケールを決定します。ロケールが検出されない場合、デフォルトロケールにリダイレクトします。
import { defineConfig } from "vite";import preact from "@preact/preset-vite";import { intlayerPlugin, intLayerMiddlewarePlugin } from "vite-intlayer";// https://vitejs.dev/config/export default defineConfig({ plugins: [preact(), intlayerPlugin(), intLayerMiddlewarePlugin()],});
(オプション) ステップ8: ロケール変更時にURLを変更
import { useLocation, route } from "preact-iso";import { Locales, getHTMLTextDir, getLocaleName, getLocalizedUrl,} from "intlayer";import { useLocale } from "preact-intlayer";import type { FunctionalComponent } from "preact";const LocaleSwitcher: FunctionalComponent = () => { const location = useLocation(); const { locale, availableLocales, setLocale } = useLocale({ onLocaleChange: (newLocale) => { const currentFullPath = location.url; // preact-isoは完全なURLを提供します // 更新されたロケールでURLを構築します // 例: /es/about?foo=bar const pathWithLocale = getLocalizedUrl(currentFullPath, newLocale); // URLパスを更新します route(pathWithLocale, true); // trueはreplaceを意味します }, }); return ( <div> <button popovertarget="localePopover">{getLocaleName(locale)}</button> <div id="localePopover" popover="auto"> {availableLocales.map((localeItem) => ( <a href={getLocalizedUrl(location.url, localeItem)} hreflang={localeItem} aria-current={locale === localeItem ? "page" : undefined} onClick={(e) => { e.preventDefault(); setLocale(localeItem); // ロケール設定後のプログラム的なナビゲーションはonLocaleChangeで処理されます }} key={localeItem} > <span> {/* ロケール - 例: FR */} {localeItem} </span> <span> {/* 自身のロケールでの言語 - 例: Français */} {getLocaleName(localeItem, localeItem)} </span> <span dir={getHTMLTextDir(localeItem)} lang={localeItem}> {/* 現在のロケールでの言語 - 例: Francés (現在のロケールがLocales.SPANISHの場合) */} {getLocaleName(localeItem, locale)} </span> <span dir="ltr" lang={Locales.ENGLISH}> {/* 英語での言語 - 例: French */} {getLocaleName(localeItem, Locales.ENGLISH)} </span> </a> ))} </div> </div> );};export default LocaleSwitcher;
ドキュメント参照:
- useLocaleフック (preact-intlayerのAPIも類似)
以下は、説明を追加し、コード例を改良したステップ9です:
(オプション) ステップ9: HTMLの言語と方向属性を切り替える
アプリケーションが複数の言語をサポートする場合、現在のロケールに合わせて<html>タグのlangおよびdir属性を更新することが重要です。これにより、以下が保証されます:
- アクセシビリティ: スクリーンリーダーや支援技術は、正しいlang属性を使用してコンテンツを正確に発音および解釈します。
- テキストレンダリング: dir(方向)属性は、テキストが適切な順序でレンダリングされることを保証します(例: 英語は左から右、アラビア語やヘブライ語は右から左)。
- SEO: 検索エンジンはlang属性を使用してページの言語を判断し、検索結果で適切なローカライズされたコンテンツを提供します。
ロケール変更時にこれらの属性を動的に更新することで、すべてのサポート言語で一貫性があり、アクセシブルな体験をユーザーに提供できます。
フックの実装
カスタムフックを作成してHTML属性を管理します。このフックはロケールの変更を監視し、属性を適切に更新します:
import { useEffect } from "preact/hooks"; import { useLocale } from "preact-intlayer"; import { getHTMLTextDir } from "intlayer";
/**
- 現在のロケールに基づいてHTML 要素の lang と dir 属性を更新します。
- lang: ブラウザや検索エンジンにページの言語を通知します。
- dir: 正しい読み順を保証します(例: 英語の場合は 'ltr'、アラビア語の場合は 'rtl')。 -
- この動的な更新は、適切なテキストレンダリング、アクセシビリティ、SEOにとって重要です。 */ export const useI18nHTMLAttributes = () => { const { locale } = useLocale();
useEffect(() => { // 現在のロケールに言語属性を更新します。 document.documentElement.lang = locale;
// 現在のロケールに基づいてテキスト方向を設定します。 document.documentElement.dir = getHTMLTextDir(locale);}, [locale]); };
```jsx fileName="src/hooks/useI18nHTMLAttributes.jsx" codeFormat="esm" import { useEffect } from "preact/hooks"; import { useLocale } from "preact-intlayer"; import { getHTMLTextDir } from "intlayer"; /** * 現在のロケールに基づいてHTML <html> 要素の `lang` と `dir` 属性を更新します。 */ export const useI18nHTMLAttributes = () => { const { locale } = useLocale(); useEffect(() => { document.documentElement.lang = locale; document.documentElement.dir = getHTMLTextDir(locale); }, [locale]); };アプリケーションでフックを使用する
ロケールが変更されるたびにHTML属性が更新されるように、フックをメインコンポーネントに統合します:
import type { FunctionalComponent } from "preact";import { IntlayerProvider } from "preact-intlayer"; // AppContentが必要な場合、useIntlayerはすでにインポートされていますimport { useI18nHTMLAttributes } from "./hooks/useI18nHTMLAttributes";import "./app.css";// ステップ5のAppContent定義const AppWithHooks: FunctionalComponent = () => { // ロケールに基づいて<html>タグのlangとdir属性を更新するフックを適用します。 useI18nHTMLAttributes(); // AppContentはステップ5からのメインコンテンツ表示コンポーネントと仮定します return <AppContent />;};const App: FunctionalComponent = () => ( <IntlayerProvider> <AppWithHooks /> </IntlayerProvider>);export default App;
これらの変更を適用することで、アプリケーションは以下を実現します:
- 言語 (lang) 属性が現在のロケールを正しく反映し、SEOやブラウザの動作に重要な役割を果たします。
- テキスト方向 (dir) がロケールに応じて調整され、異なる読み順の言語に対して可読性と使いやすさを向上させます。
- アクセシビリティ を向上させ、支援技術がこれらの属性に依存して最適に機能するようにします。
(オプション) ステップ10: ローカライズされたリンクコンポーネントの作成
アプリケーションのナビゲーションが現在のロケールを尊重するようにするために、カスタム Link コンポーネントを作成できます。このコンポーネントは内部URLに現在の言語を自動的にプレフィックスします。
この動作は以下の理由で有用です:
- SEOとユーザーエクスペリエンス: ローカライズされたURLは、検索エンジンが言語固有のページを正しくインデックス化し、ユーザーに好みの言語でコンテンツを提供するのに役立ちます。
- 一貫性: アプリケーション全体でローカライズされたリンクを使用することで、ナビゲーションが現在のロケール内に留まり、予期しない言語切り替えを防ぎます。
- 保守性: ローカリゼーションロジックを単一のコンポーネントに集中させることで、URLの管理が簡素化されます。
Preactでpreact-isoを使用する場合、標準の<a>タグが通常ナビゲーションに使用され、preact-isoがルーティングを処理します。クリック時にプログラム的なナビゲーションが必要な場合(例: ナビゲーション前にアクションを実行する場合)、useLocationからのroute関数を使用できます。以下は、URLをローカライズするカスタムアンカーコンポーネントの作成方法です:
import { getLocalizedUrl } from "intlayer";import { useLocale, useLocation, route } from "preact-intlayer"; // useLocationとrouteはpreact-intlayer経由で再エクスポートされるか、直接インポート// 再エクスポートされない場合: import { useLocation, route } from "preact-iso";import type { JSX } from "preact"; // HTMLAttributes用import { forwardRef } from "preact/compat"; // refを転送するためexport interface LocalizedLinkProps extends JSX.HTMLAttributes<HTMLAnchorElement> { href: string; replace?: boolean; // オプション: 履歴状態を置き換える}/** * 指定されたURLが外部リンクかどうかを確認するユーティリティ関数。 * URLがhttp://またはhttps://で始まる場合、外部リンクと見なされます。 */export const checkIsExternalLink = (href?: string): boolean => /^https?:\/\//.test(href ?? "");/** * 現在のロケールに基づいてhref属性を適応させるカスタムリンクコンポーネント。 * 内部リンクの場合、`getLocalizedUrl`を使用してURLにロケールをプレフィックスします(例: /ja/about)。 * これにより、ナビゲーションが同じロケールコンテキスト内に留まることを保証します。 * 標準の<a>タグを使用しますが、preact-isoの`route`を使用してクライアントサイドナビゲーションをトリガーできます。 */export const LocalizedLink = forwardRef<HTMLAnchorElement, LocalizedLinkProps>( ({ href, children, onClick, replace = false, ...props }, ref) => { const { locale } = useLocale(); const location = useLocation(); // preact-isoから const isExternalLink = checkIsExternalLink(href); const hrefI18n = href && !isExternalLink ? getLocalizedUrl(href, locale) : href; const handleClick = (event: JSX.TargetedMouseEvent<HTMLAnchorElement>) => { if (onClick) { onClick(event); } if ( !isExternalLink && href && // hrefが定義されていることを確認 event.button === 0 && // 左クリック !event.metaKey && !event.ctrlKey && !event.shiftKey && !event.altKey && // 標準の修飾キーのチェック !props.target // 新しいタブ/ウィンドウをターゲットにしていない ) { event.preventDefault(); if (location.url !== hrefI18n) { // URLが異なる場合のみナビゲート route(hrefI18n, replace); // preact-isoのrouteを使用 } } }; return ( <a href={hrefI18n} ref={ref} onClick={handleClick} {...props}> {children} </a> ); });
動作の仕組み
- 外部リンクの検出:
ヘルパー関数 checkIsExternalLink は、URLが外部リンクかどうかを判定します。外部リンクは変更されません。 - 現在のロケールの取得:
useLocale フックは現在のロケールを提供します。 - URLのローカライズ:
内部リンクの場合、getLocalizedUrl がURLに現在のロケールを付加します。 - クライアントサイドナビゲーション:
handleClick 関数は、内部リンクで標準のナビゲーションを防ぐべきかどうかを確認します。必要であれば、preact-iso の route 関数(useLocation または直接インポートで取得)を使用してクライアントサイドナビゲーションを実行します。これにより、ページ全体のリロードなしでSPAのような動作が可能になります。 - リンクの返却:
コンポーネントはローカライズされたURLとカスタムクリックハンドラを持つ <a> 要素を返します。
TypeScriptの設定
Intlayerはモジュール拡張を使用して、TypeScriptの利点を活用し、コードベースを強化します。
自動生成された型を含めるようにTypeScriptの設定を確認してください。
{ // ... 既存のTypeScript設定 "compilerOptions": { // ... "jsx": "react-jsx", "jsxImportSource": "preact", // Preact 10+推奨 // ... }, "include": [ // ... 既存のTypeScript設定 ".intlayer/**/*.ts", // 自動生成された型を含める ],}
特にjsxとjsxImportSource、または古いPreactバージョンを使用している場合はjsxFactory/jsxFragmentFactoryがpreset-viteのデフォルトで設定されていることを確認してください。
Gitの設定
Intlayerによって生成されたファイルを無視することをお勧めします。これにより、それらをGitリポジトリにコミットすることを避けられます。
これを行うには、以下の指示を .gitignore ファイルに追加します:
# Intlayerによって生成されたファイルを無視.intlayer
VS Code拡張機能
Intlayerでの開発体験を向上させるために、公式の Intlayer VS Code拡張機能 をインストールできます。
この拡張機能は以下を提供します:
- 翻訳キーのオートコンプリート。
- 翻訳漏れのリアルタイムエラー検出。
- 翻訳コンテンツのインラインプレビュー。
- 翻訳を簡単に作成および更新するためのクイックアクション。
拡張機能の使用方法の詳細については、Intlayer VS Code Extensionドキュメントを参照してください。
さらに進む
さらに進めるには、ビジュアルエディターを実装するか、CMSを使用してコンテンツを外部化することができます。
このドキュメントを改善するアイデアがある場合は、GitHubでプルリクエストを送信することで自由に貢献してください。
ドキュメントへのGitHubリンク