Skip to content

Commit

Permalink
Merge pull request #40 from proninyaroslav/android-ietf-tags
Browse files Browse the repository at this point in the history
[Android] Using a well-formed IETF language tags instead of debug string
  • Loading branch information
magnatronus authored May 7, 2022
2 parents fd5c1e8 + 4c41fa6 commit c7f42fc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import androidx.core.os.LocaleListCompat;
import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -49,7 +51,7 @@ public void onMethodCall(MethodCall call, @NonNull Result result) {
}

private String getCurrentLocale() {
return Locale.getDefault().toString();
return getLocaleTag(Locale.getDefault());
}

private List<String> getPreferredLanguages() {
Expand All @@ -58,12 +60,20 @@ private List<String> getPreferredLanguages() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
LocaleListCompat list = LocaleListCompat.getAdjustedDefault();
for (int i = 0; i < list.size(); i++) {
result.add(list.get(i).toString());
result.add(getLocaleTag(list.get(i)));
}
} else {
result.add(getCurrentLocale());
}

return result;
}

private String getLocaleTag(Locale locale) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return locale.toLanguageTag();
} else {
return locale.toString();
}
}
}
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.example.devicelocale_example"
minSdkVersion 16
targetSdkVersion 30
targetSdkVersion 31
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down

0 comments on commit c7f42fc

Please sign in to comment.