
i18nextは多言語対応のためのJavaScriptライブラリで、React、React Native、Expoなどのプロジェクトで翻訳を実装する際に使います。
i18nextには多様な機能があり、効率的に多言語翻訳を実装できます。あなたのソフトウェアはi18nextを使っていますか。ぜひ世界に知らせましょう。 オープンソースプロジェクトの翻訳管理方法を探していませんか。 Locizeはオープンソースの理念を支持し、サポートを提供しています。i18next-introduction↗
i18nextをインストールします。 # npm
npm install i18next --save
# yarn
yarn add i18nexti18nextを初期化し、翻訳データを設定します。
i18nextを初期化するには、i18nextオブジェクトを作り、翻訳データを渡します。import i18next from 'i18next';
await i18next.init({
lng: 'en',
debug: true,
resources: {
en: {
translation: {
"key": "hello world"
}
}
}
});
console.log(i18next.t('key'));i18nextの初期化オプションは次のとおりです。
ほかの設定はi18nextの初期化オプション↗を参照してください。| オプション | 説明 | 型 | 既定値 |
|---|---|---|---|
| lng | 現在の言語 | string | undefined |
| debug | デバッグモードとログの有効化 | boolean | false |
| resources | 翻訳データ | object | undefined |
| fallbackLng | 言語が指定されていない場合の言語 https://www.i18next.com/principles/fallback#language-fallback↗ | string | 'en' |
i18nextで翻訳データを管理し、言語パックを作ります。
翻訳データはresourcesに保存し、lngに現在の言語を設定します。{
"key": "value of key"
}{
"key": "키 값"
}import i18next from 'i18next';
import translationEn from "./en-US.json";
import translationKo from "./ko-KR.json";
await i18next.init({
lng: 'en', // 언어 설정은 여기서 변경
debug: true,
resources: {
en: {
translation: translationEn
},
ko: {
translation: translationKo
}
}
});
console.log(i18next.t('key')); // "value of key"
console.log(i18next.t('key', { lng: 'ko' })); // "키 값"i18nextから翻訳データを取得して使います。
i18next.tで翻訳データを取得できます。{
"key": "value of key",
"look": {
"deep": "value of look deep"
},
"error": {
"unspecific": "Something went wrong.",
"404": "The page was not found."
}
}i18next.t('key'); // "value of key"
i18next.t('look.deep'); // "value of look deep"i18next.t('notkey', 'default value to show'); // "default value to show"// const error = '404';
i18next.t([`error.${error}`, 'error.unspecific']); // -> "The page was not found"
// const error = '502';
i18next.t([`error.${error}`, 'error.unspecific']); // -> "Something went wrong"i18next.tへ渡したオブジェクトのプロパティで置き換えられます。{
"key": "{{what}} is {{how}}"
}i18next.t('key', { what: 'i18next', how: 'great' });
// -> "i18next is great"{
"key": "I am {{author.name}}"
}const author = {
name: 'Jan',
github: 'jamuhl'
};
i18next.t('key', { author });
// -> "I am Jan"i18nextは設定に応じた書式整形をサポートします。
数値、日付、通貨などを整形できます。
少し複雑なので、i18nextの書式設定↗を参照しながら進めてください。
基本の形は次のとおりです。{{value, formatname(options1: options1Value)}}:valueをformatnameで整形します。t('key', { option1: option1Value }):ルートオプションで書式を設定します。t('key', { formatParams: { value: { option1: option1Value } }):valueごとに異なる書式を設定します。numberは数値を整形します。
formatnameはnumberで、minimumFractionDigitsは小数点以下の桁数を指定します。{
"intlNumber": "Some {{val, number}}",
"intlNumberWithOptions": "Some {{val, number(minimumFractionDigits: 2)}}"
}i18next.t('intlNumber', { val: 1000 });
// --> Some 1,000
i18next.t('intlNumber', { val: 1000.1, minimumFractionDigits: 3 });
// --> Some 1,000.100
i18next.t('intlNumber', { val: 1000.1, formatParams: { val: { minimumFractionDigits: 3 } } });
// --> Some 1,000.100
i18next.t('intlNumberWithOptions', { val: 2000 });
// --> Some 2,000.00
i18next.t('intlNumberWithOptions', { val: 2000, minimumFractionDigits: 3 });
// --> Some 2,000.000
// 다국어 설정
i18next.t('intlNumber', { val: 1000.1, lng: 'de' });
// or: i18next.t('intlNumber', { val: 1000.1, locale: 'de' });
i18next.t('intlNumber', { val: 1000.1, formatParams: { val: { lng: 'de' } } });
// or: i18next.t('intlNumber', { val: 1000.1, formatParams: { val: { locale: 'de' } } });currencyは通貨を整形します。
formatnameはcurrencyで、currencyオプションに通貨コードを指定します。{
"intlCurrencyWithOptionsSimplified": "The value is {{val, currency(USD)}}",
"intlCurrencyWithOptions": "The value is {{val, currency(currency: USD)}}",
"twoIntlCurrencyWithUniqueFormatOptions": "The value is {{localValue, currency}} or {{altValue, currency}}",
}i18next.t('intlCurrencyWithOptionsSimplified', { val: 2000 });
// --> The value is $2,000.00
i18next.t('intlCurrencyWithOptions', { val: 2300 });
// --> The value is $2,300.00
i18next.t('twoIntlCurrencyWithUniqueFormatOptions',
{
localValue: 12345.67,
altValue: 16543.21,
formatParams: {
localValue: { currency: 'USD', locale: 'en-US' },
altValue: { currency: 'CAD', locale: 'fr-CA' },
},
},);
// --> The value is $12,345.67 or 16 543,21 $ CAdatetimeは日付と時刻を整形します。
formatnameはdatetimeで、formatオプションに日付と時刻の形式を指定します。{
"intlDateTime": "On the {{val, datetime}}",
}
i18next.t('intlDateTime', { val: new Date(Date.UTC(2012, 11, 20, 3, 0, 0)) });
// --> On the 12/20/2012
i18next.t('intlDateTime',
{
val: new Date(Date.UTC(2012, 11, 20, 3, 0, 0)),
formatParams: {
val: { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' },
},
});
// --> On the Thursday, December 20, 2012listは配列を整形します。
formatnameはlistで、typeオプションにリストの形式を指定します。{
"intlList": "A list of {{val, list}}"
}// JSON
i18next.t('intlList', { val: ['locize', 'i18next', 'awesomeness'] });
// --> A list of locize, i18next, and awesomenessi18nextは単数形と複数形をサポートします。
キーに_oneまたは_otherを追加して区別できます。{
"key_one": "item",
"key_other": "items",
"keyWithCount_one": "{{count}} item",
"keyWithCount_other": "{{count}} items"
}i18next.t('key', {count: 0}); // -> "items"
i18next.t('key', {count: 1}); // -> "item"
i18next.t('key', {count: 5}); // -> "items"
i18next.t('key', {count: 100}); // -> "items"
i18next.t('keyWithCount', {count: 0}); // -> "0 items"
i18next.t('keyWithCount', {count: 1}); // -> "1 item"
i18next.t('keyWithCount', {count: 5}); // -> "5 items"
i18next.t('keyWithCount', {count: 100}); // -> "100 items"_zero:0_one:1_two:2_few:3〜10_many:11〜99_other:100以上{
"key_zero": "zero",
"key_one": "singular",
"key_two": "two",
"key_few": "few",
"key_many": "many",
"key_other": "other"
}i18next.t('key', {count: 0}); // -> "zero"
i18next.t('key', {count: 1}); // -> "singular"
i18next.t('key', {count: 2}); // -> "two"
i18next.t('key', {count: 3}); // -> "few"
i18next.t('key', {count: 4}); // -> "few"
i18next.t('key', {count: 5}); // -> "few"
i18next.t('key', {count: 11}); // -> "many"
i18next.t('key', {count: 99}); // -> "many"
i18next.t('key', {count: 100}); // -> "other"i18nextを使う方法を紹介しました。
i18nextは多様な機能を提供し、多言語翻訳を効率的に実装できます。
翻訳データの管理と言語パックの作成、翻訳の取得と利用方法を確認したので、i18nextを使って多言語対応を実装できます。
フロントエンドのプラットフォームごとにi18nextの使い方は多少異なるため、それぞれのドキュメントを参照してください。
関連ライブラリも豊富なので、探してみると役立ちます。i18next - supported-frameworks↗
i18nのJSON自動翻訳サイトを使うと、翻訳データも簡単に生成できます。translate.i18next↗
Google翻訳をベースにしているそうです。