2016-06-21 18:51:44 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-06-21 18:51:44 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-06-21 18:51:44 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTI18nUtil.h"
|
|
|
|
|
2018-02-26 19:35:51 +00:00
|
|
|
#import <UIKit/UIKit.h>
|
|
|
|
|
2016-06-21 18:51:44 +00:00
|
|
|
@implementation RCTI18nUtil
|
|
|
|
|
2017-10-04 02:48:38 +00:00
|
|
|
+ (instancetype)sharedInstance
|
|
|
|
{
|
|
|
|
static RCTI18nUtil *sharedInstance;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedInstance = [self new];
|
2017-10-25 03:32:17 +00:00
|
|
|
[sharedInstance swapLeftAndRightInRTL: true];
|
2017-10-04 02:48:38 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return sharedInstance;
|
2016-06-21 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 19:18:26 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2016-06-21 18:51:44 +00:00
|
|
|
- (BOOL)isRTL
|
|
|
|
{
|
2016-08-04 19:18:26 +00:00
|
|
|
if ([self isRTLForced]) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
if ([self isRTLAllowed] && [self isApplicationPreferredLanguageRTL]) {
|
2016-06-24 01:54:01 +00:00
|
|
|
return YES;
|
|
|
|
}
|
2016-06-21 18:51:44 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2016-08-04 19:18:26 +00:00
|
|
|
/**
|
|
|
|
* Should be used very early during app start up
|
|
|
|
* Before the bridge is initialized
|
2016-09-07 00:45:37 +00:00
|
|
|
* @return whether the app allows RTL layout, default is true
|
2016-08-04 19:18:26 +00:00
|
|
|
*/
|
|
|
|
- (BOOL)isRTLAllowed
|
2016-06-21 18:51:44 +00:00
|
|
|
{
|
2016-09-07 00:45:37 +00:00
|
|
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:@"RCTI18nUtil_allowRTL"];
|
|
|
|
if (value == nil) {
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return [value boolValue];
|
2016-06-21 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
2016-08-04 19:18:26 +00:00
|
|
|
- (void)allowRTL:(BOOL)rtlStatus
|
2016-06-21 18:51:44 +00:00
|
|
|
{
|
2016-06-27 19:57:45 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_allowRTL"];
|
2016-06-21 18:51:44 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
|
}
|
|
|
|
|
2016-08-04 19:18:26 +00:00
|
|
|
/**
|
|
|
|
* 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];
|
|
|
|
}
|
|
|
|
|
2017-10-25 03:32:17 +00:00
|
|
|
- (BOOL)doLeftAndRightSwapInRTL
|
2017-10-19 02:29:36 +00:00
|
|
|
{
|
|
|
|
return [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
|
|
|
|
}
|
|
|
|
|
2017-10-25 03:32:17 +00:00
|
|
|
- (void)swapLeftAndRightInRTL:(BOOL)value
|
2017-10-19 02:29:36 +00:00
|
|
|
{
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:value forKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
|
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
|
}
|
|
|
|
|
2016-06-25 02:32:54 +00:00
|
|
|
// Check if the current device language is RTL
|
2016-06-24 01:54:01 +00:00
|
|
|
- (BOOL)isDevicePreferredLanguageRTL
|
|
|
|
{
|
|
|
|
NSLocaleLanguageDirection direction = [NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]];
|
|
|
|
return direction == NSLocaleLanguageDirectionRightToLeft;
|
|
|
|
}
|
|
|
|
|
2016-06-25 02:32:54 +00:00
|
|
|
// Check if the current application language is RTL
|
|
|
|
- (BOOL)isApplicationPreferredLanguageRTL
|
|
|
|
{
|
2018-02-26 19:35:51 +00:00
|
|
|
NSWritingDirection direction = [NSParagraphStyle defaultWritingDirectionForLanguage:nil];
|
|
|
|
return direction == NSWritingDirectionRightToLeft;
|
2016-06-25 02:32:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-21 18:51:44 +00:00
|
|
|
@end
|