Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | Package Attributes | List of all members
io.flutter.plugin.localization.LocalizationPlugin Class Reference

Public Member Functions

 LocalizationPlugin ( @NonNull Context context, @NonNull LocalizationChannel localizationChannel)
 
Locale resolveNativeLocale (@Nullable List< Locale > supportedLocales)
 
void sendLocalesToFlutter (@NonNull Configuration config)
 

Static Public Member Functions

static Locale localeFromString (@NonNull String localeString)
 

Package Attributes

final LocalizationChannel.LocalizationMessageHandler localizationMessageHandler
 

Detailed Description

Android implementation of the localization plugin.

Definition at line 23 of file LocalizationPlugin.java.

Constructor & Destructor Documentation

◆ LocalizationPlugin()

io.flutter.plugin.localization.LocalizationPlugin.LocalizationPlugin ( @NonNull Context  context,
@NonNull LocalizationChannel  localizationChannel 
)
inline

Definition at line 59 of file LocalizationPlugin.java.

60 {
61
62 this.context = context;
63 this.localizationChannel = localizationChannel;
65 }
void setLocalizationMessageHandler( @Nullable LocalizationMessageHandler localizationMessageHandler)
final LocalizationChannel.LocalizationMessageHandler localizationMessageHandler

Member Function Documentation

◆ localeFromString()

static Locale io.flutter.plugin.localization.LocalizationPlugin.localeFromString ( @NonNull String  localeString)
inlinestatic

Computes the Locale from the provided String with format language[-script][-region][-...], where script is an alphabet string of length 4, and region is either an alphabet string of length 2 or a digit string of length 3.

Definition at line 185 of file LocalizationPlugin.java.

185 {
186 // Normalize the locale string, replace all underscores with hyphens.
187 localeString = localeString.replace('_', '-');
188
189 // Pre-API 21, we fall back to manually parsing the locale tag.
190 String parts[] = localeString.split("-", -1);
191
192 // Assume the first part is always the language code.
193 String languageCode = parts[0];
194 String scriptCode = "";
195 String countryCode = "";
196 int index = 1;
197 if (parts.length > index && parts[index].length() == 4) {
198 scriptCode = parts[index];
199 index++;
200 }
201 if (parts.length > index && parts[index].length() >= 2 && parts[index].length() <= 3) {
202 countryCode = parts[index];
203 index++;
204 }
205 // Ignore the rest of the locale for this purpose.
206 return new Locale(languageCode, countryCode, scriptCode);
207 }

◆ resolveNativeLocale()

Locale io.flutter.plugin.localization.LocalizationPlugin.resolveNativeLocale ( @Nullable List< Locale >  supportedLocales)
inline

Computes the Locale in supportedLocales that best matches the user's preferred locales.

FlutterEngine must be non-null when this method is invoked.

Definition at line 74 of file LocalizationPlugin.java.

74 {
75 if (supportedLocales == null || supportedLocales.isEmpty()) {
76 return null;
77 }
78
79 // Android improved the localization resolution algorithms after API 24 (7.0, Nougat).
80 // See https://developer.android.com/guide/topics/resources/multilingual-support
81 //
82 // LanguageRange and Locale.lookup was added in API 26 and is the preferred way to
83 // select a locale. Pre-API 26, we implement a manual locale resolution.
84 if (Build.VERSION.SDK_INT >= API_LEVELS.API_26) {
85 // Modern locale resolution using LanguageRange
86 // https://developer.android.com/guide/topics/resources/multilingual-support#postN
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);
92 // Convert locale string into language range format.
93 String fullRange = locale.getLanguage();
94 if (!locale.getScript().isEmpty()) {
95 fullRange += "-" + locale.getScript();
96 }
97 if (!locale.getCountry().isEmpty()) {
98 fullRange += "-" + locale.getCountry();
99 }
100 languageRanges.add(new Locale.LanguageRange(fullRange));
101 languageRanges.add(new Locale.LanguageRange(locale.getLanguage()));
102 languageRanges.add(new Locale.LanguageRange(locale.getLanguage() + "-*"));
103 }
104 Locale platformResolvedLocale = Locale.lookup(languageRanges, supportedLocales);
105 if (platformResolvedLocale != null) {
106 return platformResolvedLocale;
107 }
108 return supportedLocales.get(0);
109 } else if (Build.VERSION.SDK_INT >= API_LEVELS.API_24) {
110 // Modern locale resolution without languageRange
111 // https://developer.android.com/guide/topics/resources/multilingual-support#postN
112 LocaleList localeList = context.getResources().getConfiguration().getLocales();
113 for (int index = 0; index < localeList.size(); ++index) {
114 Locale preferredLocale = localeList.get(index);
115 // Look for exact match.
116 for (Locale locale : supportedLocales) {
117 if (preferredLocale.equals(locale)) {
118 return locale;
119 }
120 }
121 // Look for exact language only match.
122 for (Locale locale : supportedLocales) {
123 if (preferredLocale.getLanguage().equals(locale.toLanguageTag())) {
124 return locale;
125 }
126 }
127 // Look for any locale with matching language.
128 for (Locale locale : supportedLocales) {
129 if (preferredLocale.getLanguage().equals(locale.getLanguage())) {
130 return locale;
131 }
132 }
133 }
134 return supportedLocales.get(0);
135 }
136
137 // Legacy locale resolution
138 // https://developer.android.com/guide/topics/resources/multilingual-support#preN
139 Locale preferredLocale = context.getResources().getConfiguration().locale;
140 if (preferredLocale != null) {
141 // Look for exact match.
142 for (Locale locale : supportedLocales) {
143 if (preferredLocale.equals(locale)) {
144 return locale;
145 }
146 }
147 // Look for exact language only match.
148 for (Locale locale : supportedLocales) {
149 if (preferredLocale.getLanguage().equals(locale.toString())) {
150 return locale;
151 }
152 }
153 }
154 return supportedLocales.get(0);
155 }
def Build(configs, env, options)
Definition: build.py:232

◆ sendLocalesToFlutter()

void io.flutter.plugin.localization.LocalizationPlugin.sendLocalesToFlutter ( @NonNull Configuration  config)
inline

Send the current Locale configuration to Flutter.

FlutterEngine must be non-null when this method is invoked.

Definition at line 163 of file LocalizationPlugin.java.

163 {
164 List<Locale> locales = new ArrayList<>();
165 if (Build.VERSION.SDK_INT >= API_LEVELS.API_24) {
166 LocaleList localeList = config.getLocales();
167 int localeCount = localeList.size();
168 for (int index = 0; index < localeCount; ++index) {
169 Locale locale = localeList.get(index);
170 locales.add(locale);
171 }
172 } else {
173 locales.add(config.locale);
174 }
175
176 localizationChannel.sendLocales(locales);
177 }
void add(sk_sp< SkIDChangeListener > listener) SK_EXCLUDES(fMutex)

Member Data Documentation

◆ localizationMessageHandler

final LocalizationChannel.LocalizationMessageHandler io.flutter.plugin.localization.LocalizationPlugin.localizationMessageHandler
package
Initial value:
=
new LocalizationChannel.LocalizationMessageHandler() {
@Override
public String getStringResource(@NonNull String key, @Nullable String localeString) {
Context localContext = context;
String stringToReturn = null;
Locale savedLocale = null;
if (localeString != null) {
Locale locale = localeFromString(localeString);
Configuration config = new Configuration(context.getResources().getConfiguration());
config.setLocale(locale);
localContext = context.createConfigurationContext(config);
}
String packageName = context.getPackageName();
int resId = localContext.getResources().getIdentifier(key, "string", packageName);
if (resId != 0) {
stringToReturn = localContext.getResources().getString(resId);
}
return stringToReturn;
}
}
static Locale localeFromString(@NonNull String localeString)

Definition at line 32 of file LocalizationPlugin.java.


The documentation for this class was generated from the following file: