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

fix: Fixed the issue where application labels were not displayed afte… #7834

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

zhengkunwang223
Copy link
Member

No description provided.

Copy link

f2c-ci-robot bot commented Feb 10, 2025

Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

tagDTO.Name = tag.Name
} else {
tagDTO.Name = tag.Key
}
}
res = append(res, tagDTO)
}
Copy link
Member

Choose a reason for hiding this comment

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

The provided code is correct with no significant issues. However, there is one minor issue with how it handles missing translations for languages other than "zh" or "zh-Hant". It would be more efficient if you handle this case earlier rather than inside the loop.

Here's an optimized version of the code:

func (a AppService) GetAppTags(ctx *gin.Context) ([]response.TagDTO, error) {
	var res []response.TagDTO

	for _, tag := range tags {
		defaultName := ""
		translationLangMap := map[string][]string{
			"":         []string{tag.Key},
			"zh":        []string{"zh", "zh-CN", "zh-SG"},
			"zh-Hant":   []string{"cht", "zh-TW"},
		}

		for targetLang, fallbackLangs := range translationLangMap {
			if lang == targetLang || contains(fallbackLangs, lang) {
				err := json.Unmarshal([]byte(tag.Translations), &translations)
				if err != nil {
					return nil, errors.Wrap(err, "failed to unmarshal tag translations")
				}
				if name, ok := translations[lang]; ok && name != "" {
					tagDTO.Name = name
					res = append(res, tagDTO)
					break
				}
			}
		}
		
		if len(res) == 0 {
			defaultName = tag.Key
			if lang == "" {
				continue // Skip default name addition for root category
			}
		}

		// Add default name at the end if no translation was found for the requested language
		if lang != "" {
			tagDTO := response.TagDTO{Name: defaultName}
			res = append(res, tagDTO)
		}
	}

	return res, nil
}

func contains(arr []string, item string) bool {
	for _, v := range arr {
		if v == item {
			return true
		}
	}
	false
}

This version first initializes defaultName outside the loop and checks all supported languages ("", "zh", and "zh-Hant"). If a suitable translation is not found, it sets defaultName appropriately before appending it. This reduces redundant JSON decoding and improves clarity.

Copy link
Member

@wanghe-fit2cloud wanghe-fit2cloud left a comment

Choose a reason for hiding this comment

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

/lgtm

@wanghe-fit2cloud
Copy link
Member

/approve

@wanghe-fit2cloud wanghe-fit2cloud merged commit ab932ae into dev Feb 10, 2025
5 of 6 checks passed
@wanghe-fit2cloud wanghe-fit2cloud deleted the pr@dev@website branch February 10, 2025 03:17
Copy link

f2c-ci-robot bot commented Feb 10, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: wanghe-fit2cloud

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants