summaryrefslogtreecommitdiff
path: root/locale/localization.go
blob: d5c6b481f8a7f7adacbd83ce652961452c720429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package locale

import (
	"os"

	"github.com/go-playground/validator/v10"
	"gopkg.in/yaml.v3"
)

type LocaleConfig struct {
	TagsLabel  string           `yaml:"TagsLabel" json:"TagsLabel"`
	BlogSearch BlogSearchConfig `yaml:"BlogSearch" json:"BlogSearch"`
}

type BlogSearchConfig struct {
	Header                 string `yaml:"Header" json:"Header"`
	TagsHeader             string `yaml:"TagsHeader" json:"TagsHeader"`
	OrderByHeader          string `yaml:"OrderByHeader" json:"OrderByHeader"`
	TitleOrdered           string `yaml:"TitleOrdered" json:"TitleOrdered"`
	ActionDateOrdered      string `yaml:"ActionDateOrdered" json:"ActionDateOrdered"`
	PublicationDateOrdered string `yaml:"PublicationDateOrdered" json:"PublicationDateOrdered"`
	ChooseAllTags          string `yaml:"ChooseAllTags" json:"ChooseAllTags"`
}

func LoadConfig(path string, config *LocaleConfig) error {
	fileBytes, err := os.ReadFile(path)
	if err != nil {
		return err
	}

	if err = yaml.Unmarshal(fileBytes, config); err != nil {
		return err
	}

	return nil
}

func InitConfig(path string) (*LocaleConfig, error) {
	config := &LocaleConfig{}
	if err := LoadConfig(path, config); err != nil {
		return nil, err
	}

	validate := validator.New(validator.WithRequiredStructEnabled())
	if err := validate.Struct(config); err != nil {
		return nil, err
	}

	return config, nil
}