Skip to content
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
6 changes: 5 additions & 1 deletion backend/i18n/i18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,15 @@ func Init() {
_, _ = bundle.LoadMessageFileFS(fs, "lang/en.yaml")
_, _ = bundle.LoadMessageFileFS(fs, "lang/zh-Hant.yaml")
_, _ = bundle.LoadMessageFileFS(fs, "lang/fa.yaml")
_, _ = bundle.LoadMessageFileFS(fs, "lang/pt.yaml")
}

func UseI18nForCmd(lang string) {
if lang == "" {
lang = "zh"
lang = "en"
}
if lang == "pt-BR" {
lang = "pt"
}
if bundle == nil {
Init()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to modify this code. The given function works as expected without any errors or unnecessary operations.

However, for the sake of completeness, you might want to:

  1. Add error handling for loading the YAML files.
  2. Return non-zero values instead of panicking.

The current implementation has no issues according to my review but it can be improved in terms of robustness and usability with added functionality like error responses and better return value management.

In summary: keep it!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to perform such detailed analysis for this function without any specific context provided.

Expand Down
2 changes: 1 addition & 1 deletion cmd/server/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
var language string

func init() {
RootCmd.PersistentFlags().StringVarP(&language, "language", "l", "zh", "Set the language (default is 'zh')")
RootCmd.PersistentFlags().StringVarP(&language, "language", "l", "en", "Set the language")
}

var RootCmd = &cobra.Command{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the current version of the program to analyze, but generally speaking, this code snippet appears to be missing a section where it handles errors gracefully if an invalid input is provided for "language". This error handling might not cause problems currently, but without proper checks and logging mechanisms, it could later become problematic when encountering rare input formats or incorrect values such as non-string data types that cannot be passed through var declaration.

For optimization, you can consider adding appropriate validations at command-line parsing stage or before storing into global state variables like RootCmd, avoiding any runtime panic. For example:

  
const (
   DEFAULT_LANGUAGE = "zh"
)

func init() {
	var language string

	switch flag := RootCmd.PersistentFlags().GetString("language"); flag {
	case "":
		language = DEFAULT_LANGUAGE
	default:
	.language = flag
	}

	if !utils.IsValidLanguage(language) {
	  // Handle invalid language value
	}

This change would help ensure that the user's choice of language will always be validated against known valid languages, even in the case where no value has been explicitly specified. Additionally, more granular validation logic (such as checking for specific codes associated with supported locales) should also be implemented here depending upon what type of system or application this script operates on.

Expand Down
Loading