|
14 | 14 | */ |
15 | 15 |
|
16 | 16 | import { deleteFaq, fetchAllFaqsByCategory, fetchCategoryTranslations } from '../api'; |
17 | | -import { pushErrorNotification, pushNotification } from '../utils'; |
| 17 | +import { normalizeLanguageCode, pushErrorNotification, pushNotification } from '../utils'; |
18 | 18 | import { addElement } from '../../../../assets/src/utils'; |
19 | 19 |
|
20 | 20 | export const handleFaqOverview = async () => { |
@@ -69,24 +69,32 @@ export const handleFaqOverview = async () => { |
69 | 69 | event.preventDefault(); |
70 | 70 |
|
71 | 71 | const translations = await fetchCategoryTranslations(categoryId); |
72 | | - const existingLink = element.nextElementSibling.childNodes[0]; |
73 | | - const regionNames = new Intl.DisplayNames([language], { type: 'language' }); |
| 72 | + const dropdownMenu = element.parentElement.querySelector('.dropdown-menu'); |
74 | 73 | const faqId = element.getAttribute('data-pmf-faq-id'); |
75 | 74 | let options = []; |
76 | 75 |
|
77 | | - for (const [languageCode, languageName] of Object.entries(translations)) { |
| 76 | + dropdownMenu.querySelectorAll('#dropdownTranslation').forEach((link) => { |
| 77 | + options.push(link.innerText); |
| 78 | + }); |
| 79 | + |
| 80 | + for (let [languageCode, categoryName] of Object.entries(translations)) { |
78 | 81 | if (languageCode !== language) { |
79 | | - document.querySelectorAll('#dropdownTranslation').forEach((link) => { |
80 | | - options.push(link.innerText); |
81 | | - }); |
82 | | - if (!options.includes(`→ ${regionNames.of(languageCode)}`)) { |
| 82 | + let displayName; |
| 83 | + try { |
| 84 | + const normalizedCode = normalizeLanguageCode(languageCode); |
| 85 | + displayName = new Intl.DisplayNames([language], { type: 'language' }).of(normalizedCode); |
| 86 | + } catch (e) { |
| 87 | + displayName = null; |
| 88 | + } |
| 89 | + |
| 90 | + if (displayName && !options.includes(`→ ${displayName}`)) { |
83 | 91 | const newTranslationLink = addElement('a', { |
84 | 92 | classList: 'dropdown-item', |
85 | 93 | id: 'dropdownTranslation', |
86 | 94 | href: `?action=editentry&id=${faqId}&cat=${categoryId}&translateTo=${languageCode}`, |
87 | | - innerText: `→ ${regionNames.of(languageCode)}`, |
| 95 | + innerText: `→ ${displayName}`, |
88 | 96 | }); |
89 | | - existingLink.insertAdjacentElement('afterend', newTranslationLink); |
| 97 | + dropdownMenu.appendChild(newTranslationLink); |
90 | 98 | } |
91 | 99 | } |
92 | 100 | } |
@@ -260,7 +268,7 @@ const populateCategoryTable = async (catgoryId, faqs) => { |
260 | 268 | [addElement('i', { classList: 'bi bi-globe', 'aria-hidden': 'true' })] |
261 | 269 | ), |
262 | 270 | addElement('div', { classList: 'dropdown-menu', 'aria-labelledby': 'dropdownAddNewTranslation' }, [ |
263 | | - addElement('a', { classList: 'dropdown-item', id: 'dropdownTranslation', innerText: 'n/a' }), |
| 271 | + addElement('a', { classList: 'dropdown-item', id: 'dropdownTranslation', innerText: '' }), |
264 | 272 | ]), |
265 | 273 | ]), |
266 | 274 | ]) |
|
0 commit comments