5package io.flutter.plugin.localization;
7import static io.flutter.Build.API_LEVELS;
9import android.annotation.SuppressLint;
11import android.content.res.Configuration;
14import androidx.annotation.NonNull;
15import androidx.annotation.Nullable;
16import androidx.annotation.VisibleForTesting;
17import io.flutter.embedding.engine.systemchannels.LocalizationChannel;
18import java.util.ArrayList;
20import java.util.Locale;
25 @NonNull
private final Context context;
28 "AppBundleLocaleChanges",
35 public String getStringResource(@NonNull String
key, @Nullable String localeString) {
36 Context localContext = context;
37 String stringToReturn =
null;
38 Locale savedLocale =
null;
40 if (localeString !=
null) {
43 Configuration config =
new Configuration(context.getResources().getConfiguration());
44 config.setLocale(locale);
45 localContext = context.createConfigurationContext(config);
48 String packageName = context.getPackageName();
49 int resId = localContext.getResources().getIdentifier(
key,
"string", packageName);
52 stringToReturn = localContext.getResources().getString(resId);
55 return stringToReturn;
62 this.context = context;
63 this.localizationChannel = localizationChannel;
72 @SuppressWarnings(
"deprecation")
75 if (supportedLocales ==
null || supportedLocales.isEmpty()) {
87 List<Locale.LanguageRange> languageRanges =
new ArrayList<>();
88 LocaleList localeList = context.getResources().getConfiguration().getLocales();
89 int localeCount = localeList.size();
90 for (
int index = 0; index < localeCount; ++index) {
91 Locale locale = localeList.get(index);
93 String fullRange = locale.getLanguage();
94 if (!locale.getScript().isEmpty()) {
95 fullRange +=
"-" + locale.getScript();
97 if (!locale.getCountry().isEmpty()) {
98 fullRange +=
"-" + locale.getCountry();
100 languageRanges.add(
new Locale.LanguageRange(fullRange));
101 languageRanges.add(
new Locale.LanguageRange(locale.getLanguage()));
102 languageRanges.add(
new Locale.LanguageRange(locale.getLanguage() +
"-*"));
104 Locale platformResolvedLocale = Locale.lookup(languageRanges, supportedLocales);
105 if (platformResolvedLocale !=
null) {
106 return platformResolvedLocale;
108 return supportedLocales.get(0);
112 LocaleList localeList = context.getResources().getConfiguration().getLocales();
113 for (
int index = 0; index < localeList.size(); ++index) {
114 Locale preferredLocale = localeList.get(index);
116 for (Locale locale : supportedLocales) {
117 if (preferredLocale.equals(locale)) {
122 for (Locale locale : supportedLocales) {
123 if (preferredLocale.getLanguage().equals(locale.toLanguageTag())) {
128 for (Locale locale : supportedLocales) {
129 if (preferredLocale.getLanguage().equals(locale.getLanguage())) {
134 return supportedLocales.get(0);
139 Locale preferredLocale = context.getResources().getConfiguration().locale;
140 if (preferredLocale !=
null) {
142 for (Locale locale : supportedLocales) {
143 if (preferredLocale.equals(locale)) {
148 for (Locale locale : supportedLocales) {
149 if (preferredLocale.getLanguage().equals(locale.toString())) {
154 return supportedLocales.get(0);
162 @SuppressWarnings(
"deprecation")
166 LocaleList localeList = config.getLocales();
167 int localeCount = localeList.size();
168 for (
int index = 0; index < localeCount; ++index) {
169 Locale locale = localeList.get(index);
173 locales.add(config.locale);
176 localizationChannel.sendLocales(locales);
187 localeString = localeString.replace(
'_',
'-');
190 String
parts[] = localeString.split(
"-", -1);
193 String languageCode =
parts[0];
194 String scriptCode =
"";
195 String countryCode =
"";
197 if (
parts.length > index &&
parts[index].length() == 4) {
198 scriptCode =
parts[index];
201 if (
parts.length > index &&
parts[index].length() >= 2 &&
parts[index].length() <= 3) {
202 countryCode =
parts[index];
206 return new Locale(languageCode, countryCode, scriptCode);
Locale resolveNativeLocale(@Nullable List< Locale > supportedLocales)
final LocalizationChannel.LocalizationMessageHandler localizationMessageHandler
void sendLocalesToFlutter(@NonNull Configuration config)
static Locale localeFromString(@NonNull String localeString)
LocalizationPlugin( @NonNull Context context, @NonNull LocalizationChannel localizationChannel)
def Build(configs, env, options)