1.3.0 • Published 3 years ago

react-native-language-loader v1.3.0

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

react-native-language-loader

Features

Note: Do not use any version below 1.2.3

From 1.2.3: Works on Android & ios

Recommended to use JSON files

Nothing fancy, just a lightweight module that enables you to quickly load a specific language file or multiple languages (which I needed for localization) from your android/app/src/main/assets/languages folder (android) or from your Bundle.main on ios (If you've placed your languages files in that folder).

(Example)

Installation

$ npm install react-native-language-loader --save

Android

  1. Create a languages folder in android/app/src/main/assets/
  2. Copy your language files to this folder

(Example)

IOS

  1. Open Xcode (from ios/<yourAppName>.xcodeproj)
  2. Drag & Drop or copy your language files to your xcode project (make sure to select your project as the target when copying)

(3. You might have to run pod install in your /ios/ folder)

(Example)

Usage

To load a specific language (Using callback)

First parameter is the name of the file (without extension)

Second parameter is the extension

import LanguageLoader from 'react-native-language-loader';

/* Load `en.json` */
LanguageLoader.loadLanguage('en','json',(error,language)=>{

    if(error)
    {
        // handle error
        // file not found exception, etc..
        return;
    }
    // If it is a JSON file, you might want to parse it
    var parsed = JSON.parse(language);
    console.log(parsed); // use `language` or `parsed` in your app
    
});

/* Load `myLanguage.txt` */
LanguageLoader.loadLanguage('myLanguage','txt',(error,language)=>{

    if(error)
    {
        // handle error
        // file not found exception, etc..
        return;
    }
    console.log(language); // use `language` in your app
    
});

Load a single language (Using Async/Await)

First parameter is the name of the file (without extension)

Second parameter is the extension

import LanguageLoader from 'react-native-language-loader';

var getLanguage = async () => {

    await LanguageLoader.loadLanguageAsync('en','json')
    .then(language=>JSON.parse(language)) // You can skip this if it's not a json file
    .then(language=>{
        // use `language` in your app
        console.log(language)
    })
    .catch(error=>{
        // handle error
        // file not found exception, etc..
        console.log('ERROR',error);
    });
    
};

Load every language (Using callback)

First parameter is the extension of the files that you'd like to load

import LanguageLoader from 'react-native-language-loader';

/* Load every file from assets/languages */
LanguageLoader.loadLanguages('json',(error,languages)=>{

    if(error)
    {
        // handle error
        // file not found exception, etc..
        return;
    }
    
    // languages is an array, even if no files could be found ( Empty array )
    
    console.log(languages) // use `languages` in your app
    // or parse it
    var parsed = JSON.parse(languages);
    // `parsed` is a JSON Array, containing every file from from android/app/src/main/assets/languages
    console.log(parsed); // use `parsed` in your app
    
});

Load every language (Using Async/Await)

First parameter is the extension of the files that you'd like to load

import LanguageLoader from 'react-native-language-loader';

var getLanguages = async() => {

    await LanguageLoader.loadLanguagesAsync('json')
    .then(languages=>JSON.parse(languages)) // NOTE: If you have at least 1 file which is not JSON, skip this
    .then(languages=>{
        // languages is an array, even if no files could be found ( Empty array )
        // use `languages` in your app
        // `languages` is a JSON Array, containing every file from from android/app/src/main/assets/languages
        console.log(languages)
    })
    .catch(error=>{
        // handle error
        // file not found exception, etc..
        console.log('ERROR',error);
    });
    
};

Exceptions

IOS:

`could_not_read_content`

`file_not_found`

`could_not_load_files`

Android:

IOException (Java)

Mostly automatic installation

$ react-native link react-native-language-loader

1.3.0

3 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.1.9

4 years ago

1.2.0

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago