package weknora import ( "strings" "unicode" ) // fixedLocales are the response languages the platform understands end-to-end // (settings command, preference storage, and WeKnora's LanguageLocaleName map). var fixedLocales = map[string]bool{ "zh-CN": true, "en": true, "ja": true, "ko": true, "es": true, } // resolveLocale maps a user preference + query text to an Accept-Language // value understood by WeKnora's Language middleware. // // - "" -> "" (send no header; WeKnora keeps its own fallback) // - "auto" -> best-effort detection from the query script // - a known fixed locale (e.g. "en", "ja") -> passed through verbatim // // WeKnora matches the first Accept-Language tag and maps it to a human // language name for its prompts, so the returned tag must be one it knows // ("zh-CN", "en", "ja", "ko", "es", ...). An "auto" query whose script // cannot be classified returns "" so the server behaves exactly as before. func resolveLocale(locale, query string) string { locale = strings.TrimSpace(locale) if fixedLocales[locale] { return locale } if locale == "auto" { return detectLocale(query) } return "" } // detectLocale classifies a query by the dominant non-ASCII script. Latin // text maps to "en"; CJK ideographs to "zh-CN"; kana to "ja"; hangul to "ko"; // Spanish is not reliably script-detectable and is never auto-selected. // Pure punctuation/digits/empty text return "" (no guess). func detectLocale(query string) string { var cjk, kana, hangul, latin int for _, r := range query { switch { case unicode.Is(unicode.Hiragana, r) || unicode.Is(unicode.Katakana, r): kana++ case unicode.Is(unicode.Han, r): cjk++ case unicode.Is(unicode.Hangul, r): hangul++ case r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z': latin++ } } // Japanese mixes kanji in, so any kana presence wins over Han count. switch { case kana > 0: return "ja" case hangul > 0: return "ko" case cjk > 0: return "zh-CN" case latin > 0: return "en" default: return "" } }