feat:Environment add locale

This commit is contained in:
pengfei.zhou 2021-07-06 17:35:56 +08:00 committed by osborn
parent 666998d3a6
commit 70bde4fba9
5 changed files with 42 additions and 13 deletions

View File

@ -18,7 +18,6 @@ package pub.doric.engine;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
@ -35,6 +34,7 @@ import com.github.pengfeizhou.jscore.JavaFunction;
import com.github.pengfeizhou.jscore.JavaValue;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Map;
import pub.doric.Doric;
@ -60,6 +60,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
protected IDoricJSE mDoricJSE;
private final DoricTimerExtension mTimerExtension;
private final DoricRegistry mDoricRegistry = new DoricRegistry();
private final JSONBuilder mEnvironment = new JSONBuilder();
public DoricJSEngine() {
handlerThread = new HandlerThread(this.getClass().getSimpleName());
@ -100,7 +101,7 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
} catch (Exception e) {
e.printStackTrace();
}
JSONBuilder envObject = new JSONBuilder()
mEnvironment
.put("platform", "Android")
.put("platformVersion", String.valueOf(android.os.Build.VERSION.SDK_INT))
.put("appName", appName)
@ -111,15 +112,17 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
.put("statusBarHeight", DoricUtils.px2dp(DoricUtils.getStatusBarHeight()))
.put("hasNotch", false)
.put("deviceBrand", Build.BRAND)
.put("deviceModel", Build.MODEL);
.put("deviceModel", Build.MODEL)
.put("localeLanguage", context.getResources().getConfiguration().locale.getLanguage())
.put("localeCountry", context.getResources().getConfiguration().locale.getCountry());
Map<String, Object> extend = mDoricRegistry.getEnvironmentVariables();
for (String key : extend.keySet()) {
envObject.put(key, extend.get(key));
mEnvironment.put(key, extend.get(key));
}
mDoricJSE.injectGlobalJSObject(DoricConstant.INJECT_ENVIRONMENT,
new JavaValue(envObject.toJSONObject()));
new JavaValue(mEnvironment.toJSONObject()));
mDoricJSE.injectGlobalJSFunction(DoricConstant.INJECT_LOG, new JavaFunction() {
@Override

View File

@ -44,7 +44,7 @@ - (void)onLog:(DoricLogType)type message:(NSString *)message {
@interface DoricJSEngine ()
@property(nonatomic, strong) NSMutableDictionary <NSNumber *, NSTimer *> *timers;
@property(nonatomic, strong) DoricBridgeExtension *bridgeExtension;
@property(nonatomic, strong) NSDictionary *innerEnvironmentDictionary;
@property(nonatomic, strong) NSMutableDictionary *environmentDictionary;
@property(nonatomic, strong) NSThread *jsThread;
@property(nonatomic, assign) BOOL destroyed;
@end
@ -73,8 +73,7 @@ - (instancetype)init {
screenWidth = [[UIScreen mainScreen] bounds].size.height;
screenHeight = [[UIScreen mainScreen] bounds].size.width;
}
_innerEnvironmentDictionary = @{
_environmentDictionary = @{
@"platform": @"iOS",
@"platformVersion": [[UIDevice currentDevice] systemVersion],
@"appName": infoDictionary[@"CFBundleName"],
@ -86,7 +85,9 @@ - (instancetype)init {
@"hasNotch": @(hasNotch()),
@"deviceBrand": @"Apple",
@"deviceModel": platform,
};
@"localeLanguage": [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode],
@"localeCountry": [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode],
}.mutableCopy;
self.registry = [[DoricRegistry alloc] init];
[self ensureRunOnJSThread:^() {
self.timers = [[NSMutableDictionary alloc] init];
@ -135,12 +136,10 @@ - (void)initJSEngine {
- (void)initJSExecutor {
__weak typeof(self) _self = self;
NSMutableDictionary *envDic = [self.innerEnvironmentDictionary mutableCopy];
[self.registry.environmentVariables enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
envDic[key] = obj;
self.environmentDictionary[key] = obj;
}];
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[envDic copy]];
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:[self.environmentDictionary copy]];
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
if ([type isEqualToString:@"e"]) {
[self.registry onLog:DoricLogTypeError message:message];

8
doric-js/index.d.ts vendored
View File

@ -60,6 +60,14 @@ declare module 'doric/lib/src/runtime/global' {
* ex: iPhone12,5 or pixel 3
*/
deviceModel: string;
/**
* The language code for current locale
*/
localeLanguage: string;
/**
* The country/region code for current locale
*/
localeCountry: string;
[index: string]: number | string | boolean | object | undefined;
};
function Entry(constructor: ClassType<Panel>): void;

View File

@ -48,6 +48,14 @@ declare global {
* ex: iPhone12,5 or pixel 3
*/
deviceModel: string;
/**
* The language code for current locale
*/
localeLanguage: string;
/**
* The country/region code for current locale
*/
localeCountry: string;
[index: string]: number | string | boolean | object | undefined;
};
function Entry(constructor: ClassType<Panel>): void;

View File

@ -75,6 +75,17 @@ declare global {
* ex: iPhone12,5 or pixel 3
*/
deviceModel: string,
/**
* The language code for current locale
*/
localeLanguage: string
/**
* The country/region code for current locale
*/
localeCountry: string
[index: string]: number | string | boolean | object | undefined
}