Skip to content

Commit 6a75c10

Browse files
internationalization
1 parent 76bdfc8 commit 6a75c10

File tree

3 files changed

+101
-116
lines changed

3 files changed

+101
-116
lines changed

lib/Toolbar.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -475,15 +475,13 @@ class Toolbar {
475475
schema_name,
476476
template_name
477477
);
478-
const languages = await this.getLanguages(schema_name);
479478
const schemaClass = schema.classes[template_name];
480479
return {
481480
template_path,
482481
schema_name,
483482
template_name,
484483
schema,
485484
exportFormats,
486-
languages,
487485
schemaClass,
488486
};
489487
};
@@ -496,7 +494,6 @@ class Toolbar {
496494
template_name,
497495
schema,
498496
exportFormats,
499-
languages,
500497
schemaClass,
501498
} = loadResult;
502499

@@ -509,24 +506,29 @@ class Toolbar {
509506
template_name,
510507
schema,
511508
exportFormats,
512-
languages,
513509
schemaClass,
514510
})
515511
.then(async (context) => {
512+
513+
const languages = await context.getLocaleData(template_name);
514+
context.addTranslationResources(
515+
context.template,
516+
languages
517+
);
518+
516519
let columnCoordinates;
517520
const dh = context.getCurrentDataHarmonizer();
518-
console.log(context);
519-
if (!languages) languages = await dh.getLanguages(template_name);
520521
if (!schemaClass) schemaClass = schema.classes[template_name];
521522
if (!columnCoordinates) columnCoordinates = dh.getColumnCoordinates();
522-
523+
523524
const updateGUI = ({
524525
template_name,
525526
schema,
526527
exportFormats,
527528
languages,
528529
columnCoordinates,
529530
}) => {
531+
// debugger;
530532
const languageSelect = $('#select-language-localization').empty();
531533
for (const { langcode, nativeName } of Object.values(languages)) {
532534
languageSelect.append(new Option(nativeName, langcode));

web/index.js

+86-103
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ class AppContext {
7171
schema_tree = {};
7272
dhs = {};
7373
current_data_harmonizer_name = null;
74+
template = null;
7475

7576
constructor(appConfig) {
76-
this.template = null;
7777
this.appConfig = appConfig;
7878
}
7979

@@ -83,7 +83,8 @@ class AppContext {
8383

8484
setDataHarmonizers(data_harmonizers) {
8585
this.dhs = data_harmonizers;
86-
this.setCurrentDataHarmonizer(this.schema_tree['Container'].children[0]);
86+
// NOTE: non-deterministic, assumes that the insertion order is the right order
87+
this.setCurrentDataHarmonizer(Object.keys(this.dhs)[0]);
8788
}
8889

8990
setCurrentDataHarmonizer(data_harmonizer_name) {
@@ -213,9 +214,8 @@ class AppContext {
213214
const one_to_many = {};
214215
for (const [prefix, suffixes] of Object.entries(suffix_dict)) {
215216
if (
216-
suffixes.some(
217-
(suffix) =>
218-
typeof filtered_multi_suffixes_filter[suffix] !== 'undefined'
217+
suffixes.some((suffix) =>
218+
typeof filtered_multi_suffixes_filter[suffix] !== 'undefined'
219219
) &&
220220
suffixes.length >= suffix_threshold
221221
) {
@@ -234,9 +234,7 @@ class AppContext {
234234

235235
async initializeTemplate(template_path) {
236236
const [schema_name] = template_path.split('/');
237-
if (!this.template) {
238-
this.template = await Template.create(schema_name);
239-
}
237+
this.template = await Template.create(schema_name);
240238
return this;
241239
}
242240

@@ -249,8 +247,6 @@ class AppContext {
249247
default: { langcode: 'default', nativeName: 'Default' },
250248
};
251249

252-
console.log(this.template.locales);
253-
254250
this.template.locales.forEach((locale) => {
255251
const langcode = locale.split('-')[0];
256252
const nativeName = tags.language(langcode)
@@ -263,10 +259,11 @@ class AppContext {
263259
}
264260

265261
async addTranslationResources(template, locales = null) {
266-
console.log('add translation resources');
262+
267263
if (locales === null) {
268-
locales = this.getLocaleData(template);
264+
locales = await this.getLocaleData(template);
269265
}
266+
270267
// Consolidate function for reducing objects
271268
function consolidate(iterable, reducer) {
272269
return Object.entries(iterable).reduce(reducer, {});
@@ -371,12 +368,6 @@ class AppContext {
371368
return Array.from(slotGroups);
372369
}
373370

374-
async getLocales() {
375-
const locales = this.getLocaleData(this.template);
376-
this.addTranslationResources(this.template, locales); // TODO side effect – put elsewhere?
377-
return locales;
378-
}
379-
380371
async getExportFormats(schema) {
381372
return (await import(`@/web/templates/${schema}/export.js`)).default;
382373
}
@@ -432,17 +423,7 @@ class AppContext {
432423
// schemaClass,
433424
// columnCoordinates,
434425
}) {
435-
this.appConfig = new AppConfig(template_path);
436-
this.initializeTemplate(this.appConfig.template_path);
437-
438-
this.clearInterface();
439-
this.clearContext();
440-
441-
const [_template_name, _schema_name] =
442-
this.appConfig.template_path.split('/');
443-
const _export_formats =
444-
exportFormats || (await this.getExportFormats(_template_name));
445-
426+
446427
// attributes are the classes which feature 1-M relationshisps
447428
// to process these classes into DataHarmonizer tables, the following must be performed:
448429
// - Navigation: one tab per class = one data harmonizer per class
@@ -451,70 +432,80 @@ class AppContext {
451432
// - Loading: the system will expect an excel file with sheets or a database with tables for selection. the Toolbar must know.
452433

453434
// the existence of a container class signifies a 1-M data loading setup with an ID join
454-
// if (classes['Container']) {
455-
456-
// /* e.g.
457-
458-
// const container = {
459-
// "Container": {
460-
// "name": "Container",
461-
// "from_schema": "https://example.com/GRDI",
462-
// "attributes": {
463-
// "GRDI_Sample": {
464-
// "name": "GRDI_Sample",
465-
// "from_schema": "https://example.com/GRDI",
466-
// "multivalued": true,
467-
// "alias": "GRDI_samples",
468-
// "owner": "Container",
469-
// "domain_of": [
470-
// "Container"
471-
// ],
472-
// "range": "GRDI_Sample",
473-
// "inlined_as_list": true
474-
// },
475-
// "AMR_Test": {
476-
// "name": "AMR_Test",
477-
// "from_schema": "https://example.com/GRDI",
478-
// "multivalued": true,
479-
// "alias": "AMR_Tests",
480-
// "owner": "Container",
481-
// "domain_of": [
482-
// "Container"
483-
// ],
484-
// "range": "AMR_Test",
485-
// "inlined_as_list": true
486-
// }
487-
// },
488-
// "tree_root": true
489-
// }
490-
// }
491-
492-
// const schema_tree = {
493-
// "Container": { tree_root: true, children: ["AMR_Test", "GRDI_samples"] }
494-
// "GRDI_Sample": { shared_key: ["sample_collector_sample_ID"], children: ["AMR_Test"] } // TODO: should shared_key be shared between nodes on tree?
495-
// "AMR_Test": { shared_key: ["sample_collector_sample_ID"], children: [] },
496-
// }
497-
498-
// */
499-
500-
const schema_tree = buildSchemaTree(schema);
501-
this.setSchemaTree(schema_tree);
502-
data_harmonizers = makeDataHarmonizersFromSchemaTree(
503-
this,
504-
schema,
505-
schema_tree,
506-
_schema_name,
507-
_export_formats
508-
);
509-
// HACK
510-
delete data_harmonizers[undefined];
511-
this.setDataHarmonizers(data_harmonizers);
512-
attachPropagationEventHandlersToDataHarmonizers(
513-
data_harmonizers,
514-
schema_tree
515-
);
516435

517-
return this;
436+
/* e.g.
437+
438+
const container = {
439+
"Container": {
440+
"name": "Container",
441+
"from_schema": "https://example.com/GRDI",
442+
"attributes": {
443+
"GRDI_Sample": {
444+
"name": "GRDI_Sample",
445+
"from_schema": "https://example.com/GRDI",
446+
"multivalued": true,
447+
"alias": "GRDI_samples",
448+
"owner": "Container",
449+
"domain_of": [
450+
"Container"
451+
],
452+
"range": "GRDI_Sample",
453+
"inlined_as_list": true
454+
},
455+
"AMR_Test": {
456+
"name": "AMR_Test",
457+
"from_schema": "https://example.com/GRDI",
458+
"multivalued": true,
459+
"alias": "AMR_Tests",
460+
"owner": "Container",
461+
"domain_of": [
462+
"Container"
463+
],
464+
"range": "AMR_Test",
465+
"inlined_as_list": true
466+
}
467+
},
468+
"tree_root": true
469+
}
470+
}
471+
472+
const schema_tree = {
473+
"Container": { tree_root: true, children: ["AMR_Test", "GRDI_samples"] }
474+
"GRDI_Sample": { shared_key: ["sample_collector_sample_ID"], children: ["AMR_Test"] } // TODO: should shared_key be shared between nodes on tree?
475+
"AMR_Test": { shared_key: ["sample_collector_sample_ID"], children: [] },
476+
}
477+
478+
*/
479+
480+
this.appConfig = new AppConfig(template_path);
481+
this.clearInterface();
482+
this.clearContext();
483+
484+
return this.initializeTemplate(this.appConfig.template_path)
485+
.then(async context => {
486+
const [_template_name, _schema_name] =
487+
context.appConfig.template_path.split('/');
488+
const _export_formats =
489+
exportFormats || (await context.getExportFormats(_template_name));
490+
const schema_tree = buildSchemaTree(schema);
491+
context.appConfig.template_path.split('/');
492+
context.setSchemaTree(schema_tree);
493+
data_harmonizers = makeDataHarmonizersFromSchemaTree(
494+
this,
495+
schema,
496+
schema_tree,
497+
_schema_name,
498+
_export_formats
499+
);
500+
// HACK
501+
delete data_harmonizers[undefined];
502+
context.setDataHarmonizers(data_harmonizers);
503+
attachPropagationEventHandlersToDataHarmonizers(
504+
data_harmonizers,
505+
schema_tree
506+
);
507+
return context;
508+
});
518509
}
519510
}
520511

@@ -588,6 +579,7 @@ function findSlotNamesForClass(schema, class_name) {
588579
* @returns {Object|null} The schema tree object, or null if no "Container" classes are found.
589580
*/
590581
function buildSchemaTree(schema) {
582+
591583
function updateChildrenAndSharedKeys(data) {
592584
// Use a deep clone to avoid mutating the original object
593585
const result = JSON.parse(JSON.stringify(data));
@@ -660,11 +652,7 @@ function buildSchemaTree(schema) {
660652
};
661653
}
662654

663-
const attributes = schema.classes['Container'].attributes;
664-
const classes = Object.values(attributes).reduce(
665-
(acc, item) => acc.concat([item.range]),
666-
[]
667-
);
655+
const classes = Object.keys(schema.classes).filter(el => el !== 'dh_interface');
668656
const tree_base = {
669657
Container: { tree_root: true, children: classes },
670658
};
@@ -991,6 +979,7 @@ const main = async function () {
991979
context
992980
.initializeTemplate(context.appConfig.template_path)
993981
.then(async (context) => {
982+
994983
// // internationalize
995984
// // TODO: connect to locale of browser!
996985
// // Takes `lang` as argument (unused)
@@ -999,12 +988,6 @@ const main = async function () {
999988
$(document).localize();
1000989
dhs.forEach((dh) => dh.render());
1001990
});
1002-
context.addTranslationResources(
1003-
context.template,
1004-
context.getLocaleData()
1005-
);
1006-
1007-
// await context.setupDataHarmonizers();
1008991

1009992
// // TODO: data harmonizers require initialization code inside of the toolbar to fully render? wut
1010993
new Toolbar(dhToolbarRoot, context.getCurrentDataHarmonizer(), menu, {

web/templates/menu.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11

22
{
3-
"grdi": {
4-
"GRDI_Sample": {
5-
"name": "GRDI_Sample",
3+
"test": {
4+
"TEST": {
5+
"name": "TEST",
66
"status": "published",
77
"display": true
88
}
99
},
10-
"test": {
11-
"TEST": {
12-
"name": "TEST",
10+
"grdi": {
11+
"GRDI_Sample": {
12+
"name": "GRDI_Sample",
1313
"status": "published",
1414
"display": true
1515
}

0 commit comments

Comments
 (0)