Skip to content

Commit

Permalink
Introduce dictionary [fromLang, toLang] pairs in config for templating.
Browse files Browse the repository at this point in the history
  • Loading branch information
knadh committed Jun 3, 2023
1 parent ec57672 commit 92a2eb0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
47 changes: 46 additions & 1 deletion cmd/dictpress/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func initLangs(ko *koanf.Koanf) data.LangMap {

// Language configuration.
for _, l := range ko.MapKeys("lang") {
lang := data.Lang{Types: make(map[string]string)}
lang := data.Lang{ID: l, Types: make(map[string]string)}
if err := ko.UnmarshalWithConf("lang."+l, &lang, koanf.UnmarshalConf{Tag: "json"}); err != nil {
lo.Fatalf("error loading languages: %v", err)
}
Expand All @@ -213,6 +213,51 @@ func initLangs(ko *koanf.Koanf) data.LangMap {
out[l] = lang
}

if len(out) == 0 {
lo.Fatal("0 languages defined in config")
}

return out
}

// initDicts loads language->language dictionary map.
func initDicts(langs data.LangMap, ko *koanf.Koanf) data.Dicts {
var (
out = make(data.Dicts, 0)
dicts [][]string
)

if err := ko.Unmarshal("app.dicts", &dicts); err != nil {
lo.Fatalf("error unmarshalling app.dict in config: %v", err)
}

// Language configuration.
for _, pair := range dicts {
if len(pair) != 2 {
lo.Fatalf("app.dicts should have language pairs: %v", pair)
}

var (
fromID = pair[0]
toID = pair[1]
)
from, ok := langs[fromID]
if !ok {
lo.Fatalf("unknown language '%s' defined in app.dicts config", fromID)
}

to, ok := langs[toID]
if !ok {
lo.Fatalf("unknown language '%s' defined in app.dicts config", toID)
}

out = append(out, [2]data.Lang{from, to})
}

if len(out) == 0 {
lo.Fatal("0 dicts defined in config")
}

return out
}

Expand Down
11 changes: 5 additions & 6 deletions cmd/dictpress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,10 @@ func main() {
}

// Load language config.
langs := initLangs(ko)
if len(langs) == 0 {
lo.Fatal("0 languages in config")
}

var (
langs = initLangs(ko)
dicts = initDicts(langs, ko)
)
// Run the CSV importer.
if fPath := ko.String("import"); fPath != "" {
imp := importer.New(langs, q.InsertSubmissionEntry, q.InsertSubmissionRelation, db, lo)
Expand All @@ -172,7 +171,7 @@ func main() {
os.Exit(0)
}

app.data = data.New(&q, langs)
app.data = data.New(&q, langs, dicts)
app.queries = &q

// Result paginators.
Expand Down
2 changes: 2 additions & 0 deletions cmd/dictpress/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type tplData struct {
RootURL string
EnableSubmissions bool
Langs data.LangMap
Dicts data.Dicts

Path string
Data interface{}
Expand Down Expand Up @@ -220,6 +221,7 @@ func (t *tplRenderer) Render(w io.Writer, name string, data interface{}, c echo.
RootURL: app.constants.RootURL,
EnableSubmissions: app.constants.EnableSubmissions,
Langs: app.data.Langs,
Dicts: app.data.Dicts,
Data: data,
})
}
3 changes: 3 additions & 0 deletions config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ enable_pages = true
# on approval.
enable_submissions = false

# Available dictionary pairs. [$FromLangName, $ToLangName] pairs from the languages defined below in [lang.*] keys.
dicts = [["english", "italian"], ["italian", "english"]]


[results]
# Default number of entries to return per page when paginated.
Expand Down
8 changes: 7 additions & 1 deletion internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (

// Lang represents a language's configuration.
type Lang struct {
ID string `json:"id"`
Name string `json:"name"`
Types map[string]string `json:"types"`
TokenizerName string `json:"tokenizer"`
Expand All @@ -28,6 +29,9 @@ type Lang struct {
// LangMap represents a map of language controllers indexed by the language key.
type LangMap map[string]Lang

// Dicts represents dictionaries, where each dictionary is a pair of languages.
type Dicts [][2]Lang

// Tokenizer represents a function that takes a string
// and returns a list of Postgres tsvector tokens.
type Tokenizer interface {
Expand Down Expand Up @@ -77,6 +81,7 @@ type Queries struct {
type Data struct {
queries *Queries
Langs LangMap
Dicts Dicts
}

// Query represents the parameters of a single search query.
Expand All @@ -92,10 +97,11 @@ type Query struct {
}

// New returns an instance of the search interface.
func New(q *Queries, langs LangMap) *Data {
func New(q *Queries, langs LangMap, dicts Dicts) *Data {
return &Data{
queries: q,
Langs: langs,
Dicts: dicts,
}
}

Expand Down

0 comments on commit 92a2eb0

Please sign in to comment.