localization.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package example_basics
  2. // @snippet_begin(L10nFullExample)
  3. import (
  4. "fmt"
  5. "net/http"
  6. "strings"
  7. "github.com/qor5/admin/l10n"
  8. l10n_view "github.com/qor5/admin/l10n/views"
  9. "github.com/qor5/admin/presets"
  10. "github.com/qor5/admin/presets/gorm2op"
  11. "gorm.io/gorm"
  12. )
  13. // @snippet_begin(L10nModelExample)
  14. type L10nModel struct {
  15. gorm.Model
  16. Title string
  17. l10n.Locale
  18. }
  19. func (lm *L10nModel) PrimarySlug() string {
  20. return fmt.Sprintf("%v_%v", lm.ID, lm.LocaleCode)
  21. }
  22. func (lm *L10nModel) PrimaryColumnValuesBySlug(slug string) map[string]string {
  23. segs := strings.Split(slug, "_")
  24. if len(segs) != 2 {
  25. panic("wrong slug")
  26. }
  27. return map[string]string{
  28. "id": segs[0],
  29. "locale_code": segs[1],
  30. }
  31. }
  32. // @snippet_end
  33. func LocalizationExampleMock(b *presets.Builder) {
  34. if err := DB.AutoMigrate(&L10nModel{}); err != nil {
  35. panic(err)
  36. }
  37. b.URIPrefix(LocalizationExamplePath).
  38. DataOperator(gorm2op.DataOperator(DB))
  39. // @snippet_begin(L10nBuilderExample)
  40. l10nBuilder := l10n.New()
  41. l10nBuilder.
  42. RegisterLocales("International", "international", "International").
  43. RegisterLocales("China", "cn", "China").
  44. RegisterLocales("Japan", "jp", "Japan").
  45. GetSupportLocaleCodesFromRequestFunc(func(R *http.Request) []string {
  46. return l10nBuilder.GetSupportLocaleCodes()[:]
  47. })
  48. // @snippet_end
  49. // @snippet_begin(L10nConfigureExample)
  50. mb := b.Model(&L10nModel{}).URIName("l10n-models")
  51. l10n_view.Configure(b, DB, l10nBuilder, nil, mb)
  52. mb.Listing("ID", "Title", "Locale")
  53. // @snippet_end
  54. // @snippet_end
  55. }
  56. const LocalizationExamplePath = "/samples/l10n"