Skip to content

Commit

Permalink
Add core function for matching content language to current language.
Browse files Browse the repository at this point in the history
Update duplicate resource prioritization with new function.
  • Loading branch information
rtibbles committed May 9, 2024
1 parent be59560 commit 2d074f3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
21 changes: 21 additions & 0 deletions kolibri/core/assets/src/utils/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'] || {};
Expand Down
20 changes: 11 additions & 9 deletions kolibri/plugins/learn/assets/src/utils/contentNode.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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;
Expand Down

0 comments on commit 2d074f3

Please sign in to comment.