Skip to content

Commit

Permalink
Merge pull request #1039 from hallelujah-shih/feature/set-default-lang
Browse files Browse the repository at this point in the history
Feature/set default lang
  • Loading branch information
eugeis authored Oct 11, 2024
2 parents 34f804b + 9f94cfb commit 7275dfb
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
14 changes: 11 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ func Cli(version string) (err error) {
if !currentFlags.YouTubeComments || currentFlags.YouTubeTranscript {
var transcript string
var language = "en"
if currentFlags.Language != "" {
language = currentFlags.Language
if currentFlags.Language != "" || fabric.DefaultLanguage.Value != "" {
if currentFlags.Language != "" {
language = currentFlags.Language
} else {
language = fabric.DefaultLanguage.Value
}
}
if transcript, err = fabric.YouTube.GrabTranscript(videoId, language); err != nil {
return
Expand Down Expand Up @@ -208,7 +212,11 @@ func Cli(version string) (err error) {
}

var session *db.Session
if session, err = chatter.Send(currentFlags.BuildChatRequest(strings.Join(os.Args[1:], " ")), currentFlags.BuildChatOptions()); err != nil {
chatReq := currentFlags.BuildChatRequest(strings.Join(os.Args[1:], " "))
if chatReq.Language == "" {
chatReq.Language = fabric.DefaultLanguage.Value
}
if session, err = chatter.Send(chatReq, currentFlags.BuildChatOptions()); err != nil {
return
}

Expand Down
14 changes: 12 additions & 2 deletions core/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ package core
import (
"bytes"
"fmt"
"os"
"strconv"

"github.com/atotto/clipboard"
"github.com/danielmiessler/fabric/common"
"github.com/danielmiessler/fabric/db"
"github.com/danielmiessler/fabric/jina"
"github.com/danielmiessler/fabric/lang"
"github.com/danielmiessler/fabric/vendors/anthropic"
"github.com/danielmiessler/fabric/vendors/azure"
"github.com/danielmiessler/fabric/vendors/dryrun"
Expand All @@ -19,8 +23,6 @@ import (
"github.com/danielmiessler/fabric/vendors/siliconcloud"
"github.com/danielmiessler/fabric/youtube"
"github.com/pkg/errors"
"os"
"strconv"
)

const DefaultPatternsGitRepoUrl = "https://github.com/danielmiessler/fabric.git"
Expand Down Expand Up @@ -49,6 +51,7 @@ func NewFabricBase(db *db.Db) (ret *Fabric) {
VendorsAll: NewVendorsManager(),
PatternsLoader: NewPatternsLoader(db.Patterns),
YouTube: youtube.NewYouTube(),
Language: lang.NewLanguage(),
Jina: jina.NewClient(),
}

Expand All @@ -75,6 +78,7 @@ type Fabric struct {
VendorsAll *VendorsManager
*PatternsLoader
*youtube.YouTube
*lang.Language
Jina *jina.Client

Db *db.Db
Expand All @@ -101,6 +105,7 @@ func (o *Fabric) SaveEnvFile() (err error) {

o.YouTube.SetupFillEnvFileContent(&envFileContent)
o.Jina.SetupFillEnvFileContent(&envFileContent)
o.Language.SetupFillEnvFileContent(&envFileContent)

err = o.Db.SaveEnv(envFileContent.String())
return
Expand All @@ -125,6 +130,10 @@ func (o *Fabric) Setup() (err error) {
return
}

if err = o.Language.SetupOrSkip(); err != nil {
return
}

err = o.SaveEnvFile()

return
Expand Down Expand Up @@ -200,6 +209,7 @@ func (o *Fabric) configure() (err error) {
//YouTube and Jina are not mandatory, so ignore not configured error
_ = o.YouTube.Configure()
_ = o.Jina.Configure()
_ = o.Language.Configure()

return
}
Expand Down
41 changes: 41 additions & 0 deletions lang/language.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package lang

import (
"github.com/danielmiessler/fabric/common"
"golang.org/x/text/language"
)

func NewLanguage() (ret *Language) {

label := "Language"
ret = &Language{}

ret.Configurable = &common.Configurable{
Label: label,
EnvNamePrefix: common.BuildEnvVariablePrefix(label),
ConfigureCustom: ret.configure,
}

ret.DefaultLanguage = ret.Configurable.AddSetupQuestionCustom("Output", false,
"Enter your default want output lang (for example: zh_CN)")

return
}

type Language struct {
*common.Configurable
DefaultLanguage *common.SetupQuestion
}

func (o *Language) configure() error {
if o.DefaultLanguage.Value != "" {
langTag, err := language.Parse(o.DefaultLanguage.Value)
if err == nil {
o.DefaultLanguage.Value = langTag.String()
} else {
o.DefaultLanguage.Value = ""
}
}

return nil
}

0 comments on commit 7275dfb

Please sign in to comment.