diff --git a/go.mod b/go.mod index a747de0..f2a2234 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/chai2010/webp v1.1.1 // indirect github.com/disintegration/imaging v1.6.2 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect + github.com/leonelquinteros/gotext v1.7.0 // indirect golang.org/x/image v0.20.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect ) diff --git a/go.sum b/go.sum index 4120d74..084267e 100644 --- a/go.sum +++ b/go.sum @@ -25,6 +25,8 @@ github.com/gobwas/ws v1.3.2/go.mod h1:hRKAFb8wOxFROYNsT1bqfWnhX+b5MFeJM9r2ZSwg/K github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs= +github.com/leonelquinteros/gotext v1.7.0 h1:jcJmF4AXqyamP7vuw2MMIKs+O3jAEmvrc5JQiI8Ht/8= +github.com/leonelquinteros/gotext v1.7.0/go.mod h1:qJdoQuERPpccw7L70uoU+K/BvTfRBHYsisCQyFLXyvw= github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0= github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0= diff --git a/lang.go b/lang.go new file mode 100644 index 0000000..7b3e71f --- /dev/null +++ b/lang.go @@ -0,0 +1,32 @@ +package main + +import ( + "os" + "path/filepath" + + "github.com/leonelquinteros/gotext" +) + +var translations *gotext.Locale + +// InitializeLanguage loads the language strings from .po files based on the selected language. +func InitializeLanguage(siteLanguage string) { + translationsDir := "lang" + localeDir := filepath.Join(translationsDir, siteLanguage, "LC_MESSAGES") + if _, err := os.Stat(localeDir); os.IsNotExist(err) { + printWarn("Translation directory for language '%s' does not exist. Using default.", siteLanguage) + siteLanguage = "en" // Use default language if not found + localeDir = filepath.Join(translationsDir, siteLanguage, "LC_MESSAGES") + } + + translations = gotext.NewLocale(translationsDir, siteLanguage) + translations.AddDomain("default") +} + +// Translate wraps gotext.Get and returns a translated string based on the given key. +func Translate(key string, params ...interface{}) string { + if translations == nil { + return key // Fallback if translations are not initialized + } + return translations.Get(key, params...) +} diff --git a/lang/en/LC_MESSAGES/default.po b/lang/en/LC_MESSAGES/default.po new file mode 100644 index 0000000..d37eb73 --- /dev/null +++ b/lang/en/LC_MESSAGES/default.po @@ -0,0 +1,45 @@ + +msgid "settings_title" +msgstr "Settings" + +msgid "settings" +msgstr "Settings" + +msgid "theme" +msgstr "Theme" + +msgid "theme_dark" +msgstr "Dark" + +msgid "theme_light" +msgstr "Light" + +msgid "theme_night" +msgstr "Night" + +msgid "theme_black" +msgstr "Black" + +msgid "theme_mocha" +msgstr "Mocha" + +msgid "theme_latte" +msgstr "Latte" + +msgid "safe_search" +msgstr "Safe Search" + +msgid "safe_search_off" +msgstr "Off" + +msgid "safe_search_on" +msgstr "On" + +msgid "site_language" +msgstr "Site Language" + +msgid "search_language" +msgstr "Search Language" + +msgid "save_settings" +msgstr "Save Settings" diff --git a/lang/pl/LC_MESSAGES/default.po b/lang/pl/LC_MESSAGES/default.po new file mode 100644 index 0000000..4acac06 --- /dev/null +++ b/lang/pl/LC_MESSAGES/default.po @@ -0,0 +1,45 @@ + +msgid "settings_title" +msgstr "Ustawienia" + +msgid "settings" +msgstr "Ustawienia" + +msgid "theme" +msgstr "Motyw" + +msgid "theme_dark" +msgstr "Ciemny" + +msgid "theme_light" +msgstr "Jasny" + +msgid "theme_night" +msgstr "Nocny" + +msgid "theme_black" +msgstr "Czarny" + +msgid "theme_mocha" +msgstr "Mocha" + +msgid "theme_latte" +msgstr "Latte" + +msgid "safe_search" +msgstr "Bezpieczne wyszukiwanie" + +msgid "safe_search_off" +msgstr "Wyłączone" + +msgid "safe_search_on" +msgstr "Włączone" + +msgid "site_language" +msgstr "Język witryny" + +msgid "search_language" +msgstr "Język wyszukiwania" + +msgid "save_settings" +msgstr "Zapisz ustawienia" \ No newline at end of file diff --git a/main.go b/main.go index e3b102e..91bc3f9 100755 --- a/main.go +++ b/main.go @@ -107,6 +107,8 @@ func handleSearch(w http.ResponseWriter, r *http.Request) { settings.IsThemeDark = false } + InitializeLanguage(settings.SiteLanguage) + if query == "" { data := struct { LanguageOptions []LanguageOption diff --git a/templates/settings.html b/templates/settings.html index 39e82af..5083ea2 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -1,12 +1,12 @@ - + {{ if .IsThemeDark }} {{ end }} - Settings - Ocásek + {{ translate "settings_title" }} @@ -14,67 +14,67 @@
-

Settings

+

{{ translate "settings" }}

-

Theme

+

{{ translate "theme" }}

-

Safe Search

+

{{ translate "safe_search" }}

-

Site Language

+

{{ translate "site_language" }}

{{range .LanguageOptions}} @@ -93,7 +93,7 @@

- +
diff --git a/user-settings.go b/user-settings.go index b9f5dd5..b46a6af 100755 --- a/user-settings.go +++ b/user-settings.go @@ -164,7 +164,7 @@ func handleSettings(w http.ResponseWriter, r *http.Request) { printDebug("Rendering settings with data: %+v", data) - tmpl, err := template.ParseFiles("templates/settings.html") + tmpl, err := template.New("settings.html").Funcs(template.FuncMap{"translate": Translate}).ParseFiles("templates/settings.html") if err != nil { printErr("Error parsing template: %s", err) http.Error(w, "Internal Server Error", 500)