Skip to content

Commit 154997c

Browse files
authored
Merge pull request #1549 from rbalet/docs/test-app/language-switch
docs(test-app): language switch
2 parents fa7505d + 1fadc8d commit 154997c

12 files changed

+155
-69
lines changed

projects/test-app/src/app/app.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<app-language-switch></app-language-switch>
2+
13
<div class="title">
24
<h1>{{ 'demo.title' | translate }}</h1>
35
</div>

projects/test-app/src/app/app.component.scss

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
1-
import { Component } from "@angular/core";
1+
import { Component, OnInit } from "@angular/core";
22
import { _, TranslateDirective, TranslatePipe, TranslateService } from "@ngx-translate/core";
3-
import { StandaloneComponent } from "./standalone.component";
3+
import { map } from 'rxjs';
4+
import { TranslationObject } from '../../../ngx-translate/src/public-api';
5+
import { LanguageSwitchComponent } from './language-switch/language-switch.component';
6+
import { StandaloneComponent } from "./standalone/standalone.component";
47

58

69
@Component({
710
selector: "app-root",
811
standalone: true,
9-
imports: [TranslateDirective, TranslatePipe, StandaloneComponent],
10-
templateUrl: "./app.component.html",
11-
styleUrl: "./app.component.scss"
12+
imports: [
13+
// Components
14+
LanguageSwitchComponent,
15+
StandaloneComponent,
16+
17+
// Vendors
18+
TranslateDirective,
19+
TranslatePipe,
20+
],
21+
templateUrl: "./app.component.html"
1222
})
13-
export class AppComponent
23+
export class AppComponent implements OnInit
1424
{
1525
title = _("test-app");
1626

@@ -19,4 +29,14 @@ export class AppComponent
1929
this.translate.setDefaultLang('en');
2030
this.translate.use('en');
2131
}
32+
33+
ngOnInit() {
34+
// Service Get method with a set of string[]
35+
this.translate
36+
.get(['demo.simple.text-as-attribute', 'demo.simple.text-as-content'])
37+
.pipe(map((arr: TranslationObject) => {
38+
return Object.values(arr).join(', ')
39+
}))
40+
.subscribe(console.info);
41+
}
2242
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@for(lang of translateService.getLangs(); track lang) {
2+
<button (click)="translateService.use(lang)" [class.active]="translateService.currentLang === lang">{{ lang }}</button>
3+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
:host{
2+
display: block;
3+
margin: 0 auto;
4+
text-align: center;
5+
6+
button {
7+
background-color: var(--mat-sys-secondary);
8+
border: none;
9+
color: var(--mat-sys-on-secondary);
10+
padding: 0.25rem 0.5rem;
11+
text-align: center;
12+
text-decoration: none;
13+
display: inline-block;
14+
font-size: 16px;
15+
margin-left: 1rem;
16+
border-radius: 0.5rem;
17+
transition: all 0.3s ease;
18+
min-width: 120px;
19+
min-height: 46px;
20+
cursor: pointer;
21+
box-shadow: var(--mat-sys-level2);
22+
23+
&:hover {
24+
box-shadow: var(--mat-sys-level3);
25+
&:active {
26+
box-shadow: var(--mat-sys-level1);
27+
}
28+
}
29+
30+
&.active {
31+
background-color: var(--mat-sys-primary);
32+
color: var(--mat-sys-on-primary);
33+
34+
}
35+
}
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Component, inject } from '@angular/core';
2+
import { TranslateService } from '@ngx-translate/core';
3+
4+
@Component({
5+
selector: 'app-language-switch',
6+
standalone: true,
7+
styleUrl: './language-switch.component.scss',
8+
templateUrl: './language-switch.component.html',
9+
})
10+
export class LanguageSwitchComponent {
11+
translateService = inject(TranslateService);
12+
}

projects/test-app/src/app/standalone.component.html

Lines changed: 0 additions & 10 deletions
This file was deleted.

projects/test-app/src/app/standalone.component.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="standalone-container">
2+
<div class="title">
3+
<h1>{{ 'standalone-component.title' | translate }}</h1>
4+
</div>
5+
<div>
6+
<h2>Simple translations without parameters</h2>
7+
8+
<p [translate]="'demo.simple.text-as-attribute'"></p>
9+
10+
<p translate>demo.simple.text-as-content</p>
11+
</div>
12+
</div>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.standalone-container {
2+
border: 1px solid var(--mat-sys-outline);
3+
border-radius: var(--mat-sys-corner-small);
4+
padding: 0 var(--space);
5+
}

0 commit comments

Comments
 (0)