Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/set default lang #1039

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading