matchLocale static method
Implementation
static List<String> matchLocale({
required List<String> files,
Locale? locale,
required String name,
required String extension,
}) {
files.sort((a, b) {
final aLocalized = a.startsWith(RegExp('^[a-z]+(-[a-z]+)?\\.lproj\\/'));
final bLocalized = b.startsWith(RegExp('^[a-z]+(-[a-z]+)?\\.lproj\\/'));
if (aLocalized && bLocalized) {
return a.compareTo(b);
} else if (aLocalized) {
return -1;
} else {
return 1;
}
});
files = files.reversed.toList();
List<RegExp> expressions = <RegExp>[];
// adding the fallbacks
// - match only unlocalized
// - match the five mostly spoken languages of the world, copied from Wikipedia
// - match just *any* language
expressions.addAll(
[
RegExp(
'^[a-z]+(-[a-z]+)?\\.lproj\\/$name(@\\d+x)?\\.$extension\$',
unicode: true,
caseSensitive: false,
),
...['en', 'zh', 'hi', 'es', 'fr'].reversed.map(
(language) => RegExp(
'^$language(-[a-z]+)?\\.lproj\\/$name(@\\d+x)?\\.$extension\$',
unicode: true,
caseSensitive: false,
),
),
RegExp(
'^$name(@\\d+x)?\\.$extension\$',
unicode: true,
caseSensitive: false,
),
],
);
if (locale != null) {
final language = locale.languageCode;
expressions.add(
RegExp(
'^$language(-[a-z]+)?\\.lproj\\/$name(@\\d+x)?\\.$extension\$',
unicode: true,
caseSensitive: false,
),
);
final region = locale.countryCode;
if (region != null) {
expressions.add(
RegExp(
'^$language-$region\\.lproj\\/$name(@\\d+x)?\\.$extension\$',
unicode: true,
caseSensitive: false,
),
);
}
}
for (final regex in expressions.reversed) {
final matches = <String>[];
for (final file in files) {
final match = regex.stringMatch(file);
if (match != null) matches.add(match);
}
if (matches.isNotEmpty) return matches;
}
return [];
}