├── .gitignore ├── LICENSE.md ├── README.md ├── index.d.ts ├── index.js ├── package.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | node_modules/ 3 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Antonio 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # i18next React Native Async Storage 2 | 3 | This plugin caches your user's language in React Native's Async storage 4 | 5 | ## Getting Started 6 | 7 | Install using: 8 | ```Bash 9 | yarn add i18next-react-native-async-storage 10 | ``` 11 | 12 | Then pass it to your i18n instance 13 | ```JavaScript 14 | import AsyncStoragePlugin from 'i18next-react-native-async-storage' 15 | 16 | i18n 17 | .use(AsyncStoragePlugin()) 18 | ``` 19 | 20 | ## Fallback mechanism 21 | 22 | You can pass a fallback function or language to the plugin in case it fails to find the user's language in the local storage (typically on the app's first run): 23 | 24 | ```JavaScript 25 | // With a fallback language 26 | i18n 27 | .use(AsyncStoragePlugin('en')) 28 | 29 | // With a fallback function 30 | const detectUserLanguage = (callback) => { 31 | return Expo 32 | .DangerZone 33 | .Localization 34 | .getCurrentLocaleAsync() 35 | .then(lng => { callback(lng.replace('_', '-')); }) 36 | } 37 | 38 | i18n 39 | .use(AsyncStoragePlugin(detectUserLanguage)) 40 | ``` 41 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import i18next from 'i18next'; 2 | 3 | declare function I18nextReactNativeAsyncStorage( 4 | fallback?: string | I18nextReactNativeAsyncStorage.FallbackFn, 5 | ): i18next.LanguageDetectorAsyncModule; 6 | 7 | declare namespace I18nextReactNativeAsyncStorage { 8 | type CallbackFn = (language: string) => void; 9 | type FallbackFn = (callback: CallbackFn) => any; 10 | } 11 | 12 | export = I18nextReactNativeAsyncStorage; 13 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const AsyncStorage = require('@react-native-async-storage/async-storage').default; 2 | 3 | function callFallbackIfFunc(fallback, callback){ 4 | if(typeof fallback === 'function'){ 5 | return fallback(callback) 6 | } 7 | 8 | return callback(fallback) 9 | } 10 | 11 | module.exports = exports = function(fallback){ 12 | return { 13 | type: 'languageDetector', 14 | async: true, 15 | init: () => {}, 16 | detect: async function(callback){ 17 | try { 18 | await AsyncStorage.getItem('@i18next-async-storage/user-language') 19 | .then(language => { 20 | if(language){ 21 | return callback(language) 22 | } 23 | 24 | return callFallbackIfFunc(fallback, callback) 25 | }) 26 | } catch(error){ 27 | callFallbackIfFunc(fallback, callback) 28 | } 29 | 30 | }, 31 | cacheUserLanguage: async function(language){ 32 | try { 33 | await AsyncStorage.setItem('@i18next-async-storage/user-language', language) 34 | } catch(error){ 35 | console.error(error) 36 | } 37 | } 38 | } 39 | }; 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "i18next-react-native-async-storage", 3 | "version": "1.0.4", 4 | "main": "index.js", 5 | "types": "index.d.ts", 6 | "files": [ 7 | "index.js", 8 | "index.d.ts" 9 | ], 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/0xClpz/i18next-react-native-async-storage.git" 13 | }, 14 | "author": "Antonio Calapez ", 15 | "license": "MIT", 16 | "keywords": [ 17 | "rn", 18 | "react-native", 19 | "i18n", 20 | "i18next", 21 | "async-storage", 22 | "Asyng storage" 23 | ], 24 | "devDependencies": { 25 | "i18next": "13.0.1" 26 | }, 27 | "peerDependencies": { 28 | "i18next": ">=13.0.1" 29 | }, 30 | "bugs": { 31 | "url": "https://github.com/0xClpz/i18next-react-native-async-storage/issues" 32 | }, 33 | "homepage": "https://github.com/0xClpz/i18next-react-native-async-storage#readme" 34 | } 35 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | i18next@13.0.1: 6 | version "13.0.1" 7 | resolved "https://registry.yarnpkg.com/i18next/-/i18next-13.0.1.tgz#aac758333e01a712710a81447bf033e6a0dbb71c" 8 | integrity sha512-V9hnqoP7N7aHncb1FYvHcAgKiDTmVpNuIr70QGFTyyNUVlQwz2O393fM0x7JIm0/ZYsoKjZdwtlGKYO4aYJ79Q== 9 | --------------------------------------------------------------------------------