language.go 906 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package mapper
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models"
  5. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/services/i18n"
  6. )
  7. func ToLanguage(lang models.Language) i18n.Language {
  8. switch lang {
  9. case models.LangRu:
  10. return i18n.Ru
  11. case models.LangEn:
  12. return i18n.En
  13. default:
  14. return ""
  15. }
  16. }
  17. func ToLanguageModel(lang i18n.Language) models.Language {
  18. switch lang {
  19. case i18n.Ru:
  20. return models.LangRu
  21. case i18n.En:
  22. return models.LangEn
  23. default:
  24. return 0
  25. }
  26. }
  27. func ToLanguageFromString(lang string) i18n.Language {
  28. switch lang {
  29. case "en", "EN":
  30. return i18n.En
  31. case "ru", "RU":
  32. return i18n.Ru
  33. default:
  34. return ""
  35. }
  36. }
  37. func LanguageFromContext(c *fiber.Ctx) i18n.Language {
  38. language := ToLanguageFromString(c.Params(i18n.LangParam))
  39. if len(language) == 0 {
  40. language = i18n.Default
  41. }
  42. return language
  43. }