[英]Angular i18n to generic form from array of objects
I have a form that is created from an object I received from the API.我有一个从 API 收到的 object 创建的表单。 To create this form I created an array of objects on the TS and then I make the loop and add the inputs/selects.
为了创建这个表单,我在 TS 上创建了一个对象数组,然后创建循环并添加输入/选择。 The form is rendered correctly, but when I translate the application, this form doesn't is translated.
表单正确呈现,但是当我翻译应用程序时,该表单没有被翻译。
import { SpecializedAreasComponents } from 'src/app/models/SpecializedAreasComponent';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'citation-constraint-form',
templateUrl: './citation-constraint-form.component.html',
styleUrls: ['./citation-constraint-form.component.scss'],
})
export class CitationConstraintFormComponent implements SpecializedAreasComponents, OnInit {
form: FormGroup;
@Input() data: any;
@Output() output: EventEmitter<any> = new EventEmitter();
@Input() errors: any = {};
@Input() allowEdit;
constraints: any = [];
selectOne = [
{
value: '',
description: 'Select One...',
},
];
bool = [
...this.selectOne,
{
description: 'Yes',
value: true,
},
{
description: 'No',
value: false,
},
];
activitySectors = [
...this.selectOne,
{
value: 'industry',
description: 'Industry',
},
{
value: 'wholesale',
description: 'Wholesale',
},
{
value: 'retail',
description: 'Retail',
},
{
value: 'finalConsumer',
description: 'Final Consumer',
},
{
value: 'service',
description: 'Service',
},
{
value: 'ruralProducer',
description: 'Rural Producer',
},
{
value: 'generalWarehouse',
description: 'General Warehouse',
},
{
value: 'stockWarehouse',
description: 'Stock Warehouse',
},
{
value: 'transporter',
description: 'Transporter',
},
{
value: 'publicAgency',
description: 'Public Agency',
},
{
value: 'telco',
description: 'Communication Service',
},
{
value: 'importer',
description: 'Importer',
},
{
value: 'mealSupplier',
description: 'Meal Supplier',
},
{
value: 'coops',
description: 'Cooperative',
},
{
value: 'farmCoop',
description: 'Farms Cooperative',
},
{
value: 'transporter',
description: 'Transporter',
},
{
value: 'distributionCenter',
description: 'Distribution Center',
},
{
value: 'electricityDistributor',
description: 'Electricity Distributor',
},
{
value: 'fuelDistributor',
description: 'Fuel Distributor',
},
{
value: 'pharmaDistributor',
description: 'Medication Distributor',
},
{
value: 'waterDistributor',
description: 'Water Dispenser',
},
{
value: 'publicAgency',
description: 'Orgao Publico',
},
{
value: 'religiousEstablishment',
description: 'Temples of Any Worship',
},
{
value: 'itaipubiNacional',
description: 'Itaipu Binacional',
},
{
value: 'auctioneer',
description: 'Auctioneer',
},
];
federalTaxRegimes = [
{
value: 'realProfit',
description: 'Real profit',
},
{
value: 'estimatedProfit',
description: 'Estimated profit',
},
{
value: 'simplified',
description: 'Simplified',
},
{
value: 'simplifiedOverGrossthreshold',
description: 'Simplified over grossthreshold',
},
{
value: 'simplifiedEntrepreneur',
description: 'Simplified entrepreneur',
},
{
value: 'notApplicable',
description: 'Not applicable',
},
{
value: 'individual',
description: 'Individual',
},
];
entityTypes = [
{
value: 'business',
description: 'Business',
},
{
value: 'individual',
description: 'Individual',
},
{
value: 'federalGovernment',
description: 'Federal government',
},
{
value: 'stateGovernment',
description: 'State government',
},
{
value: 'cityGovernment',
description: 'City government',
},
{
value: 'foreign',
description: 'Foreign',
},
];
specialProductClasses = [
...this.selectOne,
{
value: '1',
description: 'SUS',
},
{
value: '2',
description: 'Órgão do Poder Executivo',
},
{
value: '3',
description: 'Bancos',
},
{
value: '4',
description: 'Comércio/Indústria',
},
{
value: '5',
description: 'Poder Legislativo/Judiciário',
},
{
value: '0',
description: 'Outro',
},
];
fiscalTypes = [
...this.selectOne,
{
value: 'FOR PRODUCT',
description: 'For product',
},
{
value: 'FOR MERCHANDISE',
description: 'For merchandise',
},
{
value: 'NO RESTRICTION',
description: 'No restriction',
},
{
value: 'SERVICE',
description: 'Service',
},
{
value: 'FEEDSTOCK',
description: 'Feedstock',
},
{
value: 'FIXED ASSETS',
description: 'Fixed assets',
},
{
value: 'PACKAGING',
description: 'Packaging',
},
{
value: 'PRODUCT IN PROCESS',
description: 'Product in process',
},
{
value: 'SUBPRODUCT',
description: 'Subproduct',
},
{
value: 'INTERMEDIATE PRODUCT',
description: 'Intermediate product',
},
{
value: 'MATERIAL FOR USAGE AND CONSUMPTION',
description: 'Material for usage and consumption',
},
{
value: 'OTHER INPUTS',
description: 'Other inputs',
},
];
constraintsTypes = [
{
key: 'item',
label: 'Item',
type: 'text',
ignore: false,
},
{
key: 'unit',
label: 'UNIT',
type: 'text',
ignore: false,
},
{
key: 'lowerLimitAmount',
label: 'Lower Limit Amount',
type: 'number',
ignore: false,
},
{
key: 'upperLimitAmout',
label: 'Upper Limit Amount',
type: 'number',
},
{
key: 'fiscalType',
label: 'Fiscal Type',
type: 'select',
options: [...this.fiscalTypes],
ignore: false,
},
{
key: 'activitySectorListEmmiter',
label: 'Activity Sector List Emmiter',
type: 'select',
options: [...this.activitySectors],
ignore: false,
},
{
key: 'activitySectorListReceiver',
label: 'Activity Sector List Receiver',
type: 'select',
options: [...this.activitySectors],
ignore: false,
},
{
key: 'packaging',
label: 'Packaging',
type: 'text',
ignore: false,
},
{
key: 'supplementaryInformation',
label: 'Supplementary Information',
type: 'text',
ignore: false,
},
{
key: 'specialProductClass',
label: 'Special Product Class',
type: 'select',
options: [...this.specialProductClasses],
ignore: false,
},
{
key: 'ceanTax',
label: 'CEAN Taxable',
type: 'text',
ignore: false,
},
{
key: 'icmsTaxPayer',
label: 'ICMS Tax Payer',
type: 'select',
options: [...this.bool],
ignore: false,
},
{
key: 'subjectPayrollTaxRelief',
label: 'Subject to Payroll Tax Relief',
type: 'select',
options: [...this.bool],
ignore: false,
},
{
key: 'federalTaxRegimeListReceiver',
label: 'Federal Tax Regime',
type: 'multiple',
options: [...this.federalTaxRegimes],
ignore: false,
},
{
key: 'entityTypeListReceiver',
label: 'Entity Type',
type: 'multiple',
options: [...this.entityTypes],
ignore: false,
},
];
rows = [];
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
constraint: new FormGroup({
item: new FormControl(''),
unit: new FormControl(''),
lowerLimitAmount: new FormControl(''),
upperLimitAmout: new FormControl(''),
fiscalType: new FormControl(''),
activitySectorListEmmiter: new FormControl(''),
activitySectorListReceiver: new FormControl(''),
packaging: new FormControl(''),
supplementaryInformation: new FormControl(''),
specialProductClass: new FormControl(''),
ceanTax: new FormControl(''),
icmsTaxPayer: new FormControl(''),
subjectPayrollTaxRelief: new FormControl(''),
federalTaxRegimeListReceiver: new FormControl([]),
entityTypeListReceiver: new FormControl([]),
}),
});
}
getDescFromKey(key) {
return (
{
unit: 'UNIT',
lowerLimitAmount: 'Lower Limit Amount',
upperLimitAmount: 'Upper Limit Amount',
fiscalType: 'Fiscal Type',
activitySectorListEmmiter: 'Activity Sector List Emmiter',
activitySectorListReceiver: 'Activity Sector List Receiver',
packaging: 'Packaging',
supplementaryInformation: 'Supplementary Information',
specialProductClass: 'Special Product Class',
ceanTax: 'CEAN Taxable',
icmsTaxPayer: 'ICMS Tax Player',
subjectPayrollTaxRelief: 'Subject to Payroll Tax Relief',
federalTaxRegimeListReceiver: 'Federal Tax Regime',
entityTypeListReceiver: 'Entity Type',
}[key] || key
);
}
updateForm(value) {
if (
value?.constraint?.federalTaxRegimeListReceiver &&
value?.constraint?.federalTaxRegimeListReceiver[0]?.value &&
!value?.constraint?.federalTaxRegimeListReceiver[0]?.description
) {
value.constraint.federalTaxRegimeListReceiver = value.constraint.federalTaxRegimeListReceiver.map((ftr) => {
return {
value: ftr.value,
description: this.getDescriptionForConstraintsTypes(ftr.value, 'federalTaxRegimeListReceiver'),
};
});
}
if (
value?.constraint?.entityTypeListReceiver &&
value?.constraint?.entityTypeListReceiver[0]?.value &&
!value?.constraint?.entityTypeListReceiver[0]?.description
) {
value.constraint.entityTypeListReceiver = value.constraint.entityTypeListReceiver.map((ftr) => {
return {
value: ftr.value,
description: this.getDescriptionForConstraintsTypes(ftr.value, 'entityTypeListReceiver'),
};
});
}
this.form.patchValue(this.allowEdit ? value : { constraint: value });
}
getDescriptionForConstraintsTypes(value, type) {
const list = type === 'entityTypeListReceiver' ? this.entityTypes : this.federalTaxRegimes;
const option = list.find((o) => o.value === value);
return option.description || '';
}
ngOnInit() {
if (!this.allowEdit) this.form.disable();
this.form.valueChanges.subscribe(() => this.output.emit(this.form.value));
this.updateForm(this.data.constraint);
this.constraintsTypes.map((constraint, i) => {
const constr = {
key: constraint.key,
label: constraint.label,
value: constraint.type === 'multiple' ? [] : '',
type: constraint.type,
options: constraint?.options || [],
};
if (i === 0) {
this.rows.push([constr]);
} else if (i % 3 === 0) {
this.rows.push([constr]);
} else {
this.rows[this.rows.length - 1].push(constr);
}
});
}
extractArray(item) {
if (Array.isArray(item)) {
return item.map((a) => (typeof a === 'string' ? a : a.code));
}
return item;
}
}
<form [formGroup]="form">
<s-row class="pad-all-none" *ngFor="let row of rows" formGroupName="constraint">
<s-col span="4" *ngFor="let const of row">
<fieldset>
<label [for]="const.key" i18n>{{ const.label }}</label>
<input
[type]="const.type"
[placeholder]="const.label"
[formControlName]="const.key"
[id]="const.key"
*ngIf="const.type === 'text' || const.type === 'number'"
[attr.disabled]="!allowEdit ? '' : null"
/>
<select
[id]="const.key"
[formControlName]="const.key"
*ngIf="const.type === 'select'"
[attr.disabled]="!allowEdit ? '' : null"
>
<option *ngFor="let option of const.options" value="{{ option?.value }}">
{{ option.description }}
</option>
</select>
<kendo-multiselect
[data]="const.options"
[textField]="'description'"
[valueField]="'value'"
[id]="const.key"
[formControlName]="const.key"
[filterable]="true"
[placeholder]="'Select Multiple'"
*ngIf="const.type === 'multiple'"
[attr.disabled]="!allowEdit ? '' : null"
>
</kendo-multiselect>
</fieldset>
</s-col>
</s-row>
</form>
Does anyone have any idea of how I can use the i18n here?有人知道我如何在这里使用 i18n 吗?
Thank you.谢谢你。 Lucas.
卢卡斯。
Solved using the global variable $localize
使用全局变量
$localize
解决
My code result was:我的代码结果是:
import { SpecializedAreasComponents } from 'src/app/models/SpecializedAreasComponent';
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'citation-constraint-form',
templateUrl: './citation-constraint-form.component.html',
styleUrls: ['./citation-constraint-form.component.scss'],
})
export class CitationConstraintFormComponent implements SpecializedAreasComponents, OnInit {
form: FormGroup;
@Input() data: any;
@Output() output: EventEmitter<any> = new EventEmitter();
@Input() errors: any = {};
@Input() allowEdit;
constraints: any = [];
selectOne = [
{
value: '',
description: $localize`Select One...`,
},
];
bool = [
...this.selectOne,
{
description: $localize`Yes`,
value: true,
},
{
description: $localize`No`,
value: false,
},
];
activitySectors = [
...this.selectOne,
{
value: 'industry',
description: $localize`Industry`,
},
{
value: 'wholesale',
description: $localize`Wholesale`,
},
{
value: 'retail',
description: $localize`Retail`,
},
{
value: 'finalConsumer',
description: $localize`Final Consumer`,
},
{
value: 'service',
description: $localize`Service`,
},
{
value: 'ruralProducer',
description: $localize`Rural Producer`,
},
{
value: 'generalWarehouse',
description: $localize`General Warehouse`,
},
{
value: 'stockWarehouse',
description: $localize`Stock Warehouse`,
},
{
value: 'transporter',
description: $localize`Transporter`,
},
{
value: 'publicAgency',
description: $localize`Public Agency`,
},
{
value: 'telco',
description: $localize`Communication Service`,
},
{
value: 'importer',
description: $localize`Importer`,
},
{
value: 'mealSupplier',
description: $localize`Meal Supplier`,
},
{
value: 'coops',
description: $localize`Cooperative`,
},
{
value: 'farmCoop',
description: $localize`Farms Cooperative`,
},
{
value: 'transporter',
description: $localize`Transporter`,
},
{
value: 'distributionCenter',
description: $localize`Distribution Center`,
},
{
value: 'electricityDistributor',
description: $localize`Electricity Distributor`,
},
{
value: 'fuelDistributor',
description: $localize`Fuel Distributor`,
},
{
value: 'pharmaDistributor',
description: $localize`Medication Distributor`,
},
{
value: 'waterDistributor',
description: $localize`Water Dispenser`,
},
{
value: 'publicAgency',
description: $localize`Orgao Publico`,
},
{
value: 'religiousEstablishment',
description: $localize`Temples of Any Worship`,
},
{
value: 'itaipubiNacional',
description: $localize`Itaipu Binacional`,
},
{
value: 'auctioneer',
description: $localize`Auctioneer`,
},
];
federalTaxRegimes = [
{
value: 'realProfit',
description: $localize`Real profit`,
},
{
value: 'estimatedProfit',
description: $localize`Estimated profit`,
},
{
value: 'simplified',
description: $localize`Simplified`,
},
{
value: 'simplifiedOverGrossthreshold',
description: $localize`Simplified over grossthreshold`,
},
{
value: 'simplifiedEntrepreneur',
description: $localize`Simplified entrepreneur`,
},
{
value: 'notApplicable',
description: $localize`Not applicable`,
},
{
value: 'individual',
description: $localize`Individual`,
},
];
entityTypes = [
{
value: 'business',
description: $localize`Business`,
},
{
value: 'individual',
description: $localize`Individual`,
},
{
value: 'federalGovernment',
description: $localize`Federal government`,
},
{
value: 'stateGovernment',
description: $localize`State government`,
},
{
value: 'cityGovernment',
description: $localize`City government`,
},
{
value: 'foreign',
description: $localize`Foreign`,
},
];
specialProductClasses = [
...this.selectOne,
{
value: '1',
description: $localize`SUS`,
},
{
value: '2',
description: $localize`Órgão do Poder Executivo`,
},
{
value: '3',
description: $localize`Bancos`,
},
{
value: '4',
description: $localize`Comércio/Indústria`,
},
{
value: '5',
description: $localize`Poder Legislativo/Judiciário`,
},
{
value: '0',
description: $localize`Outro`,
},
];
fiscalTypes = [
...this.selectOne,
{
value: 'FOR PRODUCT',
description: $localize`For product`,
},
{
value: 'FOR MERCHANDISE',
description: $localize`For merchandise`,
},
{
value: 'NO RESTRICTION',
description: $localize`No restriction`,
},
{
value: 'SERVICE',
description: $localize`Service`,
},
{
value: 'FEEDSTOCK',
description: $localize`Feedstock`,
},
{
value: 'FIXED ASSETS',
description: $localize`Fixed assets`,
},
{
value: 'PACKAGING',
description: $localize`Packaging`,
},
{
value: 'PRODUCT IN PROCESS',
description: $localize`Product in process`,
},
{
value: 'SUBPRODUCT',
description: $localize`Subproduct`,
},
{
value: 'INTERMEDIATE PRODUCT',
description: $localize`Intermediate product`,
},
{
value: 'MATERIAL FOR USAGE AND CONSUMPTION',
description: $localize`Material for usage and consumption`,
},
{
value: 'OTHER INPUTS',
description: $localize`Other inputs`,
},
];
constraintsTypes = [
{
key: 'item',
label: $localize`Item`,
type: 'text',
ignore: false,
},
{
key: 'unit',
label: $localize`UNIT`,
type: 'text',
ignore: false,
},
{
key: 'lowerLimitAmount',
label: $localize`Lower Limit Amount`,
type: 'number',
ignore: false,
},
{
key: 'upperLimitAmout',
label: $localize`Upper Limit Amount`,
type: 'number',
},
{
key: 'fiscalType',
label: $localize`Fiscal Type`,
type: 'select',
options: [...this.fiscalTypes],
ignore: false,
},
{
key: 'activitySectorListEmmiter',
label: $localize`Activity Sector List Emmiter`,
type: 'select',
options: [...this.activitySectors],
ignore: false,
},
{
key: 'activitySectorListReceiver',
label: $localize`Activity Sector List Receiver`,
type: 'select',
options: [...this.activitySectors],
ignore: false,
},
{
key: 'packaging',
label: $localize`Packaging`,
type: 'text',
ignore: false,
},
{
key: 'supplementaryInformation',
label: $localize`Supplementary Information`,
type: 'text',
ignore: false,
},
{
key: 'specialProductClass',
label: $localize`Special Product Class`,
type: 'select',
options: [...this.specialProductClasses],
ignore: false,
},
{
key: 'ceanTax',
label: $localize`CEAN Taxable`,
type: 'text',
ignore: false,
},
{
key: 'icmsTaxPayer',
label: $localize`ICMS Tax Payer`,
type: 'select',
options: [...this.bool],
ignore: false,
},
{
key: 'subjectPayrollTaxRelief',
label: $localize`Subject to Payroll Tax Relief`,
type: 'select',
options: [...this.bool],
ignore: false,
},
{
key: 'federalTaxRegimeListReceiver',
label: $localize`Federal Tax Regime`,
type: 'multiple',
options: [...this.federalTaxRegimes],
ignore: false,
},
{
key: 'entityTypeListReceiver',
label: $localize`Entity Type`,
type: 'multiple',
options: [...this.entityTypes],
ignore: false,
},
];
rows = [];
constructor(private fb: FormBuilder) {
this.form = this.fb.group({
constraint: new FormGroup({
item: new FormControl(''),
unit: new FormControl(''),
lowerLimitAmount: new FormControl(''),
upperLimitAmout: new FormControl(''),
fiscalType: new FormControl(''),
activitySectorListEmmiter: new FormControl(''),
activitySectorListReceiver: new FormControl(''),
packaging: new FormControl(''),
supplementaryInformation: new FormControl(''),
specialProductClass: new FormControl(''),
ceanTax: new FormControl(''),
icmsTaxPayer: new FormControl(''),
subjectPayrollTaxRelief: new FormControl(''),
federalTaxRegimeListReceiver: new FormControl([]),
entityTypeListReceiver: new FormControl([]),
}),
});
}
getDescFromKey(key) {
return (
{
unit: $localize`UNIT`,
lowerLimitAmount: $localize`Lower Limit Amount`,
upperLimitAmount: $localize`Upper Limit Amount`,
fiscalType: $localize`Fiscal Type`,
activitySectorListEmmiter: $localize`Activity Sector List Emmiter`,
activitySectorListReceiver: $localize`Activity Sector List Receiver`,
packaging: $localize`Packaging`,
supplementaryInformation: $localize`Supplementary Information`,
specialProductClass: 'Special Product Class',
ceanTax: $localize`CEAN Taxable`,
icmsTaxPayer: $localize`ICMS Tax Player`,
subjectPayrollTaxRelief: $localize`Subject to Payroll Tax Relief`,
federalTaxRegimeListReceiver: $localize`Federal Tax Regime`,
entityTypeListReceiver: $localize`Entity Type`,
}[key] || key
);
}
updateForm(value) {
if (
value?.constraint?.federalTaxRegimeListReceiver &&
value?.constraint?.federalTaxRegimeListReceiver[0]?.value &&
!value?.constraint?.federalTaxRegimeListReceiver[0]?.description
) {
value.constraint.federalTaxRegimeListReceiver = value.constraint.federalTaxRegimeListReceiver.map((ftr) => {
return {
value: ftr.value,
description: this.getDescriptionForConstraintsTypes(ftr.value, 'federalTaxRegimeListReceiver'),
};
});
}
if (
value?.constraint?.entityTypeListReceiver &&
value?.constraint?.entityTypeListReceiver[0]?.value &&
!value?.constraint?.entityTypeListReceiver[0]?.description
) {
value.constraint.entityTypeListReceiver = value.constraint.entityTypeListReceiver.map((ftr) => {
return {
value: ftr.value,
description: this.getDescriptionForConstraintsTypes(ftr.value, 'entityTypeListReceiver'),
};
});
}
this.form.patchValue(this.allowEdit ? value : { constraint: value });
}
getDescriptionForConstraintsTypes(value, type) {
const list = type === 'entityTypeListReceiver' ? this.entityTypes : this.federalTaxRegimes;
const option = list.find((o) => o.value === value);
return option.description || '';
}
ngOnInit() {
if (!this.allowEdit) this.form.disable();
this.form.valueChanges.subscribe(() => this.output.emit(this.form.value));
this.updateForm(this.data.constraint);
this.constraintsTypes.map((constraint, i) => {
const constr = {
key: constraint.key,
label: constraint.label,
value: constraint.type === 'multiple' ? [] : '',
type: constraint.type,
options: constraint?.options || [],
};
if (i === 0) {
this.rows.push([constr]);
} else if (i % 3 === 0) {
this.rows.push([constr]);
} else {
this.rows[this.rows.length - 1].push(constr);
}
});
}
extractArray(item) {
if (Array.isArray(item)) {
return item.map((a) => (typeof a === 'string' ? a : a.code));
}
return item;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.