Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Settings] Sort Languages ComboBox #35462

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions src/settings-ui/Settings.UI/ViewModels/GeneralViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text.Json;
Expand Down Expand Up @@ -1082,25 +1081,50 @@ public void RefreshUpdatingState()
private void InitializeLanguages()
{
var lang = LanguageModel.LoadSetting();
int i = 0;
var selectedLanguageIndex = 0;

foreach (var item in langTagsAndIds)
{
Languages.Add(new LanguageModel { Tag = item.Key, ResourceID = item.Value, Language = GetResourceString(item.Value) });
var language = new LanguageModel { Tag = item.Key, ResourceID = item.Value, Language = GetResourceString(item.Value) };
var index = GetLanguageIndex(language.Language, item.Key == string.Empty);
Languages.Insert(index, language);

if (item.Key.Equals(lang, StringComparison.Ordinal))
{
_initLanguagesIndex = i;
LanguagesIndex = i;
selectedLanguageIndex = index;
}
else if (index <= selectedLanguageIndex)
{
selectedLanguageIndex++;
}
}

i++;
_initLanguagesIndex = selectedLanguageIndex;
LanguagesIndex = selectedLanguageIndex;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1309:Use ordinal string comparison", Justification = "Building a user facing list")]
private int GetLanguageIndex(string language, bool isDefault)
{
if (Languages.Count == 0 || isDefault)
{
return 0;
}

for (var i = 1; i < Languages.Count; i++)
{
if (string.Compare(Languages[i].Language, language, StringComparison.CurrentCultureIgnoreCase) > 0)
{
return i;
}
}

return Languages.Count;
}

private void NotifyLanguageChanged()
{
OutGoingLanguageSettings outsettings = new OutGoingLanguageSettings(langTagsAndIds.ElementAt(LanguagesIndex).Key);
OutGoingLanguageSettings outsettings = new OutGoingLanguageSettings(Languages[_languagesIndex].Tag);

SendConfigMSG(outsettings.ToString());
}
Expand Down
Loading