Skip to content

Commit 4fa20fe

Browse files
committed
fix translate and save translate in local storage and add text translate
1 parent de3cf39 commit 4fa20fe

File tree

16 files changed

+110
-61
lines changed

16 files changed

+110
-61
lines changed

src/app/home/home.component.html

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
<p>
2-
home works!
3-
</p>
4-
<p>
5-
{{"home" | translate}}
6-
</p>
1+
<h1>{{ "home.title" | translate }}</h1>
2+
<p>{{ "home.text" | translate }}</p>
73

src/app/home/home.module.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { HomeRoutes } from './home.routing';
21
import { NgModule } from '@angular/core';
32
import { CommonModule } from '@angular/common';
4-
import { HomeComponent } from './home.component';
53
import { TranslateModule } from '@ngx-translate/core';
64

5+
import { HomeRoutes } from './home.routing';
6+
import { HomeComponent } from './home.component';
7+
78
@NgModule({
89
imports: [
910
CommonModule,
1011
HomeRoutes,
11-
TranslateModule
12+
TranslateModule.forChild()
1213
],
1314
declarations: [HomeComponent]
1415
})
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
<p>
2-
mock-server-browser works!
3-
</p>
4-
<p>
5-
{{"mock" | translate}}
6-
</p>
1+
<h1>{{ "mock.title" | translate }}</h1>
2+
<p>{{ "mock.text" | translate }}</p>
73
<app-mock></app-mock>

src/app/mock-server-browser/mock-server-browser.module.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import { MockBrowserComponent } from './mock-browser/mock-browser.component';
21
import { NgModule } from '@angular/core';
32
import { CommonModule } from '@angular/common';
3+
import { TranslateModule } from '@ngx-translate/core';
4+
5+
import { environment } from '../../environments/environment';
6+
7+
import { MockBrowserComponent } from './mock-browser/mock-browser.component';
48
import { MockServerBrowserComponent } from './mock-server-browser.component';
59
import { MockServerBrowserRoutes } from './mock-server-browser.routing';
610
import { MockBrowserModule } from './mock-browser/mock-browser.module';
711
import { MockServerModule } from './mock-server/mock-server.module';
8-
import { environment } from '../../environments/environment';
9-
import { TranslateModule } from '@ngx-translate/core';
1012

1113
@NgModule({
1214
imports: [
1315
CommonModule,
1416
MockServerBrowserRoutes,
1517
environment.isServer ? [MockServerModule] : [MockBrowserModule],
16-
TranslateModule
18+
TranslateModule.forChild()
1719
],
1820
declarations: [MockServerBrowserComponent]
1921
})
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
<p>
2-
not-found works!
3-
</p>
1+
<h1>{{ "not-found.title" | translate }}</h1>
2+
<p>404</p>

src/app/not-found/not-found.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
3+
import { TranslateModule } from '@ngx-translate/core';
4+
35
import { NotFoundComponent } from './not-found.component';
46
import { NotFoundRoutes } from './not-found.routing';
57
import { NotFoundService } from './not-found.service';
68

79
@NgModule({
810
imports: [
911
CommonModule,
10-
NotFoundRoutes
12+
NotFoundRoutes,
13+
TranslateModule.forChild()
1114
],
1215
providers: [NotFoundService],
1316
declarations: [NotFoundComponent]

src/app/shared/layouts/sidebar/sidebar.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { Component, OnInit } from '@angular/core';
22

33
const LINKS: any[] = [
44
{ link: '/home', name: 'home', icon: 'home' },
5-
{ link: '/mock', name: 'mock', icon: 'accessibility' },
6-
{ link: '/back', name: 'back-http', icon: 'accessibility' },
5+
{ link: '/mock', name: 'mock', icon: 'info_outline' },
6+
{ link: '/back', name: 'back-http', icon: 'swap_vert' },
77
];
88

99
@Component({
1010
selector: 'app-sidebar',
1111
templateUrl: './sidebar.component.html'
1212
})
1313
export class SidebarComponent implements OnInit {
14-
public links: any[];
14+
public links: any[] = [];
1515

1616
ngOnInit() {
1717
this.links = LINKS.map(link => {

src/app/shared/layouts/toolbar/toolbar.component.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<span class="toolbar__logo">Angular RU Universal Starter</span>
22

33
<select class="toolbar__lang" (change)="changeLang($event.target.value)">
4-
<option *ngFor="let lang of languages" [value]="lang.value">
4+
<option *ngFor="let lang of languages"
5+
[value]="lang.value"
6+
[selected]="lang.value === currentLang">
57
{{ lang.name }}
68
</option>
79
</select>

src/app/shared/layouts/toolbar/toolbar.component.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22

33
import { TranslatesService } from '@shared/translates/translates.service';
44

@@ -11,12 +11,17 @@ const LANGUAGES: any[] = [
1111
selector: 'app-toolbar',
1212
templateUrl: './toolbar.component.html'
1313
})
14-
export class ToolbarComponent {
14+
export class ToolbarComponent implements OnInit {
1515
public languages: any[] = LANGUAGES;
16+
public currentLang;
1617

1718
constructor(private _translatesService: TranslatesService) {
1819
}
1920

21+
ngOnInit() {
22+
this.currentLang = this._translatesService.getCurrentLang();
23+
}
24+
2025
public changeLang(lang: string): void {
2126
this._translatesService.changeLang(lang);
2227
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
import { Injectable } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
33

4-
const DEFAULT_LANG: string = 'ru';
4+
const LANG_LIST: string[] = ['en', 'ru'];
5+
const DEFAULT_LANG: string = 'en';
56

67
@Injectable()
78
export class TranslatesService {
89
constructor(private _translateService: TranslateService) {
10+
this._translateService.addLangs(LANG_LIST);
911
this._translateService.setDefaultLang(DEFAULT_LANG);
12+
this._translateService.use(localStorage.getItem('lang') || DEFAULT_LANG);
13+
}
14+
15+
public getCurrentLang(): string {
16+
return this._translateService.currentLang;
1017
}
1118

1219
public changeLang(lang: string): void {
20+
localStorage.setItem('lang', lang);
1321
this._translateService.use(lang);
1422
}
1523
}

0 commit comments

Comments
 (0)