Skip to content

Commit

Permalink
Merge branch 'dh2-beta-release' into dh2-i18n-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethbruskiewicz committed Nov 5, 2024
2 parents 6ae2c94 + df6f3a9 commit 570e09f
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 119 deletions.
69 changes: 43 additions & 26 deletions lib/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,75 +332,93 @@ export default class AppContext {
if (locales === null) {
locales = this.getLocaleData(template);
}

const defaultLocale = {
langcode: 'default',
nativeName: 'Default',
};

// Combine default and found locales
locales = {
...locales,
default: defaultLocale,
...findLocalesForLangcodes(template.locales),
};

// Helper function to process permissible values into a translation map
const getEnumResource = (enumObject) => {
return consolidate(enumObject, (acc, [enumKey, enumObj]) => {
const { permissible_values } = enumObj || {};
return consolidate(enumObject, (acc, [, enumObj]) => {
const { permissible_values } = enumObj || {};
if (permissible_values) {
Object.entries(permissible_values).forEach(([enumKey, enumData]) => {
Object.entries(permissible_values).forEach(([, enumData]) => {
const { title, text } = enumData;
const key = text; // Enum keys are the 'text'
const value = title || text; // Use title if available, fallback to text
acc[key] = value; // Forward translation (text -> title)
acc[value] = key; // Reverse translation (title -> text)
acc[key] = value; // Forward translation (text -> title)
acc[value] = key; // Reverse translation (title -> text)
});
}
return acc;
});
};

// Build translation maps for each language
const translationsByLanguage = {};

Object.entries(template.translations).forEach(([langcode, translation]) => {
const currentLang = langcode.split('-')[0]; // Use primary language code

// Compute the enum resource for this language
const enumResource = getEnumResource(translation.schema.enums);

// Store the translations for this language
translationsByLanguage[currentLang] = {
...enumResource,
};

console.info("Computed translation resources for:", currentLang, enumResource);

console.info(
'Computed translation resources for:',
currentLang,
enumResource
);
});

// Generate translation maps between all possible language combinations
const languageCodes = Object.keys(translationsByLanguage);

// Loop over each source-destination language pair
languageCodes.forEach((sourceLang) => {
languageCodes.forEach((targetLang) => {
if (sourceLang === targetLang) return; // Skip identical language pairs

const sourceTranslation = translationsByLanguage[sourceLang];
const targetTranslation = translationsByLanguage[targetLang];
// const targetTranslation = translationsByLanguage[targetLang];

// Add resources for the current source-target language pair
const translationNamespace = `${sourceLang}_to_${targetLang}`;
i18n.addResources(sourceLang, translationNamespace, sourceTranslation);

i18n.addResources(
sourceLang,
translationNamespace,
removeNumericKeys(sourceTranslation)
);

// Add reverse mapping for this pair (target -> source)
const reverseTranslationMap = invert(sourceTranslation);
const reverseNamespace = `${targetLang}_to_${sourceLang}`;
i18n.addResources(targetLang, reverseNamespace, reverseTranslationMap);

console.info(`Added translation resources from ${sourceLang} to ${targetLang}:`, translationNamespace);
console.info(`Added reverse translation resources from ${targetLang} to ${sourceLang}:`, reverseNamespace);
i18n.addResources(
targetLang,
reverseNamespace,
removeNumericKeys(reverseTranslationMap)
);

console.info(
`Added translation resources from ${sourceLang} to ${targetLang}:`,
translationNamespace
);
console.info(
`Added reverse translation resources from ${targetLang} to ${sourceLang}:`,
reverseNamespace
);
});
});
}
Expand Down Expand Up @@ -557,7 +575,6 @@ export default class AppContext {
*/

// this.clearContext();
return this.initializeTemplate(this.appConfig.template_path, {
forced_schema,
}).then(async (context) => {
Expand Down
13 changes: 11 additions & 2 deletions lib/DataHarmonizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,8 +552,7 @@ class DataHarmonizer {
let dataObjects = jsonData.Container[container_class];
list_data = this.loadDataObjects(dataObjects);
this.hot.loadData(list_data);
}

}
} else {
// assume tabular data if not a JSON datatype
list_data = this.loadSpreadsheetData(contentBuffer.binary);
Expand Down Expand Up @@ -1705,6 +1704,16 @@ class DataHarmonizer {

fillColumn(colname, value) {
const fieldYCoordinates = this.getFieldYCoordinates();
const fields = this.getFields();

console.log(
colname,
value,
fieldYCoordinates,
this.getFieldNameMap(fields),
this.getFieldTitleMap(fields)
);

// ENSURE colname hasn't been tampered with (the autocomplete allows
// other text)
if (colname in fieldYCoordinates) {
Expand Down
Loading

0 comments on commit 570e09f

Please sign in to comment.