feat: add language selection and currency formatting options
This commit is contained in:
		
					parent
					
						
							
								698375b1bf
							
						
					
				
			
			
				commit
				
					
						f96a09abc4
					
				
			
		
					 7 changed files with 77 additions and 7 deletions
				
			
		|  | @ -1,5 +1,10 @@ | ||||||
| <p>Name: {{hotel.hotelName}}</p> | <p>Name: {{hotel.hotelName}}</p> | ||||||
| <p>Description: {{hotel.description}}</p> | <p>Description: {{hotel.description}}</p> | ||||||
| <p>Price: {{hotel.price}}$</p> | <p>Price: {{hotel.price | currency : "EUR" : "symbol" : "2.2-2" : "de-DE"}}</p> | ||||||
|  | <select [ngModel]="selectedLanguage" (ngModelChange)="languageChange($event)"> | ||||||
|  |   @for (lang of langs; track lang.lang) { | ||||||
|  |     <option value="{{lang.lang}}">{{lang.lang}}</option> | ||||||
|  |   } | ||||||
|  | </select> | ||||||
| <img src="{{hotel.imageUrl}}" alt="Hotel"> | <img src="{{hotel.imageUrl}}" alt="Hotel"> | ||||||
| <p class="border-b border-black">{{hotel.rating}}</p> | <p class="border-b border-black">{{hotel.rating}}</p> | ||||||
|  |  | ||||||
|  | @ -2,13 +2,36 @@ import { Component } from "@angular/core"; | ||||||
| import { ChildComponent } from "../Child/child.component"; | import { ChildComponent } from "../Child/child.component"; | ||||||
| import { Input } from "@angular/core"; | import { Input } from "@angular/core"; | ||||||
| import { Hotel } from "./hotel"; | import { Hotel } from "./hotel"; | ||||||
|  | import { CurrencyPipe } from "@angular/common"; | ||||||
|  | import { FormsModule } from "@angular/forms"; | ||||||
| 
 | 
 | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'app-hotel-item', |   selector: 'app-hotel-item', | ||||||
|   standalone: true, |   standalone: true, | ||||||
|   templateUrl: './HotelItem.component.html', |   templateUrl: './HotelItem.component.html', | ||||||
|   imports: [ChildComponent], |   imports: [ChildComponent, CurrencyPipe, FormsModule], | ||||||
| }) | }) | ||||||
| export class HotelItem { | export class HotelItem { | ||||||
|   @Input() public hotel!: Hotel; |   @Input() public hotel!: Hotel; | ||||||
|  |   public selectedLanguage?: string; | ||||||
|  | 
 | ||||||
|  |   public languageChange(lang: string) { | ||||||
|  |     this.selectedLanguage = lang; | ||||||
|  |     console.log(this.selectedLanguage); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   public langs = [ | ||||||
|  |     { | ||||||
|  |       "lang": "en", | ||||||
|  |       "code": "en-US" | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       "lang": "cn", | ||||||
|  |       "code": "cn-CN" | ||||||
|  |     }, | ||||||
|  |     { | ||||||
|  |       "lang": "de", | ||||||
|  |       "code": "de-DE" | ||||||
|  |     } | ||||||
|  |   ]; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,6 +1,11 @@ | ||||||
|  | <h1>{{'hello' | uppercase | text}}</h1> | ||||||
| <app-search [(input)]="search"></app-search> | <app-search [(input)]="search"></app-search> | ||||||
| <div *ngFor="let hotel of hotels"> | @for (hotel of hotels; track hotel.hotelId) { | ||||||
|   @if (hotel.hotelName.includes(search)) { |   @let should_show = search === "" || hotel.hotelName.includes(search); | ||||||
|  |   @if (should_show) { | ||||||
|     <app-hotel-item [hotel]="hotel"></app-hotel-item> |     <app-hotel-item [hotel]="hotel"></app-hotel-item> | ||||||
|   } |   } | ||||||
| </div> | } | ||||||
|  | @if (!has_hotels()) { | ||||||
|  |   <p>No hotels found</p> | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -1,12 +1,13 @@ | ||||||
| import { Component, input } from '@angular/core'; | import { Component, input } from '@angular/core'; | ||||||
| import { HotelItem } from './HotelItem/HotelItem.component'; | import { HotelItem } from './HotelItem/HotelItem.component'; | ||||||
| import { CommonModule } from '@angular/common'; |  | ||||||
| import { SearchComponent } from './Search/search.component'; | import { SearchComponent } from './Search/search.component'; | ||||||
|  | import { UpperCasePipe } from '@angular/common'; | ||||||
|  | import { TextPipe } from '../text.pipe'; | ||||||
| 
 | 
 | ||||||
| @Component({ | @Component({ | ||||||
|   selector: 'app-root', |   selector: 'app-root', | ||||||
|   standalone: true, |   standalone: true, | ||||||
|   imports: [HotelItem, CommonModule, SearchComponent], |   imports: [HotelItem, SearchComponent, UpperCasePipe, TextPipe], | ||||||
|   templateUrl: './app.component.html', |   templateUrl: './app.component.html', | ||||||
|   styleUrl: './app.component.css' |   styleUrl: './app.component.css' | ||||||
| }) | }) | ||||||
|  | @ -17,6 +18,15 @@ export class AppComponent { | ||||||
|     console.log(this.search); |     console.log(this.search); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  |   public has_hotels(): boolean { | ||||||
|  |     for (let hotel of this.hotels) { | ||||||
|  |       if (hotel.hotelName.includes(this.search)) { | ||||||
|  |         return true; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|  |     return false; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|   public hotels = [ |   public hotels = [ | ||||||
|     { |     { | ||||||
|       "hotelId": 1, |       "hotelId": 1, | ||||||
|  |  | ||||||
|  | @ -1,8 +1,13 @@ | ||||||
| import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; | import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core'; | ||||||
| import { provideRouter } from '@angular/router'; | import { provideRouter } from '@angular/router'; | ||||||
|  | import localeDe from '@angular/common/locales/de'; | ||||||
|  | import localeCn from '@angular/common/locales/zh-Hans'; | ||||||
| 
 | 
 | ||||||
| import { routes } from './app.routes'; | import { routes } from './app.routes'; | ||||||
|  | import { registerLocaleData } from '@angular/common'; | ||||||
| 
 | 
 | ||||||
|  | registerLocaleData(localeDe, 'de-DE'); | ||||||
|  | registerLocaleData(localeCn, 'cn-CN'); | ||||||
| export const appConfig: ApplicationConfig = { | export const appConfig: ApplicationConfig = { | ||||||
|   providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)] |   providers: [provideZoneChangeDetection({ eventCoalescing: true }), provideRouter(routes)] | ||||||
| }; | }; | ||||||
|  |  | ||||||
							
								
								
									
										11
									
								
								src/currency.pipe.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/currency.pipe.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,11 @@ | ||||||
|  | import {Pipe, PipeTransform} from '@angular/core'; | ||||||
|  | 
 | ||||||
|  | @Pipe({ | ||||||
|  |   name: 'jkcurrency', | ||||||
|  |   standalone: true | ||||||
|  | }) | ||||||
|  | export class CurrencyPipe implements PipeTransform { | ||||||
|  |   transform(value: string): string { | ||||||
|  |     return value.replaceAll("L", "P"); | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										11
									
								
								src/text.pipe.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								src/text.pipe.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,11 @@ | ||||||
|  | import {Pipe, PipeTransform} from '@angular/core'; | ||||||
|  | 
 | ||||||
|  | @Pipe({ | ||||||
|  |   name: 'text', | ||||||
|  |   standalone: true | ||||||
|  | }) | ||||||
|  | export class TextPipe implements PipeTransform { | ||||||
|  |   transform(value: string): string { | ||||||
|  |     return value.replaceAll("L", "P"); | ||||||
|  |   } | ||||||
|  | } | ||||||
		Reference in a new issue