-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
625 additions
and
178 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,18 @@ | ||
import { Component } from '@angular/core'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
import { ChangeDetectionStrategy, Component } from '@angular/core'; | ||
import { RouterOutlet } from '@angular/router'; | ||
import { NavMenuComponent } from './nav-menu/nav-menu.component'; | ||
import enTranslations from '../locales/en.json'; | ||
import zhHantTranslations from '../locales/zh-Hant.json'; | ||
import { TranslationService } from './services/translation.service'; | ||
|
||
@Component({ | ||
selector: 'app-root', | ||
templateUrl: './app.component.html', | ||
styleUrls: ['./app.component.css'], | ||
imports: [NavMenuComponent, RouterOutlet] | ||
imports: [NavMenuComponent, RouterOutlet], | ||
standalone: true, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class AppComponent { | ||
title = document.title; | ||
constructor(public translate: TranslateService) { | ||
translate.setTranslation('en', enTranslations); | ||
translate.setTranslation('zh-Hant', zhHantTranslations); | ||
translate.addLangs(['en', 'zh-Hant']); | ||
translate.setDefaultLang('en'); | ||
const locale = localStorage.getItem('locale'); | ||
if (locale !== null) { | ||
translate.use(locale); | ||
} else { | ||
const browserLang = translate.getBrowserCultureLang(); | ||
translate.use(browserLang?.match(/zh/) ? 'zh-Hant' : 'en'); | ||
} | ||
} | ||
|
||
constructor(private translationService: TranslationService) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,33 @@ | ||
<h1>{{ "Counter" | translate }}</h1> | ||
<div class="counter-container"> | ||
<h1>{{ "Counter" | translate }}</h1> | ||
|
||
<p aria-live="polite"> | ||
{{ "Current count" | translate }}: <strong>{{ currentCount }}</strong> | ||
</p> | ||
<p aria-live="polite" class="mt-3"> | ||
{{ "Current count" | translate }}: <strong>{{ currentCount() }}</strong> | ||
</p> | ||
|
||
<button class="btn btn-primary" (click)="incrementCounter()"> | ||
{{ "Increment" | translate }} | ||
</button> | ||
<div class="btn-group" [attr.aria-label]="'COUNTER.CONTROLS' | translate"> | ||
<button | ||
class="btn btn-primary" | ||
(click)="incrementCounter()" | ||
[attr.aria-label]="'COUNTER.INCREMENT_ARIA' | translate" | ||
> | ||
<i class="bi bi-plus-lg" aria-hidden="true"></i> | ||
{{ "Increment" | translate }} | ||
</button> | ||
|
||
<button | ||
class="btn btn-secondary ms-2" | ||
(click)="resetCounter()" | ||
[attr.aria-label]="'COUNTER.RESET_ARIA' | translate" | ||
> | ||
<i class="bi bi-arrow-counterclockwise" aria-hidden="true"></i> | ||
{{ "COUNTER.RESET" | translate }} | ||
</button> | ||
</div> | ||
|
||
<div class="mt-3"> | ||
<small class="text-muted"> | ||
{{ "COUNTER.KEYBOARD_HINT" | translate }} | ||
</small> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,34 @@ | ||
import { Component } from '@angular/core'; | ||
import { ChangeDetectionStrategy, Component, HostListener, computed, signal } from '@angular/core'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
import { NgIf } from '@angular/common'; | ||
|
||
@Component({ | ||
selector: 'app-counter', | ||
templateUrl: './counter.component.html', | ||
styleUrls: ['./counter.component.css'], | ||
imports: [TranslateModule] | ||
imports: [ | ||
TranslateModule, | ||
NgIf | ||
], | ||
standalone: true, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class CounterComponent { | ||
public currentCount = 0; | ||
private readonly count = signal(0); | ||
readonly currentCount = computed(() => this.count()); | ||
|
||
public incrementCounter() : void { | ||
this.currentCount++; | ||
@HostListener('window:keydown.space', ['$event']) | ||
@HostListener('window:keydown.enter', ['$event']) | ||
onKeyPress(event: KeyboardEvent): void { | ||
event.preventDefault(); | ||
this.incrementCounter(); | ||
} | ||
|
||
incrementCounter(): void { | ||
this.count.update(value => value + 1); | ||
} | ||
|
||
resetCounter(): void { | ||
this.count.set(0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,63 @@ | ||
import { Component, Input } from '@angular/core'; | ||
import { TranslateService, TranslationChangeEvent } from '@ngx-translate/core'; | ||
import { ChangeDetectionStrategy, Component, Input, signal } from '@angular/core'; | ||
import { ClickOutsideDirective } from '../click-outside.directive'; | ||
import { NgClass } from '@angular/common'; | ||
import { AsyncPipe, NgClass } from '@angular/common'; | ||
import { RouterLink, RouterLinkActive } from '@angular/router'; | ||
import { TranslationService } from '../services/translation.service'; | ||
import { TranslateModule } from '@ngx-translate/core'; | ||
|
||
type Language = 'en' | 'zh-Hant'; | ||
|
||
@Component({ | ||
selector: 'app-nav-menu', | ||
templateUrl: './nav-menu.component.html', | ||
styleUrls: ['./nav-menu.component.css'], | ||
imports: [RouterLink, NgClass, RouterLinkActive, ClickOutsideDirective] | ||
imports: [ | ||
RouterLink, | ||
NgClass, | ||
RouterLinkActive, | ||
ClickOutsideDirective, | ||
AsyncPipe, | ||
TranslateModule | ||
], | ||
standalone: true, | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class NavMenuComponent { | ||
@Input() title: string | undefined; | ||
|
||
isCollapsed = true; | ||
isExpanded = false; | ||
private readonly isCollapsed = signal(true); | ||
private readonly isExpanded = signal(false); | ||
|
||
readonly currentLang$ = this.translationService.currentLang$; | ||
|
||
constructor(private translationService: TranslationService) {} | ||
|
||
constructor(private translate: TranslateService) { | ||
translate.onLangChange.subscribe((event: TranslationChangeEvent) => { | ||
localStorage.setItem('locale', event.lang); | ||
}); | ||
get collapsed(): boolean { | ||
return this.isCollapsed(); | ||
} | ||
|
||
get expanded(): boolean { | ||
return this.isExpanded(); | ||
} | ||
|
||
toggleCollapsed(): void { | ||
this.isCollapsed = !this.isCollapsed; | ||
this.isCollapsed.update(value => !value); | ||
} | ||
|
||
toggleExpanded(): void { | ||
this.isExpanded = !this.isExpanded; | ||
this.isExpanded.update(value => !value); | ||
} | ||
|
||
onOutsideClick(): void { | ||
this.isExpanded = false; | ||
this.isExpanded.set(false); | ||
} | ||
|
||
isCurrentLanguage(pattern: string): boolean { | ||
return new RegExp(pattern).test(this.translate.currentLang); | ||
return new RegExp(pattern).test(this.translationService.instant('LANGUAGE')); | ||
} | ||
|
||
switchLanguage = (lang: string): void => { | ||
this.translate.use(lang); | ||
this.isExpanded = false; | ||
}; | ||
switchLanguage(lang: Language): void { | ||
this.translationService.setLanguage(lang); | ||
this.isExpanded.set(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { TranslateService } from '@ngx-translate/core'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class TranslationService { | ||
private currentLang = new BehaviorSubject<string>('en'); | ||
currentLang$ = this.currentLang.asObservable(); | ||
|
||
constructor(private translate: TranslateService) { | ||
this.initializeLanguage(); | ||
} | ||
|
||
private async initializeLanguage() { | ||
// Set available languages | ||
this.translate.addLangs(['en', 'zh-Hant']); | ||
this.translate.setDefaultLang('en'); | ||
|
||
// Load saved language or detect browser language | ||
const savedLang = localStorage.getItem('locale'); | ||
const langToUse = savedLang || this.getBrowserLanguage(); | ||
|
||
await this.setLanguage(langToUse); | ||
} | ||
|
||
private getBrowserLanguage(): string { | ||
const browserLang = this.translate.getBrowserCultureLang(); | ||
return browserLang?.match(/zh/) ? 'zh-Hant' : 'en'; | ||
} | ||
|
||
async setLanguage(lang: string): Promise<void> { | ||
if (!this.translate.getLangs().includes(lang)) { | ||
lang = 'en'; | ||
} | ||
|
||
// Dynamically import translations | ||
try { | ||
const translations = await import(`../../locales/${lang}.json`); | ||
this.translate.setTranslation(lang, translations.default); | ||
await this.translate.use(lang).toPromise(); | ||
localStorage.setItem('locale', lang); | ||
this.currentLang.next(lang); | ||
} catch (error) { | ||
console.error(`Failed to load translations for ${lang}`, error); | ||
// Fallback to English | ||
if (lang !== 'en') { | ||
await this.setLanguage('en'); | ||
} | ||
} | ||
} | ||
|
||
instant(key: string, params?: object): string { | ||
return this.translate.instant(key, params); | ||
} | ||
} |
Oops, something went wrong.