|
| 1 | +//! Project language resolution, shared by the CLI and Loom so both agree on |
| 2 | +//! which of StrayMark's supported locales (`en`, `es`, `zh-CN`) to display. |
| 3 | +//! |
| 4 | +//! Extracted into `straymark-core` in Loom M3: the CLI's |
| 5 | +//! `StrayMarkConfig::resolve_language` delegates here, and Loom calls |
| 6 | +//! [`resolve_language`] directly to localize its UI — one source of truth, no |
| 7 | +//! drift (the same principle as the M0 parser extraction). |
| 8 | +
|
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +use serde::Deserialize; |
| 12 | + |
| 13 | +/// The default StrayMark UI language when nothing else resolves. |
| 14 | +pub const DEFAULT_LANGUAGE: &str = "en"; |
| 15 | + |
| 16 | +/// Just the `language` key of `.straymark/config.yml`; the full config struct |
| 17 | +/// (complexity, regional scope, …) stays in the CLI. |
| 18 | +#[derive(Debug, Deserialize)] |
| 19 | +struct LanguageOnly { |
| 20 | + #[serde(default)] |
| 21 | + language: Option<String>, |
| 22 | +} |
| 23 | + |
| 24 | +/// Resolve the effective display language for a project, applying all |
| 25 | +/// fallbacks in order: |
| 26 | +/// |
| 27 | +/// 1. If `.straymark/config.yml` exists on disk, the value of its `language` |
| 28 | +/// key (defaulting to `"en"` when the field is absent or the file fails to |
| 29 | +/// parse). A configured value — even the default `"en"` — is treated as an |
| 30 | +/// explicit choice and is never overridden by env vars. |
| 31 | +/// 2. If no config file exists, parse `$LC_ALL` / `$LANG` and map it onto a |
| 32 | +/// supported locale (`en`, `es`, `zh-CN`). |
| 33 | +/// 3. Final fallback: `"en"`. |
| 34 | +pub fn resolve_language(project_root: &Path) -> String { |
| 35 | + let config_path = project_root.join(".straymark/config.yml"); |
| 36 | + if config_path.exists() { |
| 37 | + return std::fs::read_to_string(&config_path) |
| 38 | + .ok() |
| 39 | + .and_then(|contents| serde_yaml::from_str::<LanguageOnly>(&contents).ok()) |
| 40 | + .and_then(|c| c.language) |
| 41 | + .unwrap_or_else(|| DEFAULT_LANGUAGE.to_string()); |
| 42 | + } |
| 43 | + detect_os_locale().unwrap_or_else(|| DEFAULT_LANGUAGE.to_string()) |
| 44 | +} |
| 45 | + |
| 46 | +/// Read `$LC_ALL` (preferred when set) or `$LANG` and map a POSIX locale |
| 47 | +/// string like `zh_CN.UTF-8` or `es_MX` to one of the languages StrayMark |
| 48 | +/// supports (`en`, `es`, `zh-CN`). Returns `None` when no env var is set |
| 49 | +/// or when the territory points at an unsupported variant (e.g., |
| 50 | +/// Traditional Chinese in `zh_TW` / `zh_HK`). Callers fall back to `"en"`. |
| 51 | +pub fn detect_os_locale() -> Option<String> { |
| 52 | + let raw = std::env::var("LC_ALL") |
| 53 | + .ok() |
| 54 | + .filter(|v| !v.is_empty()) |
| 55 | + .or_else(|| std::env::var("LANG").ok().filter(|v| !v.is_empty()))?; |
| 56 | + parse_posix_locale(&raw) |
| 57 | +} |
| 58 | + |
| 59 | +/// Parse a POSIX locale string (e.g. `zh_CN.UTF-8`, `es`, `C`) and map it |
| 60 | +/// to a StrayMark-supported language code. Public for unit testing. |
| 61 | +pub fn parse_posix_locale(raw: &str) -> Option<String> { |
| 62 | + // Strip charset (`.UTF-8`) and modifier (`@euro`) suffixes first. |
| 63 | + let trimmed = raw.split('.').next()?.split('@').next()?; |
| 64 | + if trimmed.is_empty() { |
| 65 | + return None; |
| 66 | + } |
| 67 | + let mut parts = trimmed.splitn(2, '_'); |
| 68 | + let lang = parts.next()?; |
| 69 | + let territory = parts.next(); |
| 70 | + match (lang, territory) { |
| 71 | + ("zh", Some("CN")) | ("zh", Some("SG")) | ("zh", None) => Some("zh-CN".to_string()), |
| 72 | + // Traditional Chinese (TW / HK / MO) — StrayMark only ships zh-CN. |
| 73 | + ("zh", _) => None, |
| 74 | + ("es", _) => Some("es".to_string()), |
| 75 | + ("en", _) | ("C", _) | ("POSIX", _) => Some("en".to_string()), |
| 76 | + _ => None, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use super::*; |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn parse_posix_locale_zh_cn() { |
| 86 | + assert_eq!(parse_posix_locale("zh_CN.UTF-8"), Some("zh-CN".into())); |
| 87 | + assert_eq!(parse_posix_locale("zh_CN"), Some("zh-CN".into())); |
| 88 | + assert_eq!(parse_posix_locale("zh_SG.UTF-8"), Some("zh-CN".into())); |
| 89 | + // Bare "zh" with no territory: assume Simplified. |
| 90 | + assert_eq!(parse_posix_locale("zh"), Some("zh-CN".into())); |
| 91 | + } |
| 92 | + |
| 93 | + #[test] |
| 94 | + fn parse_posix_locale_traditional_chinese_unsupported() { |
| 95 | + assert_eq!(parse_posix_locale("zh_TW.UTF-8"), None); |
| 96 | + assert_eq!(parse_posix_locale("zh_HK.UTF-8"), None); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn parse_posix_locale_spanish_any_territory() { |
| 101 | + assert_eq!(parse_posix_locale("es_MX.UTF-8"), Some("es".into())); |
| 102 | + assert_eq!(parse_posix_locale("es_ES"), Some("es".into())); |
| 103 | + assert_eq!(parse_posix_locale("es_AR.UTF-8"), Some("es".into())); |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn parse_posix_locale_english_and_pseudo() { |
| 108 | + assert_eq!(parse_posix_locale("en_US.UTF-8"), Some("en".into())); |
| 109 | + assert_eq!(parse_posix_locale("en"), Some("en".into())); |
| 110 | + assert_eq!(parse_posix_locale("C"), Some("en".into())); |
| 111 | + assert_eq!(parse_posix_locale("POSIX"), Some("en".into())); |
| 112 | + } |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn parse_posix_locale_unsupported_returns_none() { |
| 116 | + assert_eq!(parse_posix_locale("fr_FR.UTF-8"), None); |
| 117 | + assert_eq!(parse_posix_locale("ja_JP.UTF-8"), None); |
| 118 | + assert_eq!(parse_posix_locale(""), None); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn parse_posix_locale_strips_charset_and_modifier() { |
| 123 | + assert_eq!(parse_posix_locale("es_ES@euro"), Some("es".into())); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn resolve_language_reads_config_language() { |
| 128 | + let tmp = tempfile::tempdir().unwrap(); |
| 129 | + std::fs::create_dir_all(tmp.path().join(".straymark")).unwrap(); |
| 130 | + std::fs::write( |
| 131 | + tmp.path().join(".straymark/config.yml"), |
| 132 | + "language: zh-CN\n", |
| 133 | + ) |
| 134 | + .unwrap(); |
| 135 | + assert_eq!(resolve_language(tmp.path()), "zh-CN"); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn resolve_language_config_without_language_key_defaults_en() { |
| 140 | + let tmp = tempfile::tempdir().unwrap(); |
| 141 | + std::fs::create_dir_all(tmp.path().join(".straymark")).unwrap(); |
| 142 | + std::fs::write( |
| 143 | + tmp.path().join(".straymark/config.yml"), |
| 144 | + "complexity:\n threshold: 5\n", |
| 145 | + ) |
| 146 | + .unwrap(); |
| 147 | + assert_eq!(resolve_language(tmp.path()), "en"); |
| 148 | + } |
| 149 | +} |
0 commit comments