Provide I18n Module in OSS 1/3
Summary: Create I18nUtil and I18nManagerModule for OSS. Reviewed By: dmmiller Differential Revision: D3575674 fbshipit-source-id: aad96be167f3e7b0692a9cc2c7f8a558e7aa722a
This commit is contained in:
parent
c7ff46bf55
commit
0ab1486f7e
|
@ -0,0 +1,19 @@
|
||||||
|
include_defs('//ReactAndroid/DEFS')
|
||||||
|
|
||||||
|
android_library(
|
||||||
|
name = 'i18nmanager',
|
||||||
|
srcs = glob(['*.java']),
|
||||||
|
deps = [
|
||||||
|
react_native_dep('third-party/java/infer-annotations:infer-annotations'),
|
||||||
|
react_native_dep('third-party/java/jsr-305:jsr-305'),
|
||||||
|
react_native_target('java/com/facebook/react/bridge:bridge'),
|
||||||
|
react_native_target('java/com/facebook/react/common:common'),
|
||||||
|
],
|
||||||
|
visibility = [
|
||||||
|
'PUBLIC',
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
project_config(
|
||||||
|
src_target = ':i18nmanager',
|
||||||
|
)
|
|
@ -0,0 +1,54 @@
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.facebook.react.modules.i18nmanager;
|
||||||
|
|
||||||
|
import com.facebook.react.bridge.NativeModule;
|
||||||
|
import com.facebook.react.bridge.ReactApplicationContext;
|
||||||
|
import com.facebook.react.bridge.ReactContext;
|
||||||
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||||
|
import com.facebook.react.bridge.ReactMethod;
|
||||||
|
import com.facebook.react.common.MapBuilder;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link NativeModule} that allows JS to set allowRTL and get isRTL status.
|
||||||
|
*/
|
||||||
|
public class I18nManagerModule extends ReactContextBaseJavaModule {
|
||||||
|
|
||||||
|
private final I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
|
||||||
|
|
||||||
|
public I18nManagerModule(ReactApplicationContext reactContext) {
|
||||||
|
super(reactContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "I18nManager";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, Object> getConstants() {
|
||||||
|
final Map<String, Object> constants = MapBuilder.newHashMap();
|
||||||
|
constants.put("isRTL", sharedI18nUtilInstance.isRTL(
|
||||||
|
getReactApplicationContext()
|
||||||
|
));
|
||||||
|
return constants;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ReactMethod
|
||||||
|
public void allowRTL(boolean value) {
|
||||||
|
sharedI18nUtilInstance.setAllowRTL(
|
||||||
|
getReactApplicationContext(),
|
||||||
|
value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.facebook.react.modules.i18nmanager;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
import android.content.SharedPreferences.Editor;
|
||||||
|
|
||||||
|
public class I18nUtil {
|
||||||
|
private static I18nUtil sharedI18nUtilInstance = null;
|
||||||
|
|
||||||
|
private static final String MY_PREFS_NAME =
|
||||||
|
"com.facebook.react.modules.i18nmanager.I18nUtil";
|
||||||
|
private static final String KEY_FOR_PREFS =
|
||||||
|
"RCTI18nUtil_allowRTL";
|
||||||
|
|
||||||
|
private I18nUtil() {
|
||||||
|
// Exists only to defeat instantiation.
|
||||||
|
}
|
||||||
|
|
||||||
|
public static I18nUtil getInstance() {
|
||||||
|
if(sharedI18nUtilInstance == null) {
|
||||||
|
sharedI18nUtilInstance = new I18nUtil();
|
||||||
|
}
|
||||||
|
return sharedI18nUtilInstance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If set allowRTL on the JS side,
|
||||||
|
// the RN app will automatically have a RTL layout.
|
||||||
|
public boolean isRTL(Context context) {
|
||||||
|
return allowRTL(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean allowRTL(Context context) {
|
||||||
|
SharedPreferences prefs =
|
||||||
|
context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE);
|
||||||
|
return prefs.getBoolean(KEY_FOR_PREFS, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAllowRTL(Context context, boolean allowRTL) {
|
||||||
|
SharedPreferences.Editor editor =
|
||||||
|
context.getSharedPreferences(MY_PREFS_NAME, Context.MODE_PRIVATE).edit();
|
||||||
|
editor.putBoolean(KEY_FOR_PREFS, allowRTL);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue