babalae/better-genshin-impact · error · InvalidOperationException

当前UI语言为空,无法更新语言文件。

Error message

当前UI语言为空,无法更新语言文件。

What it means

Thrown by OnUpdateUiLanguageAsync when Config.OtherConfig.UiCultureInfoName is null, empty, or whitespace. The culture name is used to build the remote i18n JSON filename, so a blank value cannot map to a downloadable language file. The code treats this as a programmer/state error (InvalidOperationException) rather than prompting the user.

Source

Thrown at BetterGenshinImpact/ViewModel/Pages/CommonSettingsPageViewModel.cs:108

    public AllConfig Config { get; set; }
    public ObservableCollection<OverlayMetricSettingItem> OverlayMetricItems { get; }
    public ObservableCollection<OverlayStyleSettingGroup> OverlayStyleSettingGroups { get; }
    public ObservableCollection<string> CountryList { get; } = new();
    public ObservableCollection<string> Areas { get; } = new();

    public ObservableCollection<string> MapPathingTypes { get; } = ["SIFT", "TemplateMatch"];

    [ObservableProperty] private FrozenDictionary<string, string> _languageDict =
        new[] { "zh-Hans", "zh-Hant", "en", "ja" }
            .ToFrozenDictionary(c => c, c => CultureInfoNameToKVPConverter.GetDisplayName(c));

    [RelayCommand]
    private async Task OnUpdateUiLanguageAsync()
    {
        var cultureName = Config.OtherConfig.UiCultureInfoName ?? string.Empty;
        if (string.IsNullOrWhiteSpace(cultureName))
        {
            throw new InvalidOperationException("当前UI语言为空,无法更新语言文件。");
        }

        if (cultureName == "zh-Hans")
        {
            await ThemedMessageBox.InformationAsync("zh-Hans 无语言文件,无需更新。");
            return;
        }

        var urls = new[]
        {
            $"https://raw.githubusercontent.com/babalae/bettergi-i18n/refs/heads/main/i18n/{cultureName}.json",
            $"https://cnb.cool/bettergi/bettergi-i18n/-/git/raw/main/i18n/{cultureName}.json"
        };

        using var httpClient = new HttpClient
        {
            Timeout = TimeSpan.FromSeconds(30)
        };

View on GitHub (pinned to a7cb36712d)

Solutions

  1. Default the language dropdown to zh-Hans in the settings UI so UiCultureInfoName is never blank.
  2. Validate UiCultureInfoName before invoking the command and show ThemedMessageBox.WarningAsync instead of throwing.
  3. Ensure Config.OtherConfig initializes UiCultureInfoName to a non-empty fallback (e.g. zh-Hans).

Example fix

// before
var cultureName = Config.OtherConfig.UiCultureInfoName ?? string.Empty;
if (string.IsNullOrWhiteSpace(cultureName))
{
    throw new InvalidOperationException("当前UI语言为空,无法更新语言文件。");
}

// after
var cultureName = Config.OtherConfig.UiCultureInfoName ?? string.Empty;
if (string.IsNullOrWhiteSpace(cultureName))
{
    await ThemedMessageBox.WarningAsync("请先在设置中选择 UI 语言。");
    return;
}
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrWhiteSpace(Config.OtherConfig.UiCultureInfoName))
{
    // Do not invoke OnUpdateUiLanguage; prompt the user to pick a language first.
    return;
}

Prevention

When it happens

Trigger: Invoking the 'update language file' command while UiCultureInfoName is unset. Occurs when the config was never initialized, a config migration left it blank, or the settings dropdown was cleared and saved as empty.

Common situations: Fresh install before any language is selected; manually edited config.json with the field removed or blanked; a culture dropdown cleared by the user; config load fell back to a default that leaves the field null.

Related errors


AI-assisted analysis of babalae/better-genshin-impact@a7cb36712d (2026-08-13). Data as JSON: /api/errors/64006ebc8c120875. Report an issue: GitHub.