Skip to content

Commit

Permalink
[add] Locales & Themes
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Sep 26, 2023
1 parent 70f6931 commit 5d18028
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 28 deletions.
16 changes: 7 additions & 9 deletions sui/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@ package core

// ITemplate is the interface for the ITemplate
type ITemplate interface {
Get() error
Save() error

Pages() ([]IPage, error)
Blocks() ([]IBlock, error)
Components() ([]IComponent, error)

Page(route string) (IPage, error)

Blocks() ([]IBlock, error)
Block(name string) (IBlock, error)

Components() ([]IComponent, error)
Component(name string) (IComponent, error)

Styles() []string
Locales() []string
Themes() []string
Assets() []string
Locales() []SelectOption
Themes() []SelectOption
}

// IPage is the interface for the page
Expand Down
23 changes: 18 additions & 5 deletions sui/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,24 @@ type Block struct {

// Template is the struct for the template
type Template struct {
Version int `json:"version"` // Yao Builder version
ID string `json:"id"`
Name string `json:"name"`
Descrption string `json:"description"`
Screenshots []string `json:"screenshots"`
Version int `json:"version"` // Yao Builder version
ID string `json:"id"`
Name string `json:"name"`
Descrption string `json:"description"`
Screenshots []string `json:"screenshots"`
Themes []SelectOption `json:"themes"`
}

// Theme is the struct for the theme
type Theme struct {
ID string `json:"id"`
Name string `json:"name,omitempty"`
}

// SelectOption is the struct for the select option
type SelectOption struct {
Label string `json:"label"`
Value string `json:"value"`
}

// SourceCodes is the struct for the page codes
Expand Down
1 change: 1 addition & 0 deletions sui/storages/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (local *Local) getTemplate(id string, path string) (*Template, error) {
Name: strings.ToUpper(id),
Version: 1,
Screenshots: []string{},
Themes: []core.SelectOption{},
}}

// load the template.json
Expand Down
46 changes: 32 additions & 14 deletions sui/storages/local/template.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
package local

// Get get the template
func (tmpl *Template) Get() error {
return nil
}
import (
"path/filepath"

// Save save the template
func (tmpl *Template) Save() error {
return nil
}
"github.com/yaoapp/yao/sui/core"
"golang.org/x/text/language"
)

// Styles get the global styles
func (tmpl *Template) Styles() []string {
// Assets get the assets treelist
func (tmpl *Template) Assets() []string {
return nil
}

// Locales get the global locales
func (tmpl *Template) Locales() []string {
return nil
func (tmpl *Template) Locales() []core.SelectOption {

supportLocales := []core.SelectOption{}
path := filepath.Join(tmpl.Root, "__locales")
if !tmpl.local.fs.IsDir(path) {
return nil
}

dirs, err := tmpl.local.fs.ReadDir(path, false)
if err != nil {
return nil
}

for _, dir := range dirs {
locale := filepath.Base(dir)
label := language.Make(locale).String()
supportLocales = append(supportLocales, core.SelectOption{
Value: locale,
Label: label,
})
}

return supportLocales
}

// Themes get the global themes
func (tmpl *Template) Themes() []string {
return nil
func (tmpl *Template) Themes() []core.SelectOption {
return tmpl.Template.Themes
}
50 changes: 50 additions & 0 deletions sui/storages/local/template_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,51 @@
package local

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestTemplateThemes(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("tech-blue")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

themes := tmpl.Themes()
if len(themes) < 2 {
t.Fatalf("Themes error: %v", len(themes))
}

assert.Equal(t, "dark", themes[0].Value)
assert.Equal(t, "暗色主题", themes[0].Label)
assert.Equal(t, "light", themes[1].Value)
assert.Equal(t, "明亮主题", themes[1].Label)
}

func TestTemplateLocales(t *testing.T) {
tests := prepare(t)
defer clean()

tmpl, err := tests.Demo.GetTemplate("tech-blue")
if err != nil {
t.Fatalf("GetTemplate error: %v", err)
}

locales := tmpl.Locales()
if len(locales) < 3 {
t.Fatalf("Locales error: %v", len(locales))
}

assert.Equal(t, "ar", locales[0].Label)
assert.Equal(t, "ar", locales[0].Value)

assert.Equal(t, "zh-CN", locales[1].Label)
assert.Equal(t, "zh-cn", locales[1].Value)

assert.Equal(t, "zh-TW", locales[2].Label)
assert.Equal(t, "zh-tw", locales[2].Value)
}

0 comments on commit 5d18028

Please sign in to comment.