mirror of
https://github.com/status-im/react-native.git
synced 2025-01-09 09:12:02 +00:00
96de161304
Summary: We're now enabling RTL layout by default assuming the app has the proper localized assets. Previously, this was disabled by default to minimize surprise. Apps that don't want this RTL support can still manually disable them by using `RCTI18nUtil.allowRTL(false)` (iOS) or `I18nUtil.allowRTL(false)` (android) in the apps start-up logic, as outlined in the blog post: http://facebook.github.io/react-native/blog/2016/08/19/right-to-left-support-for-react-native-apps.html iOS Util function:f0fb228ec7/React/Modules/RCTI18nUtil.m (L53)
Android Util function:380830e4aa/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java (L63)
Differential Revision: D3825054 fbshipit-source-id: 88b355fef9e3847939a414f80d2285979e27af08
95 lines
2.6 KiB
Objective-C
95 lines
2.6 KiB
Objective-C
/**
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
* All rights reserved.
|
|
*
|
|
* This source code is licensed under the BSD-style license found in the
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
*/
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#import "RCTI18nUtil.h"
|
|
|
|
@implementation RCTI18nUtil
|
|
|
|
+ (id)sharedInstance {
|
|
static RCTI18nUtil *sharedRCTI18nUtilInstance = nil;
|
|
@synchronized(self) {
|
|
if (sharedRCTI18nUtilInstance == nil)
|
|
sharedRCTI18nUtilInstance = [self new];
|
|
}
|
|
return sharedRCTI18nUtilInstance;
|
|
}
|
|
|
|
/**
|
|
* 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 isRTLForced]) {
|
|
return YES;
|
|
}
|
|
if ([self isRTLAllowed] && [self isApplicationPreferredLanguageRTL]) {
|
|
return YES;
|
|
}
|
|
return NO;
|
|
}
|
|
|
|
/**
|
|
* Should be used very early during app start up
|
|
* Before the bridge is initialized
|
|
* @return whether the app allows RTL layout, default is true
|
|
*/
|
|
- (BOOL)isRTLAllowed
|
|
{
|
|
NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:@"RCTI18nUtil_allowRTL"];
|
|
if (value == nil) {
|
|
return YES;
|
|
}
|
|
return [value boolValue];
|
|
}
|
|
|
|
- (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
|
|
{
|
|
NSLocaleLanguageDirection direction = [NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]];
|
|
return direction == NSLocaleLanguageDirectionRightToLeft;
|
|
}
|
|
|
|
// Check if the current application language is RTL
|
|
- (BOOL)isApplicationPreferredLanguageRTL
|
|
{
|
|
NSString *preferredAppLanguage = [[[NSBundle mainBundle] preferredLocalizations] objectAtIndex:0];
|
|
NSLocaleLanguageDirection direction = [NSLocale characterDirectionForLanguage:preferredAppLanguage];
|
|
return direction == NSLocaleLanguageDirectionRightToLeft;
|
|
}
|
|
|
|
@end
|