Skip to content

Commit

Permalink
set default language by enviroment variable LANG
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonTian authored and yndu13 committed Jan 31, 2024
1 parent d4bafad commit 726ccf6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
3 changes: 0 additions & 3 deletions cli/command_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ import (

func (c *Command) PrintHead(ctx *Context) {
Printf(ctx.Writer(), "%s\n", c.Short.Text())
//if c.Long != nil {
// fmt.Printf("\n%s\n", c.Long.Text())
//}
}

func (c *Command) PrintUsage(ctx *Context) {
Expand Down
2 changes: 1 addition & 1 deletion config/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func doConfigure(ctx *cli.Context, profileName string, mode string) error {

cp.Language = ReadInput(cp.Language)
if cp.Language != "zh" && cp.Language != "en" {
cp.Language = "en"
cp.Language = i18n.GetLanguage()
}

//fmt.Printf("User site: [china|international|japan] %s", cp.Site)
Expand Down
3 changes: 2 additions & 1 deletion config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/i18n"
jmespath "github.com/jmespath/go-jmespath"
)

Expand Down Expand Up @@ -80,7 +81,7 @@ func NewProfile(name string) Profile {
Name: name,
Mode: "",
OutputFormat: "json",
Language: "en",
Language: i18n.GetLanguage(),
}
}

Expand Down
16 changes: 15 additions & 1 deletion i18n/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@
// limitations under the License.
package i18n

import (
"os"
"strings"
)

type LanguageCode string

const (
Zh = LanguageCode("zh")
En = LanguageCode("en")
)

var language = "en"
func getLanguageFromEnv() string {
lang := os.Getenv("LANG")
if strings.HasPrefix(lang, "zh_CN") {
return "zh"
} else {
return "en"
}
}

var language = getLanguageFromEnv()

func SetLanguage(lang string) {
language = lang
Expand Down
12 changes: 5 additions & 7 deletions openapi/commando.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func (c *Commando) InitWithCommand(cmd *cli.Command) {
}

func (c *Commando) main(ctx *cli.Context, args []string) error {
//
// aliyun
if len(args) == 0 {
c.printUsage(ctx)
Expand Down Expand Up @@ -238,6 +237,7 @@ func (c *Commando) createInvoker(ctx *cli.Context, productCode string, apiOrMeth
}
}
}

if strings.ToLower(product.ApiStyle) == "rpc" {
//
// Rpc call
Expand All @@ -258,8 +258,8 @@ func (c *Commando) createInvoker(ctx *cli.Context, productCode string, apiOrMeth
}, nil
}
return nil, &InvalidApiError{apiOrMethod, &product}

}

//
// Restful Call
// aliyun cs GET /clusters
Expand All @@ -268,10 +268,12 @@ func (c *Commando) createInvoker(ctx *cli.Context, productCode string, apiOrMeth
if err != nil {
return nil, err
}

if !ok {
return nil, cli.NewErrorWithTip(fmt.Errorf("product '%s' need restful call", product.GetLowerCode()),
"Use `aliyun %s {GET|PUT|POST|DELETE} <path> ...`", product.GetLowerCode())
}

if api, ok := c.library.GetApi(product.Code, product.Version, ctx.Command().Name); ok {
return &RestfulInvoker{
basicInvoker,
Expand All @@ -281,14 +283,14 @@ func (c *Commando) createInvoker(ctx *cli.Context, productCode string, apiOrMeth
&api,
}, nil
}

return &RestfulInvoker{
basicInvoker,
method,
path,
force,
nil,
}, nil

} else {
if !force {
return nil, &InvalidProductError{Code: productCode, library: c.library}
Expand Down Expand Up @@ -423,9 +425,5 @@ func (c *Commando) printUsage(ctx *cli.Context) {
cmd.PrintSubCommands(ctx)
cmd.PrintFlags(ctx)
cmd.PrintSample(ctx)
//if configError != nil {
// cli.Printf("Configuration Invailed: %s\n", configError)
// cli.Printf("Run `aliyun configure` first:\n %s\n", configureCommand.Usage)
//}
cmd.PrintTail(ctx)
}
2 changes: 0 additions & 2 deletions openapi/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ import (
)

type Library struct {
lang string
builtinRepo *meta.Repository
writer io.Writer
}

func NewLibrary(w io.Writer, lang string) *Library {
return &Library{
builtinRepo: meta.LoadRepository(),
lang: lang,
writer: w,
}
}
Expand Down

0 comments on commit 726ccf6

Please sign in to comment.