Global Variable / Constant in React Native

后端 未结 4 1298
北荒
北荒 2021-02-01 02:03

is there a way in React Native that I can define on a global variable all the strings that I will be using like in Android Development there is a String.xml where you can put al

4条回答
  •  醉酒成梦
    2021-02-01 02:48

    If you want to switch between languages depening on platform localisation.

    fetch node_module via npm

    npm i react-native-localization --save 
    

    Define variables in class:

    // Localisation.js
    let LocalizedStrings = require ('react-native-localization'); 
    let strings = new LocalizedStrings ({ 
     en: { 
         loginTitle:  "Login",
     }, 
     de: {
         loginTitle:  "Anmelden",
     }
    })
    

    When you need the strings:

    var STRINGS = require ('./Localization');
    {STRINGS.loginTitle}
    

提交回复
热议问题