diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java index 0894e4fa0..b6d8e7546 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/i18nmanager/I18nUtil.java @@ -20,7 +20,7 @@ import java.util.Locale; public class I18nUtil { private static I18nUtil sharedI18nUtilInstance = null; - private static final String MY_PREFS_NAME = + private static final String SHARED_PREFS_NAME = "com.facebook.react.modules.i18nmanager.I18nUtil"; private static final String KEY_FOR_PREFS_ALLOWRTL = "RCTI18nUtil_allowRTL"; @@ -57,16 +57,11 @@ public class I18nUtil { * Before the bridge is initialized */ private boolean isRTLAllowed(Context context) { - SharedPreferences prefs = - context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE); - return prefs.getBoolean(KEY_FOR_PREFS_ALLOWRTL, false); + return isPrefSet(context, KEY_FOR_PREFS_ALLOWRTL, false); } public void allowRTL(Context context, boolean allowRTL) { - SharedPreferences.Editor editor = - context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit(); - editor.putBoolean(KEY_FOR_PREFS_ALLOWRTL, allowRTL); - editor.apply(); + setPref(context, KEY_FOR_PREFS_ALLOWRTL, allowRTL); } /** @@ -74,16 +69,11 @@ public class I18nUtil { * Used for development and testing purpose */ private boolean isRTLForced(Context context) { - SharedPreferences prefs = - context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE); - return prefs.getBoolean(KEY_FOR_PREFS_FORCERTL, false); + return isPrefSet(context, KEY_FOR_PREFS_FORCERTL, false); } - public void forceRTL(Context context, boolean allowRTL) { - SharedPreferences.Editor editor = - context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit(); - editor.putBoolean(KEY_FOR_PREFS_FORCERTL, allowRTL); - editor.apply(); + public void forceRTL(Context context, boolean forceRTL) { + setPref(context, KEY_FOR_PREFS_FORCERTL, forceRTL); } // Check if the current device language is RTL @@ -92,4 +82,17 @@ public class I18nUtil { TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()); return directionality == ViewCompat.LAYOUT_DIRECTION_RTL; } + + private boolean isPrefSet(Context context, String key, boolean defaultValue) { + SharedPreferences prefs = + context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE); + return prefs.getBoolean(key, defaultValue); + } + + private void setPref(Context context, String key, boolean value) { + SharedPreferences.Editor editor = + context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE).edit(); + editor.putBoolean(key, value); + editor.apply(); + } }