Set option to modify locale used (#60)

This commit is contained in:
ByteDream 2022-11-01 22:17:19 +01:00
parent 10617df834
commit 59e8793a2f
3 changed files with 26 additions and 0 deletions

View file

@ -10,6 +10,8 @@ import (
"github.com/crunchy-labs/crunchy-cli/cli/commands/login" "github.com/crunchy-labs/crunchy-cli/cli/commands/login"
"github.com/crunchy-labs/crunchy-cli/cli/commands/update" "github.com/crunchy-labs/crunchy-cli/cli/commands/update"
"github.com/crunchy-labs/crunchy-cli/utils" "github.com/crunchy-labs/crunchy-cli/utils"
"github.com/crunchy-labs/crunchyroll-go/v3"
crunchyUtils "github.com/crunchy-labs/crunchyroll-go/v3/utils"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os" "os"
"runtime/debug" "runtime/debug"
@ -22,6 +24,8 @@ var (
proxyFlag string proxyFlag string
langFlag string
useragentFlag string useragentFlag string
) )
@ -40,6 +44,14 @@ var RootCmd = &cobra.Command{
utils.Log = commands.NewLogger(false, false, false) utils.Log = commands.NewLogger(false, false, false)
} }
if langFlag != "" {
if !crunchyUtils.ValidateLocale(crunchyroll.LOCALE(langFlag)) {
return fmt.Errorf("'%s' is not a valid language. Choose from %s", langFlag, strings.Join(utils.LocalesAsStrings(), ", "))
}
os.Setenv("CRUNCHY_LANG", langFlag)
}
utils.Log.Debug("Executing `%s` command with %d arg(s)", cmd.Name(), len(args)) utils.Log.Debug("Executing `%s` command with %d arg(s)", cmd.Name(), len(args))
utils.Client, err = utils.CreateOrDefaultClient(proxyFlag, useragentFlag) utils.Client, err = utils.CreateOrDefaultClient(proxyFlag, useragentFlag)
@ -53,6 +65,8 @@ func init() {
RootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use") RootCmd.PersistentFlags().StringVarP(&proxyFlag, "proxy", "p", "", "Proxy to use")
RootCmd.PersistentFlags().StringVar(&langFlag, "lang", "", fmt.Sprintf("Set language to use. If not set, it's received from the system locale dynamically. Choose from: %s", strings.Join(utils.LocalesAsStrings(), ", ")))
RootCmd.PersistentFlags().StringVar(&useragentFlag, "useragent", fmt.Sprintf("crunchy-cli/%s", utils.Version), "Useragent to do all request with") RootCmd.PersistentFlags().StringVar(&useragentFlag, "useragent", fmt.Sprintf("crunchy-cli/%s", utils.Version), "Useragent to do all request with")
RootCmd.AddCommand(archive.Cmd) RootCmd.AddCommand(archive.Cmd)

View file

@ -44,6 +44,14 @@ Disables all output.
Shows verbose output. Shows verbose output.
.TP .TP
\fB--lang\fR
Set language to use. If not set, it's received from the system locale dynamically. Choose from: ar-ME, ar-SA, de-DE, en-US, es-419, es-ES, es-LA, fr-FR, it-IT, ja-JP, pt-BR, pt-PT, ru-RU, zh-CN.
.TP
\fB--useragent\fR
Useragent to do all request with.
.TP
\fB--version\fR \fB--version\fR
Shows the current cli version. Shows the current cli version.

View file

@ -14,6 +14,10 @@ import (
// SystemLocale receives the system locale // SystemLocale receives the system locale
// https://stackoverflow.com/questions/51829386/golang-get-system-language/51831590#51831590 // https://stackoverflow.com/questions/51829386/golang-get-system-language/51831590#51831590
func SystemLocale(verbose bool) crunchyroll.LOCALE { func SystemLocale(verbose bool) crunchyroll.LOCALE {
if lang, ok := os.LookupEnv("CRUNCHY_LANG"); ok {
return crunchyroll.LOCALE(lang)
}
if runtime.GOOS != "windows" { if runtime.GOOS != "windows" {
if lang, ok := os.LookupEnv("LANG"); ok { if lang, ok := os.LookupEnv("LANG"); ok {
var l crunchyroll.LOCALE var l crunchyroll.LOCALE