react-native-i18n-storage
Sync your I18n Manager from Native Side Without hassle!
Why?
React Native I18n Storage is a simple library to manage your app's I18nManager settings and language by keeping them in sync with local storage from the initial native app launch!
Which means that you can change the locale from native side and the app will restart with the new locale from the local storage!
Conventions
- Locale and Layout are initialized from the first native app launch, and then they are kept in sync with the local storage.
- saves huge performance cost by not using hooks or context
- keeps your code clean and simple
- use Locale and RTL IN/OUT of React Components and they will be kept in sync with the local storage
- No need to change your code logic
- No need to change your code structure or architecture
- No need to change your code style
- No need to change your code design pattern
- No need to change your code design pattern
- No need to change your code anything 😅
Prerequisites
if you have not installed them yet, you can install them by running the following commands:
// you can use npm instead of yarn
yarn add @react-native-async-storage/async-storage react-native-restart
Installation
// you can use npm instead of yarn
yarn add @ahmedrowaihi/react-native-i18n-storage
Setup
iOS
#import "AppDelegate.h"
#import <I18nStorage.h>
2. modify AppDelegate.mm file inside didFinishLaunchingWithOptions
method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
I18nStorage *i18nStorage = [[I18nStorage alloc] init];
[i18nStorage SyncI18nStorage];
}
Android
1. import package to your MainApplication.java
import com.facebook.react.ReactApplication;
import com.i18nstorage.I18nStorageUtility;
2. Add the following code to your MainApplication.java file inside onCreate
method:
@Override
public void onCreate() {
super.onCreate();
I18nStorageUtility.SyncI18nStorage(this);
}
Usage
change the locale and Restart the app
import { setI18nStorage } from '@ahmedrowaihi/react-native-i18n-storage';
import { Platform, Settings } from 'react-native';
await setI18nStorage({
locale: 'ar',
forceRTL: true,
allowRTL: true,
doLeftAndRightSwapInRTL: true,
});
or with optional parameters
await setI18nStorage(
{
locale: 'ar',
forceRTL: true,
allowRTL: true,
doLeftAndRightSwapInRTL: true,
},
false,
() => {}
);
get the locale and RTL from the local storage
import { getI18nStorage } from '@ahmedrowaihi/react-native-i18n-storage';
const I18nStorage = await getI18nStorage();
reset the locale and RTL to the default values
import { resetI18nStorage } from '@ahmedrowaihi/react-native-i18n-storage';
await resetI18nStorage();
or with optional parameters
await resetI18nStorage(
false,
() => {}
);
Roadmap
Contributing
See the contributing guide to learn how to contribute to the repository and the development workflow.
License
MIT
development workflow.
License
MIT