Skip to content

armgit / nota

Public Imported from GitHub
0

Nota — a free DAW for macOS, Windows and Linux. No subscription, no account — just open it and make music.

2.6 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-only
// Copyright (c) 2026 Egor Khindikaynen (Nota). See LICENSES/ for license terms.
//
// The interface language (spec 068): which codes exist, what a stored value means, when a
// change is still waiting for a restart, and how a language counts ("1 track", "2 tracks").
// The counting rule is named by the language file itself (spec 077 research R6), so a language
// downloaded later brings its rule instead of needing one written here per code.
// Pure — no UI, no I/O — so the smoke test can walk it, like InterfaceScale. A hand-edited or
// future value never throws: it reads as English.

using System;
using System.Collections.Generic;

namespace Nota.Application.Localization;

public enum PluralForm { One, Other }

public static class UiLanguage
{
    /// <summary>English: the default, and what any missing text falls back to.</summary>
    public const string Default = "en";

    /// <summary>The stored code if it names an available language (case ignored), else
    /// <see cref="Default"/>. The settings file is not rewritten by this.</summary>
    public static string Normalize(string? code, IReadOnlyCollection<string> available)
    {
        if (string.IsNullOrWhiteSpace(code)) return Default;
        var lower = code.Trim().ToLowerInvariant();
        foreach (var a in available)
            if (string.Equals(a, lower, StringComparison.Ordinal)) return lower;
        return Default;
    }

    /// <summary>True while the stored language is not the one running: a restart is pending.
    /// Choosing the running language again clears it.</summary>
    public static bool IsPending(string? stored, string running, IReadOnlyCollection<string> available)
        => !string.Equals(Normalize(stored, available), running, StringComparison.Ordinal);

    /// <summary>English, German …: one when n is 1.</summary>
    public const string RuleOneIs1 = "one_is_1";

    /// <summary>French …: one when n is 0 or 1.</summary>
    public const string RuleOneIs0Or1 = "one_is_0_1";

    /// <summary>Whether this build implements a plural rule a language file names.</summary>
    public static bool IsKnownRule(string? rule) => rule is RuleOneIs1 or RuleOneIs0Or1;

    /// <summary>Which plural form a whole-number count takes under a language file's rule. An
    /// unknown rule counts like English (a file naming one is rejected when it is read).</summary>
    public static PluralForm Plural(string rule, long n) => rule switch
    {
        RuleOneIs0Or1 => n is 0 or 1 ? PluralForm.One : PluralForm.Other,
        _ => n == 1 ? PluralForm.One : PluralForm.Other,
    };
}