Skip to content

Commit f4daaa4

Browse files
committed
Translate tutorial
1 parent d2b3cb2 commit f4daaa4

File tree

13 files changed

+137
-18
lines changed

13 files changed

+137
-18
lines changed

cloudapp/src/app/app-routing.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ExternalComponent } from './external/external.component';
99
import { XmlComponent } from './xml/xml.component';
1010
import { BindComponent } from './bind/bind.component';
1111
import { StoreComponent } from './store/store.component';
12+
import { TranslateComponent } from './translate/translate.component';
1213

1314
const routes: Routes = [
1415
{ path: '', component: MainComponent },
@@ -20,6 +21,7 @@ const routes: Routes = [
2021
{ path: 'xml', component: XmlComponent },
2122
{ path: 'bind', component: BindComponent },
2223
{ path: 'store', component: StoreComponent },
24+
{ path: 'translate', component: TranslateComponent },
2325
];
2426

2527
@NgModule({

cloudapp/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ExternalComponent } from './external/external.component';
1818
import { XmlComponent } from './xml/xml.component';
1919
import { BindComponent } from './bind/bind.component';
2020
import { StoreComponent } from './store/store.component';
21+
import { TranslateComponent } from './translate/translate.component';
2122

2223
export function getToastrModule() {
2324
return ToastrModule.forRoot({
@@ -38,7 +39,8 @@ export function getToastrModule() {
3839
ExternalComponent,
3940
XmlComponent,
4041
BindComponent,
41-
StoreComponent
42+
StoreComponent,
43+
TranslateComponent
4244
],
4345
imports: [
4446
MaterialModule,

cloudapp/src/app/bind/bind.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export class BindComponent implements OnInit, OnDestroy {
3030
const entities = (pageInfo.entities||[]).filter(e=>e.type=='ITEM');
3131
if (entities.length > 0) {
3232
this.restService.call(entities[0].link)
33-
.subscribe(res=>this.form = toFormGroup(res) as FormGroup);
33+
.subscribe(res=>this.form = toFormGroup(res));
34+
} else {
35+
this.form = null;
3436
}
3537
});
3638
}

cloudapp/src/app/main/main.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ <h1>
1212
<li><a [routerLink]="['xml']">Working with bibliographic records in XML</a></li>
1313
<li><a [routerLink]="['bind']">Model binding with REST objects</a></li>
1414
<li><a [routerLink]="['store']">Using the Store Service</a></li>
15+
<li><a [routerLink]="['translate']">Translating your app</a></li>
1516
</ul>
1617
</section>

cloudapp/src/app/store/store.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class StoreComponent implements OnInit {
2828

2929
this.storeService.get(FORM_NAME).subscribe( value => {
3030
if (value) {
31-
this.form = toFormGroup(value) as FormGroup;
31+
this.form = toFormGroup(value);
3232
}
3333

3434
this.form.valueChanges.pipe(
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<div>
2+
<p translate [translateParams]="{ date: today }">Translate.Intro</p>
3+
</div>
4+
<div>
5+
<p translate>Translate.ButtonDesc</p>
6+
<button mat-stroked-button type="button" (click)="hi()">{{ 'Translate.Button' | translate }}</button>
7+
</div>
8+
<div>
9+
<p translate>Translate.SelectDesc</p>
10+
<mat-form-field>
11+
<mat-label translate>Translate.Policy</mat-label>
12+
<mat-select>
13+
<mat-option *ngFor="let policy of policies" [value]="policy.code">
14+
{{ policy.desc | translate }}
15+
</mat-option>
16+
</mat-select>
17+
</mat-form-field>
18+
</div>
19+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
div {
2+
margin-bottom: 10px;
3+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { AppService } from '../app.service';
3+
import { TranslateService } from '@ngx-translate/core';
4+
import { CloudAppEventsService, InitData, CloudAppRestService } from '@exlibris/exl-cloudapp-angular-lib';
5+
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
6+
7+
@Component({
8+
selector: 'app-translate',
9+
templateUrl: './translate.component.html',
10+
styleUrls: ['./translate.component.scss']
11+
})
12+
export class TranslateComponent implements OnInit {
13+
initData: InitData;
14+
policies: { code: string, desc: string }[];
15+
today = new Date().toLocaleDateString();
16+
blockTypes: any;
17+
18+
constructor(
19+
private appService: AppService,
20+
private translate: TranslateService,
21+
private eventsService: CloudAppEventsService,
22+
private restService: CloudAppRestService
23+
) { }
24+
25+
ngOnInit() {
26+
this.translate.get('Translate.Title').subscribe(text=>this.appService.setTitle(text));
27+
this.eventsService.getInitData().subscribe(data=>this.initData = data);
28+
this.policies = [
29+
{ code: 'D', desc: _('Translate.Policies.DAILY') },
30+
{ code: 'W', desc: _('Translate.Policies.WEEKLY') },
31+
{ code: 'M', desc: _('Translate.Policies.MONTHLY') },
32+
{ code: 'Y', desc: _('Translate.Policies.YEARLY') },
33+
];
34+
}
35+
36+
hi() {
37+
alert(this.translate.instant('Translate.Prompt',
38+
{ name: this.initData.user.firstName } )
39+
);
40+
}
41+
42+
}

cloudapp/src/app/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { AbstractControl, FormArray, FormGroup, FormControl } from "@angular/forms";
22

3-
export function toFormGroup (object: Object): AbstractControl {
3+
/** Converts an object to a FormGroup recursively handling properties and array */
4+
export function toFormGroup (object: Object): FormGroup {
5+
return _toFormGroup(object) as FormGroup;
6+
}
7+
8+
function _toFormGroup (object: Object): AbstractControl {
49
if (Array.isArray(object)) {
510
return new FormArray(object.map(entry=>toFormGroup(entry)));
611
} else if (typeof object === 'object' && object != null) {

cloudapp/src/i18n/en.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"Translate": {
3+
"Button": "Say hi",
4+
"ButtonDesc": "This shows how to translate the contents of a button and to define the string inside of the typescript file.",
5+
"Intro": "This tutorial shows some of the several ways to indicate strings for translation. This shows how to translate text and include parameters such as today's date ({{date}})",
6+
"Policies": {
7+
"DAILY": "Daily",
8+
"MONTHLY": "Monthly",
9+
"WEEKLY": "Weekly",
10+
"YEARLY": "Yearly"
11+
},
12+
"Policy": "Policy",
13+
"Prompt": "Hello {{name}}",
14+
"SelectDesc": "This shows how to translate the descriptions of select options.",
15+
"Title": "Translating your app"
16+
}
17+
}

0 commit comments

Comments
 (0)