Skip to content

Commit ff61850

Browse files
Ismaestroiramos
authored andcommitted
feat(storage): add ngx-store module to save language in local storage
1 parent 8fd0f36 commit ff61850

5 files changed

Lines changed: 65 additions & 7 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ npm run update | Update the project dependencies with ng update
7171
* Lazy loading modules
7272
* Service Workers
7373
* Dynamic Imports
74+
* Storage module (ngx-store)
7475
* More logical structure directory (from [here](https://itnext.io/choosing-a-highly-scalable-folder-structure-in-angular-d987de65ec7))
7576
* Basic example library
7677
* Scroll restoration and anchor examples

package-lock.json

Lines changed: 47 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/app.component.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {NavigationEnd, Router} from '@angular/router';
55
import {MatSnackBar} from '@angular/material';
66
import {_} from '@biesbjerg/ngx-translate-extract/dist/utils/utils';
77
import {AppConfig} from './configs/app.config';
8+
import {LocalStorage} from 'ngx-store';
89

910
declare const require;
1011
declare const Modernizr;
@@ -16,6 +17,7 @@ declare const Modernizr;
1617

1718
export class AppComponent implements OnInit {
1819

20+
@LocalStorage() language = 'en';
1921
isOnline: boolean;
2022

2123
constructor(private translateService: TranslateService,
@@ -28,12 +30,18 @@ export class AppComponent implements OnInit {
2830

2931
ngOnInit() {
3032
this.translateService.setDefaultLang('en');
31-
this.translateService.use('en');
33+
this.translateService.use(this.language);
3234

3335
// With this we load the default language in the main bundle (cache busting)
3436
this.translateService.setTranslation('en', require('../assets/i18n/en.json'));
3537

3638
this.title.setTitle('Angular Example App');
39+
40+
this.onEvents();
41+
this.checkBrowserFeatures();
42+
}
43+
44+
onEvents() {
3745
this.router.events.subscribe((event: any) => {
3846
if (event instanceof NavigationEnd) {
3947
switch (event.urlAfterRedirects) {
@@ -53,8 +61,6 @@ export class AppComponent implements OnInit {
5361
}
5462
}
5563
});
56-
57-
this.checkBrowserFeatures();
5864
}
5965

6066
checkBrowserFeatures() {

src/app/shared/components/header/header.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {TranslateService} from '@ngx-translate/core';
33
import {_} from '@biesbjerg/ngx-translate-extract/dist/utils/utils';
44
import {APP_CONFIG, AppConfig} from '../../../configs/app.config';
55
import {ProgressBarService} from '../../../core/services/progress-bar.service';
6+
import {LocalStorage} from 'ngx-store';
67

78
@Component({
89
selector: 'app-header',
@@ -12,6 +13,8 @@ import {ProgressBarService} from '../../../core/services/progress-bar.service';
1213

1314
export class HeaderComponent implements OnInit {
1415

16+
@LocalStorage() language = 'en';
17+
1518
appConfig: any;
1619
menuItems: any[];
1720
progressBarMode: string;
@@ -34,6 +37,7 @@ export class HeaderComponent implements OnInit {
3437
changeLanguage(language: string): void {
3538
this.translateService.use(language).subscribe(() => {
3639
this.loadMenus();
40+
this.language = language;
3741
});
3842
}
3943

src/app/shared/shared.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {Error404PageComponent} from './pages/error404-page/error404-page.compone
1414
import {HeroCardComponent} from './components/hero-card/hero-card.component';
1515
import {ScrollToFirstInvalidDirective} from './directives/scroll-to-first-invalid.directive';
1616
import {NgxExampleLibraryModule} from '@ismaestro/ngx-example-library';
17+
import {WebStorageModule} from 'ngx-store';
1718

1819
@NgModule({
1920
imports: [
@@ -23,7 +24,8 @@ import {NgxExampleLibraryModule} from '@ismaestro/ngx-example-library';
2324
TranslateModule.forChild(),
2425
ReactiveFormsModule,
2526
RouterModule,
26-
NgxExampleLibraryModule
27+
NgxExampleLibraryModule,
28+
WebStorageModule
2729
],
2830
declarations: [
2931
HomePageComponent,
@@ -41,6 +43,7 @@ import {NgxExampleLibraryModule} from '@ismaestro/ngx-example-library';
4143
FlexLayoutModule,
4244
TranslateModule,
4345
NgxExampleLibraryModule,
46+
WebStorageModule,
4447
HeaderComponent,
4548
SearchBarComponent,
4649
FooterComponent,

0 commit comments

Comments
 (0)