This is my new blog CMS https://jlelse.blog
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.0 KiB

3 weeks ago
  1. package main
  2. import (
  3. "os"
  4. "path"
  5. "gopkg.in/yaml.v3"
  6. )
  7. const stringsDir = "templates/strings"
  8. const defaultStrings = "default"
  9. const variantFileExt = ".yaml"
  10. var templateStrings map[string]map[string]string
  11. func initTemplateStrings() error {
  12. templateStrings = map[string]map[string]string{}
  13. variants := []string{defaultStrings}
  14. for _, blog := range appConfig.Blogs {
  15. variants = append(variants, blog.Lang)
  16. }
  17. for _, variant := range variants {
  18. variantStrings := map[string]string{}
  19. fileContent, err := os.ReadFile(path.Join(stringsDir, variant+variantFileExt))
  20. if err != nil {
  21. if os.IsNotExist(err) {
  22. continue
  23. }
  24. return err
  25. }
  26. err = yaml.Unmarshal(fileContent, variantStrings)
  27. if err != nil {
  28. return err
  29. }
  30. templateStrings[variant] = variantStrings
  31. }
  32. return nil
  33. }
  34. func getTemplateStringVariant(lang, name string) (result string) {
  35. m, ok := templateStrings[lang]
  36. if !ok {
  37. m = templateStrings[defaultStrings]
  38. }
  39. result, ok = m[name]
  40. if !ok {
  41. result = templateStrings[defaultStrings][name]
  42. }
  43. return
  44. }