[add] Locales & Themes
This commit is contained in:
parent
70f6931eb7
commit
5d18028cfc
5 changed files with 108 additions and 28 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue