RCTI18nUtil.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTI18nUtil.h"
  8. #import <UIKit/UIKit.h>
  9. @implementation RCTI18nUtil
  10. + (instancetype)sharedInstance
  11. {
  12. static RCTI18nUtil *sharedInstance;
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. sharedInstance = [self new];
  16. [sharedInstance swapLeftAndRightInRTL:true];
  17. });
  18. return sharedInstance;
  19. }
  20. /**
  21. * Check if the app is currently running on an RTL locale.
  22. * This only happens when the app:
  23. * - is forcing RTL layout, regardless of the active language (for development purpose)
  24. * - allows RTL layout when using RTL locale
  25. */
  26. - (BOOL)isRTL
  27. {
  28. if ([self isRTLForced]) {
  29. return YES;
  30. }
  31. if ([self isRTLAllowed] && [self isApplicationPreferredLanguageRTL]) {
  32. return YES;
  33. }
  34. return NO;
  35. }
  36. /**
  37. * Should be used very early during app start up
  38. * Before the bridge is initialized
  39. * @return whether the app allows RTL layout, default is true
  40. */
  41. - (BOOL)isRTLAllowed
  42. {
  43. NSNumber *value = [[NSUserDefaults standardUserDefaults] objectForKey:@"RCTI18nUtil_allowRTL"];
  44. if (value == nil) {
  45. return YES;
  46. }
  47. return [value boolValue];
  48. }
  49. - (void)allowRTL:(BOOL)rtlStatus
  50. {
  51. [[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_allowRTL"];
  52. [[NSUserDefaults standardUserDefaults] synchronize];
  53. }
  54. /**
  55. * Could be used to test RTL layout with English
  56. * Used for development and testing purpose
  57. */
  58. - (BOOL)isRTLForced
  59. {
  60. BOOL rtlStatus = [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_forceRTL"];
  61. return rtlStatus;
  62. }
  63. - (void)forceRTL:(BOOL)rtlStatus
  64. {
  65. [[NSUserDefaults standardUserDefaults] setBool:rtlStatus forKey:@"RCTI18nUtil_forceRTL"];
  66. [[NSUserDefaults standardUserDefaults] synchronize];
  67. }
  68. - (BOOL)doLeftAndRightSwapInRTL
  69. {
  70. return [[NSUserDefaults standardUserDefaults] boolForKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
  71. }
  72. - (void)swapLeftAndRightInRTL:(BOOL)value
  73. {
  74. [[NSUserDefaults standardUserDefaults] setBool:value forKey:@"RCTI18nUtil_makeRTLFlipLeftAndRightStyles"];
  75. [[NSUserDefaults standardUserDefaults] synchronize];
  76. }
  77. // Check if the current device language is RTL
  78. - (BOOL)isDevicePreferredLanguageRTL
  79. {
  80. NSLocaleLanguageDirection direction =
  81. [NSLocale characterDirectionForLanguage:[[NSLocale preferredLanguages] objectAtIndex:0]];
  82. return direction == NSLocaleLanguageDirectionRightToLeft;
  83. }
  84. // Check if the current application language is RTL
  85. - (BOOL)isApplicationPreferredLanguageRTL
  86. {
  87. NSWritingDirection direction = [NSParagraphStyle defaultWritingDirectionForLanguage:nil];
  88. return direction == NSWritingDirectionRightToLeft;
  89. }
  90. @end