Repository: incubator-weex Updated Branches: refs/heads/master 3a0ac37de -> 7a2743136
+ [android] new local module Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/aa28a1f9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/aa28a1f9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/aa28a1f9 Branch: refs/heads/master Commit: aa28a1f99f1f2ba7f1adb282892c5fc10afb5b26 Parents: fc7bcca Author: misakuo <misa...@apache.org> Authored: Wed Sep 27 14:26:03 2017 +0800 Committer: misakuo <misa...@apache.org> Committed: Wed Sep 27 14:26:03 2017 +0800 ---------------------------------------------------------------------- .../main/java/com/taobao/weex/WXSDKEngine.java | 2 + .../taobao/weex/ui/module/WXLocalModule.java | 93 ++++++++++++++++++++ 2 files changed, 95 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/aa28a1f9/android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java b/android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java index 175e96e..52a688c 100644 --- a/android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java +++ b/android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java @@ -89,6 +89,7 @@ import com.taobao.weex.ui.component.list.SimpleListComponent; import com.taobao.weex.ui.component.list.WXCell; import com.taobao.weex.ui.component.list.WXListComponent; import com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList; +import com.taobao.weex.ui.module.WXLocalModule; import com.taobao.weex.ui.module.WXMetaModule; import com.taobao.weex.ui.module.WXModalUIModule; import com.taobao.weex.ui.module.WXTimerModule; @@ -300,6 +301,7 @@ public class WXSDKEngine { registerModule("picker", WXPickersModule.class); registerModule("meta", WXMetaModule.class,true); registerModule("webSocket", WebSocketModule.class); + registerModule("local", WXLocalModule.class); registerDomObject(simpleList, WXListDomObject.class); http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/aa28a1f9/android/sdk/src/main/java/com/taobao/weex/ui/module/WXLocalModule.java ---------------------------------------------------------------------- diff --git a/android/sdk/src/main/java/com/taobao/weex/ui/module/WXLocalModule.java b/android/sdk/src/main/java/com/taobao/weex/ui/module/WXLocalModule.java new file mode 100644 index 0000000..5f6d8cb --- /dev/null +++ b/android/sdk/src/main/java/com/taobao/weex/ui/module/WXLocalModule.java @@ -0,0 +1,93 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package com.taobao.weex.ui.module; + +import android.content.Context; +import android.content.res.Configuration; +import android.content.res.Resources; +import android.os.Build; +import android.os.LocaleList; +import android.text.TextUtils; + +import com.taobao.weex.WXEnvironment; +import com.taobao.weex.annotation.JSMethod; +import com.taobao.weex.bridge.JSCallback; +import com.taobao.weex.common.WXModule; + +import java.util.Locale; + +/** + * Created by moxun on 2017/9/26. + * + * Ref: https://tools.ietf.org/html/bcp47 + */ + +public class WXLocalModule extends WXModule { + + @JSMethod + public void getLanguage(JSCallback callback) { + callback.invoke(getLanguageTags()); + } + + @JSMethod + public void getLanguages(JSCallback callback) { + callback.invoke(getLanguageTags().split(",")); + } + + private String getLanguageTags() { + Context application = WXEnvironment.getApplication(); + if (application != null) { + Resources res = application.getResources(); + if (res != null) { + Configuration configuration = res.getConfiguration(); + if (configuration != null) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + LocaleList localeList = configuration.getLocales(); + return localeList.toLanguageTags(); + } else { + Locale local = configuration.locale; + if (local != null) { + return toLanguageTag(local); + } + } + } + } + } + return ""; + } + + private String toLanguageTag(Locale locale) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + return locale.toLanguageTag(); + } else { + StringBuilder sb = new StringBuilder(); + String language = locale.getLanguage(); + String script = locale.getScript(); + String region = locale.getCountry(); + sb.append(language); + if (!TextUtils.isEmpty(script)) { + sb.append("-").append(script); + } + if (!TextUtils.isEmpty(region)) { + sb.append("-").append(region); + } + return sb.toString(); + } + } +}