);
+ };
- // Prevent assigning scrollRef twice to languages that are both in common list and the alphabetical list.
- if (isSelected) {
- foundSelected = true;
- }
+ const onSearch = (locale) => (
+ getLocaleNativeName(locale).toLowerCase()
+ .includes(filterLow) ||
+ getLanguageNativeName(locale).toLowerCase()
+ .includes(filterLow) ||
+ getLanguageEnglishName(locale).toLowerCase()
+ .includes(filterLow)
+ );
- return item;
- };
+ const suggestedItems = commonLocales
+ .map(findLocaleWithId)
+ .sort((a, b) => {
+ if (!a || !b) {
+ return 0;
+ }
+ // Sort alphabetically by native name but keep the default locale at the top
+ if (getLocaleId(a) === this.initiallySelectedLanguage) {
+ return -1;
+ } else if (getLocaleId(b) === this.initiallySelectedLanguage) {
+ return 1;
+ }
+ return getLocaleNativeName(a).localeCompare(getLocaleNativeName(b));
+ })
+ .filter(locale => locale && onSearch(locale))
+ .map(locale => createMenuItem(locale, "option-common"));
- // List common languages.
- if (!filter) {
+ if (suggestedItems.length > 0) {
filtered.push(