fix(frontend): improve synonym search (#5639)

# Description of Changes

This pull request updates the `getSynonyms` utility function to improve
how it retrieves tool synonyms from translations and enhances logging
for debugging. The function now checks multiple possible translation
keys and provides more detailed log messages to help identify issues
with missing or malformed tags.

Improvements to translation key lookup and logging:

* The function now checks both `home.${toolId}.tags` and
`${toolId}.tags` keys, using the first valid translation found to
increase robustness when fetching synonyms.
* Added detailed logging to indicate which translation key was used and
the resulting tags, making it easier to trace and debug missing or
incorrect translations.
* Improved error handling and cleaned up the function's closing syntax
for clarity and maintainability.

<img width="750" height="703" alt="image"
src="https://github.com/user-attachments/assets/2e89c391-1fc1-4ce1-b171-5ee84aedf37a"
/>


---

## Checklist

### General

- [ ] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [ ] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md)
(if applicable)
- [ ] I have performed a self-review of my own code
- [ ] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### Translations (if applicable)

- [ ] I ran
[`scripts/counter_translation.py`](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/docs/counter_translation.md)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [ ] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/devGuide/DeveloperGuide.md#6-testing)
for more details.
This commit is contained in:
Ludy
2026-02-06 12:01:37 +01:00
committed by GitHub
parent a23f0ec53d
commit 47e0c9cb51

View File

@@ -1,26 +1,29 @@
import { TFunction } from 'i18next';
import { TFunction } from "i18next";
// Helper function to get synonyms for a tool (only from translations)
export const getSynonyms = (t: TFunction, toolId: string): string[] => {
try {
const tagsKey = `${toolId}.tags`;
const tags = t(tagsKey) as unknown as string;
const candidateKeys = [`home.${toolId}.tags`, `${toolId}.tags`];
const match = candidateKeys
.map((key) => ({ key, value: t(key) as unknown as string }))
.find(({ key, value }) => value && value !== key);
const tags = match?.value;
const usedKey = match?.key;
// If the translation key doesn't exist or returns the key itself, return empty array
if (!tags || tags === tagsKey) {
if (!tags) {
console.warn(`[Tags] Missing tags for tool: ${toolId}`);
return [];
}
// Split by comma and clean up the tags
const cleanedTags = tags
.split(',')
.split(",")
.map((tag: string) => tag.trim())
.filter((tag: string) => tag.length > 0);
// Log the tags found for this tool
if (cleanedTags.length > 0) {
console.info(`[Tags] Tool "${toolId}" has ${cleanedTags.length} tags:`, cleanedTags);
console.info(`[Tags] Tool "${toolId}" (${usedKey}) has ${cleanedTags.length} tags:`, cleanedTags);
} else {
console.warn(`[Tags] Tool "${toolId}" has empty tags value`);
}
@@ -29,6 +32,5 @@ export const getSynonyms = (t: TFunction, toolId: string): string[] => {
} catch (error) {
console.error(`[Tags] Failed to get translated synonyms for tool ${toolId}:`, error);
return [];
}};
}
};