[React Native] react-native-locallize와 i18n-js를 이용한 다언어화

document
  • react-native-localize
  • i18n-js
  • 프로젝트에서 매크로 패키지 가져오기
    yarn add react-native-localize
    yarn add i18n-js
    
    추가 설정
    react-native 버전이 0.60보다 크면 수동 링크 설정이 필요하지 않기 때문에 이것으로 끝냅니다.
    수동 링크 설정이 필요한 경우 다음을 참조하십시오.
  • react-native-localize linking instructions
  • 사용 방법 샘플
    다음 세 개의 파일을 만듭니다.
  • en.js
    English용 정의 파일
  • ja.js
    Japanese용 정의 파일
  • i18n.js
    상기 정의 파일을 호출하여 번역하는 파일
  • en.js
    export default {
      welcome: "welcome"
    };
    
    ja.js
    export default {
      welcome: "ようこそ",
    };
    
    i18n.js
    import I18n from 'i18n-js';
    import * as Localize from 'react-native-localize';
    
    import en from './en';
    import ja from './ja';
    
    const locales = Localize.getLocales();
    console.log(locales);
    
    if (Array.isArray(locales)) {
      I18n.locale = locales[0].languageTag;
    }
    
    I18n.fallbacks = true;
    I18n.translations = {
      en,
      ja,
    };
    
    export default I18n;
    
    마지막으로 export의 I18n을 사용합니다.
    import React from "react";
    import I18n from "./I18n";
    
    const App = () => {
      return (
        <View>
          <Text>{I18n.t('welcome')}</Text>
        </View>
      );
    };
    
    export default App;
    
    
    결실

    좋은 웹페이지 즐겨찾기