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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ChangeDetectionStrategy, Component } from "@angular/core";
2+
import { TranslateDirective, TranslatePipe } from "@ngx-translate/core";
3+
4+
5+
@Component({
6+
selector: "app-standalone-component",
7+
standalone: true,
8+
imports: [TranslateDirective, TranslatePipe],
9+
styleUrl: "./standalone.component.scss",
10+
templateUrl: "./standalone.component.html",
11+
changeDetection: ChangeDetectionStrategy.OnPush,
12+
})
13+
export class StandaloneComponent
14+
{
15+
16+
}

projects/test-app/src/styles.scss

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,43 @@
1-
/* You can add global styles to this file, and also import other style files */
1+
:root {
2+
--mat-sys-primary: #c2282b;
3+
--mat-sys-on-primary: #fff;
4+
5+
--mat-sys-secondary: #3f4258;
6+
--mat-sys-on-secondary: #fff;
7+
8+
--mat-sys-surface-dim: #ffe8f1;
9+
10+
--mat-sys-outline: #e5e5e5;
11+
12+
--mat-sys-corner-small: 8px;
13+
--mat-sys-corner-medium: 12px;
14+
15+
--mat-sys-level0: 0px 0px 0px 0px rgba(0, 0, 0, .2), 0px 0px 0px 0px rgba(0, 0, 0, .14), 0px 0px 0px 0px rgba(0, 0, 0, .12);
16+
--mat-sys-level1: 0px 2px 1px -1px rgba(0, 0, 0, .2), 0px 1px 1px 0px rgba(0, 0, 0, .14), 0px 1px 3px 0px rgba(0, 0, 0, .12);
17+
--mat-sys-level2: 0px 3px 3px -2px rgba(0, 0, 0, .2), 0px 3px 4px 0px rgba(0, 0, 0, .14), 0px 1px 8px 0px rgba(0, 0, 0, .12);
18+
--mat-sys-level3: 0px 3px 5px -1px rgba(0, 0, 0, .2), 0px 6px 10px 0px rgba(0, 0, 0, .14), 0px 1px 18px 0px rgba(0, 0, 0, .12);
19+
20+
--space-smallest: 4px;
21+
--space-smaller: 8px;
22+
--space-small: 12px;
23+
--space: 16px;
24+
}
25+
26+
27+
body {
28+
font-family: Arial, Helvetica, sans-serif;
29+
}
30+
31+
p, .translated{
32+
background-color: var(--mat-sys-surface-dim);
33+
padding:0.5rem 1rem;
34+
}
35+
36+
.title {
37+
38+
h1 {
39+
background-color: var(--mat-sys-surface-dim);
40+
border-radius: var(--mat-sys-corner-small);
41+
padding: var(--space) var(--space-small);
42+
}
43+
}

0 commit comments

Comments
 (0)