forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: # Changelog: [Internal] - As in the title. Differential Revision: D57248159
- Loading branch information
1 parent
3f17c8b
commit 123826a
Showing
6 changed files
with
138 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
.../ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.java
This file was deleted.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
...ve/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nManagerModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.modules.i18nmanager | ||
|
||
import android.os.Build | ||
import com.facebook.fbreact.specs.NativeI18nManagerSpec | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.module.annotations.ReactModule | ||
|
||
/** [NativeModule] that allows JS to set allowRTL and get isRTL status. */ | ||
@ReactModule(name = NativeI18nManagerSpec.NAME) | ||
public class I18nManagerModule(context: ReactApplicationContext?) : NativeI18nManagerSpec(context) { | ||
override public fun getTypedExportedConstants(): Map<String, Any> { | ||
val context = getReactApplicationContext() | ||
val locale = | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { | ||
context.resources.configuration.locales[0] | ||
} else { | ||
@Suppress("DEPRECATION") | ||
context.resources.configuration.locale | ||
} | ||
return mapOf( | ||
"isRTL" to I18nUtil.instance.isRTL(context), | ||
"doLeftAndRightSwapInRTL" to I18nUtil.instance.doLeftAndRightSwapInRTL(context), | ||
"localeIdentifier" to locale.toString()) | ||
} | ||
|
||
override fun allowRTL(value: Boolean) { | ||
I18nUtil.instance.allowRTL(getReactApplicationContext(), value) | ||
} | ||
|
||
override fun forceRTL(value: Boolean) { | ||
I18nUtil.instance.forceRTL(getReactApplicationContext(), value) | ||
} | ||
|
||
override fun swapLeftAndRightInRTL(value: Boolean) { | ||
I18nUtil.instance.swapLeftAndRightInRTL(getReactApplicationContext(), value) | ||
} | ||
} |
98 changes: 0 additions & 98 deletions
98
...ct-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
...eact-native/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.modules.i18nmanager | ||
|
||
import android.content.Context | ||
import androidx.core.text.TextUtilsCompat | ||
import androidx.core.view.ViewCompat | ||
import java.util.Locale | ||
|
||
public class I18nUtil private constructor() { | ||
/** | ||
* Check if the device 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 | ||
*/ | ||
public fun isRTL(context: Context): Boolean = | ||
if (isRTLForced(context)) { | ||
true | ||
} else isRTLAllowed(context) && isDevicePreferredLanguageRTL | ||
|
||
/** | ||
* Should be used very early during app start up Before the bridge is initialized | ||
* | ||
* @return whether the app allows RTL layout, default is true | ||
*/ | ||
private fun isRTLAllowed(context: Context): Boolean = | ||
isPrefSet(context, KEY_FOR_PREFS_ALLOWRTL, true) | ||
|
||
public fun allowRTL(context: Context, allowRTL: Boolean) { | ||
setPref(context, KEY_FOR_PREFS_ALLOWRTL, allowRTL) | ||
} | ||
|
||
public fun doLeftAndRightSwapInRTL(context: Context): Boolean = | ||
isPrefSet(context, KEY_FOR_PERFS_MAKE_RTL_FLIP_LEFT_AND_RIGHT_STYLES, true) | ||
|
||
public fun swapLeftAndRightInRTL(context: Context, flip: Boolean) { | ||
setPref(context, KEY_FOR_PERFS_MAKE_RTL_FLIP_LEFT_AND_RIGHT_STYLES, flip) | ||
} | ||
|
||
/** Could be used to test RTL layout with English Used for development and testing purpose */ | ||
private fun isRTLForced(context: Context): Boolean = | ||
isPrefSet(context, KEY_FOR_PREFS_FORCERTL, false) | ||
|
||
public fun forceRTL(context: Context, forceRTL: Boolean) { | ||
setPref(context, KEY_FOR_PREFS_FORCERTL, forceRTL) | ||
} | ||
|
||
private val isDevicePreferredLanguageRTL: Boolean | ||
// Check if the current device language is RTL | ||
get() { | ||
val directionality = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) | ||
return directionality == ViewCompat.LAYOUT_DIRECTION_RTL | ||
} | ||
|
||
private fun isPrefSet(context: Context, key: String, defaultValue: Boolean): Boolean = | ||
context | ||
.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE) | ||
.getBoolean(key, defaultValue) | ||
|
||
private fun setPref(context: Context, key: String, value: Boolean) { | ||
val editor = context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit() | ||
editor.putBoolean(key, value) | ||
editor.apply() | ||
} | ||
|
||
public companion object { | ||
@JvmStatic public val instance: I18nUtil = I18nUtil() | ||
|
||
private const val SHARED_PREFS_NAME = "com.facebook.react.modules.i18nmanager.I18nUtil" | ||
private const val KEY_FOR_PREFS_ALLOWRTL = "RCTI18nUtil_allowRTL" | ||
private const val KEY_FOR_PREFS_FORCERTL = "RCTI18nUtil_forceRTL" | ||
private const val KEY_FOR_PERFS_MAKE_RTL_FLIP_LEFT_AND_RIGHT_STYLES = | ||
"RCTI18nUtil_makeRTLFlipLeftAndRightStyles" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters