Skip to content

Commit 0e2e742

Browse files
guisoares1unai-reliefappAntoineRelief
authored
fix: survey localization not working after update. (#1936)
--------- Co-authored-by: Unai Zalba <[email protected]> Co-authored-by: Antoine Hurard <[email protected]>
1 parent 3d62401 commit 0e2e742

File tree

10 files changed

+58
-41
lines changed

10 files changed

+58
-41
lines changed

apps/back-office/src/app/dashboard/dashboard.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
3-
import { AppAbility } from '@oort-front/shared';
3+
import { AppAbility, UnsubscribeComponent } from '@oort-front/shared';
4+
import { takeUntil } from 'rxjs';
45

56
/**
67
* Main BO dashboard component
@@ -10,7 +11,7 @@ import { AppAbility } from '@oort-front/shared';
1011
templateUrl: './dashboard.component.html',
1112
styleUrls: ['./dashboard.component.scss'],
1213
})
13-
export class DashboardComponent {
14+
export class DashboardComponent extends UnsubscribeComponent {
1415
// === HEADER TITLE ===
1516
public title = 'Back-office';
1617

@@ -27,8 +28,9 @@ export class DashboardComponent {
2728
private translate: TranslateService,
2829
private ability: AppAbility
2930
) {
31+
super();
3032
this.setNavGroups();
31-
translate.onLangChange.subscribe(() => {
33+
translate.onLangChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
3234
this.setNavGroups();
3335
});
3436
}

libs/shared/src/i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@
738738
"resourceSelectText": "First you have to select a resource before setting any filters"
739739
}
740740
},
741-
"saveSurvey": "SavingSurvey"
741+
"saveSurvey": "Saving the form"
742742
},
743743
"formMapProperties": {
744744
"geofields": {

libs/shared/src/lib/components/dashboard-filter/dashboard-filter.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { DrawerPositionerModule } from './directives/drawer-positioner/drawer-po
88
import { DrawerPositionerDirective } from './directives/drawer-positioner/drawer-positioner.directive';
99
import { SurveyCreatorModule } from 'survey-creator-angular';
1010
import { SurveyModule } from 'survey-angular-ui';
11+
import 'survey-core/survey.i18n.js';
12+
import 'survey-creator-core/survey-creator-core.i18n.js';
1113

1214
/** Dashboard floating filter module. */
1315
@NgModule({

libs/shared/src/lib/components/dashboard-filter/filter-builder-modal/filter-builder-modal.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ import { renderGlobalProperties } from '../../../survey/render-global-properties
1919
import { ReferenceDataService } from '../../../services/reference-data/reference-data.service';
2020
import { FormHelpersService } from '../../../services/form-helper/form-helper.service';
2121
import { Question } from '../../../survey/types';
22+
import 'survey-core/survey.i18n.js';
23+
import 'survey-creator-core/survey-creator-core.i18n.js';
24+
2225
/**
2326
* Data passed to initialize the filter builder
2427
*/

libs/shared/src/lib/components/form-builder/form-builder.component.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import {
2626
import { SurveyCreatorModel } from 'survey-creator-core';
2727
import { Question } from '../../survey/types';
2828
import { DOCUMENT } from '@angular/common';
29+
import { takeUntil } from 'rxjs';
30+
import { UnsubscribeComponent } from '../utils/unsubscribe/unsubscribe.component';
2931

3032
/**
3133
* Array containing the different types of questions.
@@ -96,7 +98,10 @@ const CORE_FIELD_CLASS = 'core-question';
9698
templateUrl: './form-builder.component.html',
9799
styleUrls: ['./form-builder.component.scss'],
98100
})
99-
export class FormBuilderComponent implements OnInit, OnChanges, OnDestroy {
101+
export class FormBuilderComponent
102+
extends UnsubscribeComponent
103+
implements OnInit, OnChanges, OnDestroy
104+
{
100105
@Input() form!: Form;
101106
@Output() save: EventEmitter<any> = new EventEmitter();
102107
@Output() formChange: EventEmitter<any> = new EventEmitter();
@@ -126,9 +131,10 @@ export class FormBuilderComponent implements OnInit, OnChanges, OnDestroy {
126131
private formHelpersService: FormHelpersService,
127132
@Inject(DOCUMENT) private document: Document
128133
) {
134+
super();
129135
// translate the editor in the same language as the interface
130136
surveyLocalization.currentLocale = this.translate.currentLang;
131-
this.translate.onLangChange.subscribe(() => {
137+
this.translate.onLangChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
132138
surveyLocalization.currentLocale = this.translate.currentLang;
133139
this.setFormBuilder(this.surveyCreator.text);
134140
});
@@ -177,7 +183,8 @@ export class FormBuilderComponent implements OnInit, OnChanges, OnDestroy {
177183
}
178184
}
179185

180-
ngOnDestroy(): void {
186+
override ngOnDestroy(): void {
187+
super.ngOnDestroy();
181188
this.surveyCreator.survey?.dispose();
182189
}
183190

@@ -316,7 +323,7 @@ export class FormBuilderComponent implements OnInit, OnChanges, OnDestroy {
316323
renderGlobalProperties(this.referenceDataService)
317324
)
318325
);
319-
// this.surveyCreator.survey.locale = this.translate.currentLang; // -> set the defaultLanguage property also
326+
this.surveyCreator.survey.locale = surveyLocalization.currentLocale; // -> set the defaultLanguage property also
320327

321328
// add move up/down buttons
322329
// ¿No need? As the new surveyjs creator has a built in drag and drop feature to move questions between pages and within a page between questions

libs/shared/src/lib/components/form-builder/form-builder.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { TranslateModule } from '@ngx-translate/core';
55
import { DateInputModule } from '@progress/kendo-angular-dateinputs';
66
import { DialogModule } from '@oort-front/ui';
77
import { SurveyCreatorModule } from 'survey-creator-angular';
8+
import 'survey-core/survey.i18n.js';
9+
import 'survey-creator-core/survey-creator-core.i18n.js';
810

911
/**
1012
* FormBuilderModule is a class used to manage all the modules and components

libs/shared/src/lib/components/ui/core-grid/filter/filter.component.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { Component, Input, OnDestroy, OnInit } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33
import {
44
BaseFilterCellComponent,
55
FilterService,
66
} from '@progress/kendo-angular-grid';
77
import { isEmpty, isNil } from 'lodash';
8+
import { Subject, takeUntil } from 'rxjs';
89

910
/**
1011
* Shared-grid-filter component
@@ -16,7 +17,7 @@ import { isEmpty, isNil } from 'lodash';
1617
})
1718
export class GridFilterComponent
1819
extends BaseFilterCellComponent
19-
implements OnInit
20+
implements OnInit, OnDestroy
2021
{
2122
/** @returns selected value */
2223
public get selectedValue(): any {
@@ -85,6 +86,7 @@ export class GridFilterComponent
8586
] as const;
8687

8788
public selectedOperator!: string;
89+
private destroy$: Subject<void> = new Subject<void>();
8890

8991
/**
9092
* Constructor for shared-grid-filter
@@ -103,7 +105,7 @@ export class GridFilterComponent
103105
this.selectedOperator = this.isNotArray ? 'eq' : 'contains';
104106
this.setOperators();
105107
this.choices = (this.data || []).slice();
106-
this.translate.onLangChange.subscribe(() => {
108+
this.translate.onLangChange.pipe(takeUntil(this.destroy$)).subscribe(() => {
107109
this.setOperators();
108110
});
109111
}
@@ -176,4 +178,10 @@ export class GridFilterComponent
176178
};
177179
this.applyFilter(this.filter);
178180
}
181+
182+
override ngOnDestroy(): void {
183+
super.ngOnDestroy();
184+
this.destroy$.next();
185+
this.destroy$.complete();
186+
}
179187
}

libs/shared/src/lib/survey/components/utils.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Dialog } from '@angular/cdk/dialog';
22
import { UntypedFormControl } from '@angular/forms';
33
import { NgZone } from '@angular/core';
44
// todo: as it something to do with survey-angular
5-
// import { surveyLocalization } from 'survey-core';
5+
import { SurveyModel, surveyLocalization } from 'survey-core';
66
import localForage from 'localforage';
7+
import { Question } from '../types';
78

89
/**
910
* Build the search button for resource and resources components
@@ -17,20 +18,18 @@ import localForage from 'localforage';
1718
* @returns The button DOM element
1819
*/
1920
export const buildSearchButton = (
20-
question: any,
21+
question: Question,
2122
fieldsSettingsForm: any,
2223
multiselect: boolean,
2324
dialog: Dialog,
2425
temporaryRecords: UntypedFormControl,
2526
document: Document
2627
): any => {
2728
const searchButton = document.createElement('button');
28-
// todo: not working
29-
// searchButton.innerText = surveyLocalization.getString(
30-
// 'oort:search',
31-
// question.survey.locale
32-
// );
33-
searchButton.innerText = 'Search';
29+
searchButton.innerText = surveyLocalization.getString(
30+
'oort:search',
31+
(question.survey as SurveyModel).locale
32+
);
3433
searchButton.className = 'sd-btn !px-3 !py-2';
3534
searchButton.style.marginRight = '8px';
3635
if (fieldsSettingsForm) {
@@ -84,19 +83,17 @@ export const buildSearchButton = (
8483
* @returns The button DOM element
8584
*/
8685
export const buildAddButton = (
87-
question: any,
86+
question: Question,
8887
multiselect: boolean,
8988
dialog: Dialog,
9089
ngZone: NgZone,
9190
document: Document
9291
): any => {
9392
const addButton = document.createElement('button');
94-
// todo: not working
95-
// addButton.innerText = surveyLocalization.getString(
96-
// 'oort:addNewRecord',
97-
// question.survey.locale
98-
// );
99-
addButton.innerText = 'Add new record';
93+
addButton.innerText = surveyLocalization.getString(
94+
'oort:addNewRecord',
95+
(question.survey as SurveyModel).locale
96+
);
10097
addButton.className = 'sd-btn !px-3 !py-2';
10198
if (question.addRecord && question.addTemplate && !question.isReadOnly) {
10299
addButton.onclick = async () => {
@@ -111,7 +108,7 @@ export const buildAddButton = (
111108
locale: question.resource.value,
112109
askForConfirm: false,
113110
...(question.prefillWithCurrentRecord && {
114-
prefillData: question.survey.data,
111+
prefillData: (question.survey as SurveyModel).data,
115112
}),
116113
},
117114
height: '98%',

libs/shared/src/lib/survey/localization.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { set } from 'lodash';
21
import { surveyLocalization } from 'survey-core';
32

43
/** Available localizable strings, for survey */
@@ -33,11 +32,10 @@ const SURVEY_LOCALIZABLE_STRINGS = [
3332
export const initLocalization = () => {
3433
for (const item of SURVEY_LOCALIZABLE_STRINGS) {
3534
for (const [locale, value] of Object.entries(item.locales)) {
36-
set(
37-
surveyLocalization.locales,
38-
`surveyLocalization.locales.${locale}.oort:${item.key}`,
39-
value
40-
);
35+
if (!surveyLocalization.locales[locale]) {
36+
surveyLocalization.locales[locale] = {};
37+
}
38+
surveyLocalization.locales[locale][`oort:${item.key}`] = value;
4139
}
4240
}
4341
};

libs/shared/src/lib/survey/widgets/comment-widget.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {
22
CustomWidgetCollection,
33
Serializer,
4-
// SurveyModel,
5-
// surveyLocalization,
4+
SurveyModel,
5+
surveyLocalization,
66
} from 'survey-core';
77
import { Question, QuestionComment } from '../types';
88
/**
@@ -38,12 +38,10 @@ export const init = (
3838
mainDiv.id = 'editComment';
3939
mainDiv.style.marginBottom = '0.5em';
4040
const btnEl = document.createElement('button');
41-
// todo: not working
42-
// btnEl.innerText = surveyLocalization.getString(
43-
// 'oort:edit',
44-
// (question.survey as SurveyModel).locale
45-
// );
46-
btnEl.innerText = 'Edit';
41+
btnEl.innerText = surveyLocalization.getString(
42+
'oort:edit',
43+
(question.survey as SurveyModel).locale
44+
);
4745
btnEl.className = 'sd-btn !px-3 !py-2';
4846
btnEl.style.width = '50px';
4947
mainDiv.appendChild(btnEl);

0 commit comments

Comments
 (0)