diff --git a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java index 42be91d1..86654ffd 100644 --- a/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java +++ b/doric-android/doric/src/main/java/pub/doric/engine/DoricJSEngine.java @@ -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 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 diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m index 04a1ceb1..34325d98 100644 --- a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m +++ b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m @@ -44,7 +44,7 @@ - (void)onLog:(DoricLogType)type message:(NSString *)message { @interface DoricJSEngine () @property(nonatomic, strong) NSMutableDictionary *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]; diff --git a/doric-js/index.d.ts b/doric-js/index.d.ts index 10e7c8d7..437874b6 100644 --- a/doric-js/index.d.ts +++ b/doric-js/index.d.ts @@ -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): void; diff --git a/doric-js/lib/src/runtime/global.d.ts b/doric-js/lib/src/runtime/global.d.ts index df921338..d401ded8 100644 --- a/doric-js/lib/src/runtime/global.d.ts +++ b/doric-js/lib/src/runtime/global.d.ts @@ -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): void; diff --git a/doric-js/src/runtime/global.ts b/doric-js/src/runtime/global.ts index fc095ad8..8d5c3f2b 100644 --- a/doric-js/src/runtime/global.ts +++ b/doric-js/src/runtime/global.ts @@ -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 }