Skip to content

Instantly share code, notes, and snippets.

@ahmedrowaihi
Last active April 27, 2023 02:48
Show Gist options
  • Save ahmedrowaihi/b1d6b9f87a5347087f9a11dda2017df3 to your computer and use it in GitHub Desktop.
Save ahmedrowaihi/b1d6b9f87a5347087f9a11dda2017df3 to your computer and use it in GitHub Desktop.
Native Side Set React Native I18nManager before the JS Bundle Kicks Off
// Modify "didFinishLaunchingWithOptions" method in IOS AppDelegate.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Ahmed Rowihi: Native Side I18n hack :D
// set React Native I18nManager before the JS Bundle Kicks Off!
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"@user_language"];
BOOL isRTL = [value isEqualToString:@"ar"] || [[[NSLocale preferredLanguages] objectAtIndex:0] hasPrefix:@"ar_"];
if (isRTL) {
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"ar"];
[[NSUserDefaults standardUserDefaults] setObject:@[locale.localeIdentifier] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:locale.localeIdentifier forKey:@"AppleLocale"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"AppleLocale"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[[RCTI18nUtil sharedInstance] allowRTL:isRTL];
[[RCTI18nUtil sharedInstance] forceRTL:isRTL];
// ... your rest of default code before RCTAppSetupPrepareApp(application);
// Modify onCreate() in Android MainApplication.java
@Override
public void onCreate() {
super.onCreate();
// @ahmedrowaihi: Native Side I18n hack :D
// set React Native I18nManager before the JS Bundle Kicks Off!
String value = null;
try {
value = AsyncLocalStorageUtil.getItemImpl(
ReactDatabaseSupplier.getInstance(this).get(),
"@user_language");
if (value == null) value = Locale.getDefault().getLanguage();
boolean isRTL = "ar".equals(value);
Locale locale = new Locale(value);
Configuration config = getBaseContext().getResources().getConfiguration();
config.setLocale(locale);
I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
sharedI18nUtilInstance.allowRTL(this, isRTL);
sharedI18nUtilInstance.forceRTL(this, isRTL);
} catch (Exception ignored) {}
// ... your rest of default code before react host
// new ReactNativeHostWrapper(this, new MainApplicationReactNativeHost(this));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment