From 2d074f33d46415e6dc4aa176d226418fc53b0bdf Mon Sep 17 00:00:00 2001 From: Richard Tibbles Date: Thu, 9 May 2024 12:59:35 -0700 Subject: [PATCH] Add core function for matching content language to current language. Update duplicate resource prioritization with new function. --- kolibri/core/assets/src/utils/i18n.js | 21 +++++++++++++++++++ .../learn/assets/src/utils/contentNode.js | 20 ++++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/kolibri/core/assets/src/utils/i18n.js b/kolibri/core/assets/src/utils/i18n.js index 989875440ce..1946b2e3ab6 100644 --- a/kolibri/core/assets/src/utils/i18n.js +++ b/kolibri/core/assets/src/utils/i18n.js @@ -25,6 +25,27 @@ export const getContentLangDir = language => { return (language || {}).lang_direction || languageDirections.LTR; }; +const contentLanguageCodes = { + ff: ['ful', 'fuv'], + ny: ['nya'], + sw: ['swa'], + yo: ['yor'], +}; + +export const getContentLangActive = language => { + const langCode = languageIdToCode(currentLanguage); + const additionalCodes = contentLanguageCodes[langCode] || []; + if (language.id.toLowerCase() === currentLanguage.toLowerCase()) { + // Best possible match, return a 2 to have it still be truthy, but distinguishable + // from a 1 which is a lang_code match + return 2; + } + if (language.lang_code === langCode || additionalCodes.includes(language.lang_code)) { + return 1; + } + return 0; +}; + const logging = logger.getLogger(__filename); const languageGlobals = plugin_data['languageGlobals'] || {}; diff --git a/kolibri/plugins/learn/assets/src/utils/contentNode.js b/kolibri/plugins/learn/assets/src/utils/contentNode.js index ca1915da991..3612720457d 100644 --- a/kolibri/plugins/learn/assets/src/utils/contentNode.js +++ b/kolibri/plugins/learn/assets/src/utils/contentNode.js @@ -1,6 +1,6 @@ import groupBy from 'lodash/groupBy'; import sortBy from 'lodash/sortBy'; -import { currentLanguage } from 'kolibri.utils.i18n'; +import { getContentLangActive } from 'kolibri.utils.i18n'; export function deduplicateResources(contentNodes) { const grouped = groupBy(contentNodes, 'content_id'); @@ -9,15 +9,17 @@ export function deduplicateResources(contentNodes) { if (groupedNodes.length === 1) { return groupedNodes[0]; } - const langCode = currentLanguage.split('-')[0]; const sortedNodes = sortBy(groupedNodes, n => { - if (n.lang && n.lang.id === currentLanguage) { - // Best language match return 0 to sort first - return 0; - } - if (n.lang && n.lang.lang_code === langCode) { - // lang_code match, so next best - return 1; + if (n.lang) { + const langActiveScore = getContentLangActive(n.lang); + if (langActiveScore == 2) { + // Best possible match return 0 to sort first + return 0; + } + if (langActiveScore == 1) { + // lang_code match, so next best + return 1; + } } // Everything else return 2;