Skip to content

Commit

Permalink
[Android] Return a IETF language tag instead of debug string
Browse files Browse the repository at this point in the history
  • Loading branch information
proninyaroslav committed Mar 15, 2022
1 parent 1d16bab commit 4c41fa6
Showing 1 changed file with 12 additions and 2 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();
}
}
}

0 comments on commit 4c41fa6

Please sign in to comment.