make disabling engines work correctly with webui and show right capitalization in webui

This commit is contained in:
Arya 2023-09-11 07:45:52 +05:30
parent 6c509efcd2
commit fc4e2d70b6
Signed by: arya
GPG Key ID: 842D12BDA50DF120
6 changed files with 42 additions and 12 deletions

View File

@ -48,7 +48,6 @@ You can find it in /api/swagger of any instance ([example](https://mozhi.aryak.m
Features of Mozhi can be customized and toggled on/off using Environment Variables. Features of Mozhi can be customized and toggled on/off using Environment Variables.
- `MOZHI_PORT`: Port the webserver listens on (if hosting API) - `MOZHI_PORT`: Port the webserver listens on (if hosting API)
- `MOZHI_USER_AGENT`: Change user agent used to make HTTP requests
- `MOZHI_LIBRETRANSLATE_URL`: URL of Libretranslate instance (Example: `MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt`) - `MOZHI_LIBRETRANSLATE_URL`: URL of Libretranslate instance (Example: `MOZHI_LIBRETRANSLATE_URL=https://lt.psf.lt`)
These envvars turn off/on engines. By default all of them are enabled. These envvars turn off/on engines. By default all of them are enabled.

View File

@ -3,7 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
libmozhi "codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
"github.com/ktr0731/go-fuzzyfinder" "github.com/ktr0731/go-fuzzyfinder"
"github.com/spf13/cobra" "github.com/spf13/cobra"

View File

@ -4,7 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
libmozhi "codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -72,7 +72,7 @@ func init() {
rootCmd.AddCommand(translateCmd) rootCmd.AddCommand(translateCmd)
translateCmd.Flags().SortFlags = false translateCmd.Flags().SortFlags = false
translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[google|libre|reverso|deepl|watson|yandex|mymemory|duckduckgo]") translateCmd.Flags().StringVarP(&engine, "engine", "e", "", "[all|google|libre|reverso|deepl|watson|yandex|mymemory|duckduckgo]")
translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language") translateCmd.Flags().StringVarP(&source, "source", "s", "", "Source language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language") translateCmd.Flags().StringVarP(&dest, "dest", "t", "", "Target language. Use langlist command to get code for your language")
translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated") translateCmd.Flags().StringVarP(&query, "query", "q", "", "Text to be translated")

View File

@ -1,7 +1,7 @@
package pages package pages
import ( import (
libmozhi "codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
"codeberg.org/aryak/mozhi/utils" "codeberg.org/aryak/mozhi/utils"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )

View File

@ -3,18 +3,48 @@ package pages
import ( import (
"codeberg.org/aryak/libmozhi" "codeberg.org/aryak/libmozhi"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"os"
) )
func envTrueNoExist(env string) bool {
if _, ok := os.LookupEnv(env); ok == false || os.Getenv(env) == "true" {
return true
}
return false
}
func engineList() map[string]string {
engines := map[string]string{"google":"Google", "deepl": "DeepL", "duckduckgo": "DuckDuckGo", "libre": "LibreTranslate", "mymemory": "MyMemory", "reverso": "Reverso", "watson": "Watson", "yandex": "Yandex"}
if envTrueNoExist("MOZHI_GOOGLE_ENABLED") == false {
delete(engines,"google")
} else if envTrueNoExist("MOZHI_DEEPL_ENABLED") == false {
delete(engines,"deepl")
} else if envTrueNoExist("MOZHI_DUCKDUCKGO_ENABLED") == false {
delete(engines,"duckduckgo")
} else if envTrueNoExist("MOZHI_LIBRETRANSLATE_ENABLED") == false || envTrueNoExist("MOZHI_LIBRETRANSLATE_URL") {
delete(engines,"libre")
} else if envTrueNoExist("MOZHI_MYMEMORY_ENABLED") == false {
delete(engines,"mymemory")
} else if envTrueNoExist("MOZHI_REVERSO_ENABLED") == false {
delete(engines,"reverso")
} else if envTrueNoExist("MOZHI_WATSON_ENABLED") == false {
delete(engines,"watson")
} else if envTrueNoExist("MOZHI_YANDEX_ENABLED") == false {
delete(engines,"yandex")
}
return engines
}
func HandleIndex(c *fiber.Ctx) error { func HandleIndex(c *fiber.Ctx) error {
engines := []string{"google", "deepl", "duckduckgo", "libretranslate", "mymemory", "reverso", "watson", "yandex"} engines := engineList()
var engine string var engine string
var originalText string var originalText string
if c.Query("engine") == "" { if c.Query("engine") == "" {
engine = "google" engine = "google"
} }
if c.Query("engine") != "" { if c.Query("engine") != "" {
for _, name := range engines { for key, _ := range engines {
if c.Query("engine") == name { if c.Query("engine") == key {
engine = c.Query("engine") engine = c.Query("engine")
} }
} }

View File

@ -2,11 +2,12 @@
<main> <main>
<!-- Need to do this custom selector thingy since <select> cant submit on click --> <!-- Need to do this custom selector thingy since <select> cant submit on click -->
<div class="custom-select"> <div class="custom-select">
Translate with: <a href="#" class="selected-option">{{.Engine}}</a> {{range $key, $value := .enginesNames}} {{ if eq $.Engine $key }}Translate
with: <a href="#" class="selected-option">{{$value}}</a> {{end}} {{end}}
<ul class="options"> <ul class="options">
{{range .enginesNames}} {{range $key, $value := .enginesNames}}
<a href="/?engine={{.}}"> <a href="/?engine={{$key}}">
<li>{{.}}</li> <li>{{$value}}</li>
</a> </a>
{{end}} {{end}}
</ul> </ul>