private/Language.ps1

# FR-023: explicit language choice, independent of the OS's own $PSUICulture, persisted
# locally. Uses PowerShell's native Import-LocalizedData/.psd1 mechanism (research.md) -
# no external i18n dependency.

$script:OctaSupportedCultures = @('es-ES', 'en-US')

function Get-OctaSettingsPath {
    $folder = Join-Path $env:LOCALAPPDATA 'Octa'
    if (-not (Test-Path $folder)) {
        New-Item -ItemType Directory -Path $folder -Force | Out-Null
    }
    return Join-Path $folder 'settings.json'
}

function Get-OctaLanguage {
    $path = Get-OctaSettingsPath
    if (Test-Path $path) {
        $settings = Get-Content -Path $path -Raw | ConvertFrom-Json
        if ($settings.Culture -in $script:OctaSupportedCultures) {
            return $settings.Culture
        }
    }
    return $null
}

function Set-OctaLanguage {
    param([Parameter(Mandatory)][ValidateSet('es-ES', 'en-US')][string]$Culture)
    $path = Get-OctaSettingsPath
    [pscustomobject]@{ Culture = $Culture } | ConvertTo-Json | Set-Content -Path $path -Encoding utf8
}

function Show-OctaLanguageMenu {
    <#
        First-launch (or explicit change) language submenu. Plain, minimal prompt - reuses
        the same direct-keypress convention as the main menu (FR-018).
    #>

    Show-OctaBanner -Tagline 'Windows 11 Debloat -- Local. Transparente. Open Source. / Local. Transparent. Open Source.'
    Write-Host ''
    Write-Host 'Idioma / Language:'
    Write-Host ''
    Write-Host ' 1 Espanol'
    Write-Host ' 2 English'
    Write-Host ''
    Write-Host -NoNewline 'Selecciona idioma / Select language: '

    do {
        $key = [Console]::ReadKey($true)
        switch ($key.KeyChar) {
            '1' { Write-Host '1'; return 'es-ES' }
            '2' { Write-Host '2'; return 'en-US' }
        }
    } while ($true)
}

function Get-OctaStrings {
    <#
        Resolves the active language (persisted choice, prompting on first run) and imports
        its string table via the native Import-LocalizedData -UICulture override - never the
        OS's own $PSUICulture by default.

        In a non-interactive context (no console attached, redirected input - e.g. CI, a
        scheduled task, a remote exec without a pty) [Console]::ReadKey throws, so the
        language prompt is skipped in favor of a silent 'en-US' default for THIS call only -
        the choice is deliberately not persisted, so the next genuinely interactive run still
        prompts normally instead of being silently locked into a default the user never chose.
    #>

    [CmdletBinding()]
    param()

    $culture = Get-OctaLanguage
    if (-not $culture) {
        if (Test-OctaInteractiveConsole) {
            $culture = Show-OctaLanguageMenu
            Set-OctaLanguage -Culture $culture
        }
        else {
            $culture = 'en-US'
        }
    }

    return Import-LocalizedData -BaseDirectory $PSScriptRoot\.. -FileName 'Octa.psd1' -UICulture $culture
}