added lang "auto-detect" instead of random lang
This commit is contained in:
parent
9b5a40632a
commit
82d5a83911
2 changed files with 76 additions and 10 deletions
18
main.go
18
main.go
|
@ -17,7 +17,7 @@ type LanguageOption struct {
|
||||||
var settings UserSettings
|
var settings UserSettings
|
||||||
|
|
||||||
var languageOptions = []LanguageOption{
|
var languageOptions = []LanguageOption{
|
||||||
{Code: "", Name: "Any Language"},
|
{Code: "", Name: "Auto-detect"},
|
||||||
{Code: "en", Name: "English"},
|
{Code: "en", Name: "English"},
|
||||||
{Code: "af", Name: "Afrikaans"},
|
{Code: "af", Name: "Afrikaans"},
|
||||||
{Code: "ar", Name: "العربية (Arabic)"},
|
{Code: "ar", Name: "العربية (Arabic)"},
|
||||||
|
@ -70,25 +70,31 @@ func handleSearch(w http.ResponseWriter, r *http.Request) {
|
||||||
query, safe, lang, searchType, page := parseSearchParams(r)
|
query, safe, lang, searchType, page := parseSearchParams(r)
|
||||||
|
|
||||||
// Load user settings
|
// Load user settings
|
||||||
settings = loadUserSettings(r)
|
settings = loadUserSettings(w, r)
|
||||||
|
|
||||||
// Update the theme, safe search, and language based on query parameters or use existing settings
|
// Update theme if provided, or use existing settings
|
||||||
theme := r.URL.Query().Get("theme")
|
theme := r.URL.Query().Get("theme")
|
||||||
if theme != "" {
|
if theme != "" {
|
||||||
settings.Theme = theme
|
settings.Theme = theme
|
||||||
saveUserSettings(w, settings)
|
saveUserSettings(w, settings) // Save if theme is updated
|
||||||
} else if settings.Theme == "" {
|
} else if settings.Theme == "" {
|
||||||
settings.Theme = "dark" // Default theme
|
settings.Theme = "dark" // Default theme
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update safe search if provided, or use existing settings
|
||||||
if safe != "" && safe != settings.SafeSearch {
|
if safe != "" && safe != settings.SafeSearch {
|
||||||
settings.SafeSearch = safe
|
settings.SafeSearch = safe
|
||||||
saveUserSettings(w, settings)
|
saveUserSettings(w, settings) // Save if safe search is updated
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update language if provided, or use existing settings
|
||||||
if lang != "" && lang != settings.Language {
|
if lang != "" && lang != settings.Language {
|
||||||
settings.Language = lang
|
settings.Language = lang
|
||||||
saveUserSettings(w, settings)
|
saveUserSettings(w, settings) // Save if language is updated
|
||||||
|
} else if settings.Language == "" {
|
||||||
|
// If no language set, auto-detect from browser or default to "en"
|
||||||
|
settings.Language = normalizeLangCode(r.Header.Get("Accept-Language"))
|
||||||
|
saveUserSettings(w, settings) // Save if language is auto-detected
|
||||||
}
|
}
|
||||||
|
|
||||||
// This will do for now (to handle Dark Reader addon)
|
// This will do for now (to handle Dark Reader addon)
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,21 +14,35 @@ type UserSettings struct {
|
||||||
IsThemeDark bool
|
IsThemeDark bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadUserSettings(r *http.Request) UserSettings {
|
func loadUserSettings(w http.ResponseWriter, r *http.Request) UserSettings {
|
||||||
var settings UserSettings
|
var settings UserSettings
|
||||||
|
saveRequired := false // Track if we need to save settings back
|
||||||
|
|
||||||
// Load theme
|
// Load theme
|
||||||
if cookie, err := r.Cookie("theme"); err == nil {
|
if cookie, err := r.Cookie("theme"); err == nil {
|
||||||
settings.Theme = cookie.Value
|
settings.Theme = cookie.Value
|
||||||
} else {
|
} else {
|
||||||
settings.Theme = "dark" // Default theme
|
settings.Theme = "dark" // Default theme
|
||||||
|
saveRequired = true // No cookie found, need to save this later
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load language
|
// Load language
|
||||||
if cookie, err := r.Cookie("language"); err == nil {
|
if cookie, err := r.Cookie("language"); err == nil {
|
||||||
settings.Language = cookie.Value
|
settings.Language = cookie.Value
|
||||||
} else {
|
} else {
|
||||||
settings.Language = "en" // Default language
|
settings.Language = "" // Set language to empty, handled later
|
||||||
|
}
|
||||||
|
|
||||||
|
// If language is empty, get it from the Accept-Language header
|
||||||
|
if settings.Language == "" {
|
||||||
|
acceptLang := r.Header.Get("Accept-Language")
|
||||||
|
if acceptLang != "" {
|
||||||
|
// Get the first language from Accept-Language header and normalize
|
||||||
|
settings.Language = normalizeLangCode(strings.Split(acceptLang, ",")[0])
|
||||||
|
} else {
|
||||||
|
settings.Language = "en" // Default language if Accept-Language is not present
|
||||||
|
}
|
||||||
|
saveRequired = true // No language cookie found, need to save
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load safe search
|
// Load safe search
|
||||||
|
@ -35,6 +50,12 @@ func loadUserSettings(r *http.Request) UserSettings {
|
||||||
settings.SafeSearch = cookie.Value
|
settings.SafeSearch = cookie.Value
|
||||||
} else {
|
} else {
|
||||||
settings.SafeSearch = "" // Default safe search off
|
settings.SafeSearch = "" // Default safe search off
|
||||||
|
saveRequired = true // No safe search cookie found, need to save
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save settings if required (no cookie found for any of the settings)
|
||||||
|
if saveRequired {
|
||||||
|
saveUserSettings(w, settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
return settings
|
return settings
|
||||||
|
@ -74,7 +95,7 @@ func saveUserSettings(w http.ResponseWriter, settings UserSettings) {
|
||||||
func handleSaveSettings(w http.ResponseWriter, r *http.Request) {
|
func handleSaveSettings(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "POST" {
|
if r.Method == "POST" {
|
||||||
// Load current settings
|
// Load current settings
|
||||||
settings := loadUserSettings(r)
|
settings := loadUserSettings(w, r)
|
||||||
|
|
||||||
// Update only the settings that were submitted in the form
|
// Update only the settings that were submitted in the form
|
||||||
if theme := r.FormValue("theme"); theme != "" {
|
if theme := r.FormValue("theme"); theme != "" {
|
||||||
|
@ -82,6 +103,12 @@ func handleSaveSettings(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
if lang := r.FormValue("lang"); lang != "" {
|
if lang := r.FormValue("lang"); lang != "" {
|
||||||
settings.Language = lang
|
settings.Language = lang
|
||||||
|
} else {
|
||||||
|
// If lang is empty, try to get from Accept-Language header
|
||||||
|
acceptLang := r.Header.Get("Accept-Language")
|
||||||
|
if acceptLang != "" {
|
||||||
|
settings.Language = strings.Split(acceptLang, ",")[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if safe := r.FormValue("safe"); safe != "" {
|
if safe := r.FormValue("safe"); safe != "" {
|
||||||
settings.SafeSearch = safe
|
settings.SafeSearch = safe
|
||||||
|
@ -97,7 +124,7 @@ func handleSaveSettings(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
func handleSettings(w http.ResponseWriter, r *http.Request) {
|
func handleSettings(w http.ResponseWriter, r *http.Request) {
|
||||||
// Load user settings
|
// Load user settings
|
||||||
settings = loadUserSettings(r)
|
settings = loadUserSettings(w, r)
|
||||||
|
|
||||||
data := struct {
|
data := struct {
|
||||||
LanguageOptions []LanguageOption
|
LanguageOptions []LanguageOption
|
||||||
|
@ -129,3 +156,36 @@ func handleSettings(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper function to normalize language codes
|
||||||
|
func normalizeLangCode(lang string) string {
|
||||||
|
lang = strings.ToLower(lang)
|
||||||
|
|
||||||
|
// First, check if the language code is already valid
|
||||||
|
if isValidLangCode(lang) {
|
||||||
|
return lang
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip regional codes (e.g., en-US -> en)
|
||||||
|
if strings.Contains(lang, "-") {
|
||||||
|
lang = strings.Split(lang, "-")[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-check if the normalized version is valid
|
||||||
|
if isValidLangCode(lang) {
|
||||||
|
return lang
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the language is not recognized, default to "en"
|
||||||
|
return "en"
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper function to check if a language code exists in the language options
|
||||||
|
func isValidLangCode(lang string) bool {
|
||||||
|
for _, opt := range languageOptions {
|
||||||
|
if opt.Code == lang {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue