Provide forceRTL for LTR language to test

Summary:
1. Provide forceRTL function for developer to test RTL layout in LTR language bundle in I18nUtil and expose it in I18nManager.
2. Rename `allowRTL` and `setAllowRTL` functions

Reviewed By: fkgozali

Differential Revision: D3663693

fbshipit-source-id: 3db13a44c069ae73d1728c211306422db5dd8122
This commit is contained in:
Mengjue Wang 2016-08-04 12:18:26 -07:00 коммит произвёл Facebook Github Bot 8
Родитель 0dd93b62b4
Коммит f0fb228ec7
4 изменённых файлов: 45 добавлений и 8 удалений

Просмотреть файл

@ -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;

Просмотреть файл

@ -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

Просмотреть файл

@ -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

Просмотреть файл

@ -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
{