2023年10月1日日曜日

react native 国際化対応 その1

react nativeアプリを複数言語に対応するために


==参考==

react-i18next Quick start

 https://react.i18next.com/guides/quick-start

=====


1.インストール

npm install react-i18next i18next --save


2.Quick startのConfigure i18next の i18n.js をコピペしてファイルを作る


3.index.js でimport

import {AppRegistry} from 'react-native';
import App from './App';
import {name as appName} from './app.json';
import './i18n'; //追加


4.実行するとエラーになる

Your environment seems not to be Intl API compatible, use an Intl.PluralRules polyfill.

Will fallback to the compatibilityJSON v3 format handling.


5.i18n.js の 初期化処理に”compatibilityJSON: 'v3',”を追加

i18n.use(initReactI18next).init({
  resources,
  lng: 'en',

  interpolation: {
    escapeValue: false,
  },
  compatibilityJSON: 'v3', //追加
});


6.使う(App.js)

import React from 'react';
import {Text, View} from 'react-native';
import {useTranslation} from 'react-i18next';

function App() {
  const {t, i18n} = useTranslation();

  return (
    <View>
      <Text>{t('Welcome to React')}</Text>
    </View>
  );
}

export default App;


7.実行







0 件のコメント:

コメントを投稿