diff --git a/Libraries/Utilities/I18nManager.js b/Libraries/Utilities/I18nManager.js index eae4e5935..764058d5f 100644 --- a/Libraries/Utilities/I18nManager.js +++ b/Libraries/Utilities/I18nManager.js @@ -14,11 +14,13 @@ type I18nManagerStatus = { isRTL: boolean, allowRTL: (allowRTL: boolean) => {}, + forceRTL: (forceRTL: boolean) => {}, }; const I18nManager : I18nManagerStatus = require('NativeModules').I18nManager || { isRTL: false, allowRTL: () => {}, + forceRTL: () => {}, }; module.exports = I18nManager; diff --git a/React/Modules/RCTI18nManager.m b/React/Modules/RCTI18nManager.m index 32371f229..de3666630 100644 --- a/React/Modules/RCTI18nManager.m +++ b/React/Modules/RCTI18nManager.m @@ -16,7 +16,12 @@ RCT_EXPORT_MODULE() RCT_EXPORT_METHOD(allowRTL:(BOOL)value) { - [[RCTI18nUtil sharedInstance] setAllowRTL:value]; + [[RCTI18nUtil sharedInstance] allowRTL:value]; +} + +RCT_EXPORT_METHOD(forceRTL:(BOOL)value) +{ + [[RCTI18nUtil sharedInstance] forceRTL:value]; } - (NSDictionary *)constantsToExport diff --git a/React/Modules/RCTI18nUtil.h b/React/Modules/RCTI18nUtil.h index a5865bd7c..b98719765 100644 --- a/React/Modules/RCTI18nUtil.h +++ b/React/Modules/RCTI18nUtil.h @@ -18,8 +18,10 @@ @interface RCTI18nUtil : NSObject - (BOOL)isRTL; -- (BOOL)allowRTL; -- (void)setAllowRTL:(BOOL)value; +- (BOOL)isRTLAllowed; +- (void)allowRTL:(BOOL)value; +- (BOOL)isRTLForced; +- (void)forceRTL:(BOOL)value; + (id)sharedInstance; @end diff --git a/React/Modules/RCTI18nUtil.m b/React/Modules/RCTI18nUtil.m index 72c5f5b06..40367574f 100644 --- a/React/Modules/RCTI18nUtil.m +++ b/React/Modules/RCTI18nUtil.m @@ -22,29 +22,57 @@ return sharedRCTI18nUtilInstance; } -// If current using language is RTL language and meanwhile set allowRTL on the JS side, -// the RN app will automatically have a RTL layout. +/** + * Check if the app is currently running on an RTL locale. + * This only happens when the app: + * - is forcing RTL layout, regardless of the active language (for development purpose) + * - allows RTL layout when using RTL locale + */ - (BOOL)isRTL { - if ([self allowRTL] && [self isApplicationPreferredLanguageRTL]) { + if ([self isRTLForced]) { + return YES; + } + if ([self isRTLAllowed] && [self isApplicationPreferredLanguageRTL]) { return YES; } return NO; } -- (BOOL)allowRTL +/** + * Should be used very early during app start up + * Before the bridge is initialized + */ +- (BOOL)isRTLAllowed { BOOL rtlStatus = [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_allowRTL"]; return rtlStatus; } -- (void)setAllowRTL:(BOOL)rtlStatus +- (void)allowRTL:(BOOL)rtlStatus { [[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_allowRTL"]; [[NSUserDefaults standardUserDefaults] synchronize]; } +/** + * Could be used to test RTL layout with English + * Used for development and testing purpose + */ +- (BOOL)isRTLForced +{ + BOOL rtlStatus = [[NSUserDefaults standardUserDefaults] + boolForKey:@"RCTI18nUtil_forceRTL"]; + return rtlStatus; +} + +- (void)forceRTL:(BOOL)rtlStatus +{ + [[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_forceRTL"]; + [[NSUserDefaults standardUserDefaults] synchronize]; +} + // Check if the current device language is RTL - (BOOL)isDevicePreferredLanguageRTL {