Skip to content

Commit 67a0318

Browse files
author
Enda Phelan
committed
fix(i18n): fix error where locale file not being loaded
1 parent a28550e commit 67a0318

File tree

45 files changed

+72
-153
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+72
-153
lines changed

cmd/rhoas/main.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ var (
2626
generateDocs = os.Getenv("GENERATE_DOCS") == "true"
2727
)
2828

29+
// load all locale files
30+
func initLocales() {
31+
err := localizer.IncludeAssetsAndLoadMessageFiles()
32+
if err != nil {
33+
fmt.Fprintln(os.Stderr, err)
34+
os.Exit(1)
35+
}
36+
}
37+
2938
func main() {
39+
initLocales()
40+
3041
buildVersion := build.Version
3142
cmdFactory := factory.New(build.Version)
3243
logger, err := cmdFactory.Logger()
@@ -35,9 +46,6 @@ func main() {
3546
os.Exit(1)
3647
}
3748

38-
//nolint:errcheck
39-
localizer.IncludeAssets()
40-
4149
initConfig(cmdFactory)
4250

4351
rootCmd := root.NewRootCommand(cmdFactory, buildVersion)

cmd/rhoas/pkged.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/config/file.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ import (
1111
"github.com/mitchellh/go-homedir"
1212
)
1313

14-
func init() {
15-
localizer.LoadMessageFiles("config")
16-
}
17-
1814
// NewFile creates a new config type
1915
func NewFile() IConfig {
2016
cfg := &File{}

internal/localizer/localizer.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,26 @@ type Config struct {
4343
PluralCount int
4444
}
4545

46-
// IncludeAssets walks the /internal/locales directory
46+
// IncludeAssetsAndLoadMessageFiles walks the /internal/locales directory
4747
// and allows the static assets found to be embedded into the binary
4848
// by github.com/markbates/pkger
49-
func IncludeAssets() error {
49+
// It also loads all files into memory
50+
func IncludeAssetsAndLoadMessageFiles() error {
51+
localeFileName := fmt.Sprintf("active.%v", getLangFormat())
5052
return pkger.Walk("/locales", func(path string, info os.FileInfo, err error) error {
5153
if err != nil {
5254
return err
5355
}
5456

57+
if info.IsDir() || localeFileName != info.Name() {
58+
return nil
59+
}
60+
61+
err = loadMessageFile(path)
62+
if err != nil {
63+
return err
64+
}
65+
5566
return nil
5667
})
5768
}
@@ -77,30 +88,31 @@ func MustLocalizeFromID(messageID string) string {
7788
})
7889
}
7990

80-
// LoadMessageFiles loads the message file int context
91+
// loadMessageFile loads the message file int context
8192
// Using github.com/nicksnyder/go-i18n/v2/i18n
8293
// pathTree to File is an array of the parent directories
83-
// For example: ["cmd/kafka/topic/create"] resolves to /locales/cmd/kafka/topic/create/active.en.toml
84-
func LoadMessageFiles(dirs ...string) {
85-
for _, path := range dirs {
86-
pathToFile := fmt.Sprintf("/locales/%v/active.%v", path, getLangFormat())
87-
// open the static i18n file
88-
f, err := pkger.Open(pathToFile)
89-
if err != nil {
90-
panic(err)
91-
}
92-
defer f.Close()
93-
b := bytes.NewBufferString("")
94-
// copy to contents of the file to a buffer string
95-
if _, err := io.Copy(b, f); err != nil {
96-
panic(err)
97-
}
98-
// read the contents of the file to a byte array
99-
out, _ := ioutil.ReadAll(b)
100-
// load the contents into context
101-
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
102-
bundle.MustParseMessageFileBytes(out, "en.toml")
94+
func loadMessageFile(path string) (err error) {
95+
// open the static i18n file
96+
f, err := pkger.Open(path)
97+
if err != nil {
98+
return err
10399
}
100+
defer f.Close()
101+
b := bytes.NewBufferString("")
102+
// copy to contents of the file to a buffer string
103+
if _, err = io.Copy(b, f); err != nil {
104+
panic(err)
105+
}
106+
// read the contents of the file to a byte array
107+
out, _ := ioutil.ReadAll(b)
108+
// load the contents into context
109+
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
110+
_, err = bundle.ParseMessageFileBytes(out, "en.toml")
111+
if err != nil {
112+
return err
113+
}
114+
115+
return nil
104116
}
105117

106118
// get the file extension for the current language

internal/mockutil/mockutil.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package mockutil
22

33
import (
44
"context"
5-
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
5+
"errors"
6+
7+
kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
68

79
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/config"
810
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api"
@@ -32,20 +34,20 @@ func NewConnectionMock(conn *connection.KeycloakConnection, apiClient *kasclient
3234
return &connection.ConnectionMock{
3335
RefreshTokensFunc: func(ctx context.Context) (string, string, error) {
3436
if conn.Token.AccessToken == "" && conn.Token.RefreshToken == "" {
35-
return "", "", connection.ErrNotLoggedIn
37+
return "", "", errors.New("")
3638
}
3739
if conn.Token.RefreshToken == "expired" {
38-
return "", "", connection.ErrSessionExpired
40+
return "", "", errors.New("")
3941
}
4042

4143
return "valid", "valid", nil
4244
},
4345
LogoutFunc: func(ctx context.Context) error {
4446
if conn.Token.AccessToken == "" && conn.Token.RefreshToken == "" {
45-
return connection.ErrNotLoggedIn
47+
return errors.New("")
4648
}
4749
if conn.Token.AccessToken == "expired" && conn.Token.RefreshToken == "expired" {
48-
return connection.ErrSessionExpired
50+
return errors.New("")
4951
}
5052

5153
return nil

pkg/auth/token/token.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ type Token struct {
1515
RefreshToken string `json:"refresh_token,omitempty" doc:"Offline or refresh token."`
1616
}
1717

18-
func init() {
19-
localizer.LoadMessageFiles("auth/token")
20-
}
21-
2218
func (c *Token) IsValid() (tokenIsValid bool, err error) {
2319
now := time.Now()
2420
if c.AccessToken != "" {

pkg/browser/browser.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import (
88
"github.com/bf2fc6cc711aee1a0c2a/cli/internal/localizer"
99
)
1010

11-
func init() {
12-
localizer.LoadMessageFiles("browser")
13-
}
14-
1511
func GetOpenBrowserCommand(url string) (*exec.Cmd, error) {
1612
switch runtime.GOOS {
1713
case "linux":

pkg/cluster/kubernetes_cluster.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ var MKCRMeta = metav1.TypeMeta{
4747
APIVersion: "rhoas.redhat.com/v1alpha1",
4848
}
4949

50-
var (
51-
SecretAlreadyExistsError error
52-
)
53-
54-
func init() {
55-
localizer.LoadMessageFiles("cluster/kubernetes")
56-
57-
SecretAlreadyExistsError = errors.New(localizer.MustLocalizeFromID("cluster.kubernetes.error.secretAlreadyExistsError"))
58-
}
59-
6050
// NewKubernetesClusterConnection configures and connects to a Kubernetes cluster
6151
func NewKubernetesClusterConnection(connection connection.Connection, config config.IConfig, logger logging.Logger, kubeconfig string) (Cluster, error) {
6252
if kubeconfig == "" {
@@ -279,7 +269,7 @@ func (c *Kubernetes) createSecret(ctx context.Context, serviceAcct *kasclient.Se
279269

280270
_, err = c.clientset.CoreV1().Secrets(namespace).Get(ctx, secretName, metav1.GetOptions{})
281271
if err == nil {
282-
return SecretAlreadyExistsError
272+
return errors.New(localizer.MustLocalizeFromID("cluster.kubernetes.error.secretAlreadyExistsError"))
283273
}
284274

285275
createdSecret, err := c.clientset.CoreV1().Secrets(namespace).Create(context.TODO(), secret, metav1.CreateOptions{})

pkg/cmd/cluster/cluster.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010

1111
// NewServiceAccountCommand creates a new command sub-group to manage service accounts
1212
func NewClusterCommand(f *factory.Factory) *cobra.Command {
13-
localizer.LoadMessageFiles("cmd/cluster")
14-
1513
cmd := &cobra.Command{
1614
Use: localizer.MustLocalizeFromID("cluster.status.cmd.use"),
1715
Short: localizer.MustLocalizeFromID("cluster.status.cmd.shortDescription"),

pkg/cmd/cluster/connect/connect.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ func NewConnectCommand(f *factory.Factory) *cobra.Command {
3535
IO: f.IOStreams,
3636
}
3737

38-
localizer.LoadMessageFiles("cmd/cluster/common", "cmd/cluster/connect", "cmd/common/flags")
39-
4038
cmd := &cobra.Command{
4139
Use: localizer.MustLocalizeFromID("cluster.connect.cmd.use"),
4240
Short: localizer.MustLocalizeFromID("cluster.connect.cmd.shortDescription"),

0 commit comments

Comments
 (0)