Skip to content

Commit

Permalink
use routerLinkActive in language
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasferreiralimax committed Jun 14, 2024
1 parent b7cbae5 commit d7805ac
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<section class="language">
@for (language of languages; track language) {
<a [href]="getUrl(language)" (click)="changeLanguage(language)" [ngClass]="{ 'actived': isLanguageValid(language) }">
<a [href]="getUrl(language)" (click)="changeLanguage(language)" routerLinkActive="actived">
<img [src]="getImageUrl(language)" [alt]="language" />
{{language}}
</a>
Expand Down
21 changes: 1 addition & 20 deletions src/app/components/base/app-language/app-language.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { LanguageService } from '../../../services/language.service';
import { ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';

@Component({
selector: 'app-language',
Expand All @@ -10,32 +8,15 @@ import { filter } from 'rxjs/operators';
})
export class AppLanguageComponent implements OnInit {
languages = ['pt-BR', 'en-US', 'es-ES', 'fr-FR', 'ru-RU', 'zh-CN'];
codeLanguage: string;

constructor(private languageService: LanguageService, private router: Router, private route: ActivatedRoute) {
// Monitora as mudanças de rota para atualizar o código de idioma
this.router.events.pipe(
filter(event => event instanceof NavigationEnd)
).subscribe(() => {
// Obtém o código de idioma da rota atual
const segments = this.route.snapshot.url.map(segment => segment.path);
if (segments.length > 0) {
this.codeLanguage = segments[0]; // Assume que o código de idioma está no primeiro segmento
} else {
this.codeLanguage = 'pt'; // Código de idioma padrão se não estiver presente na URL
}
});
constructor(private languageService: LanguageService) {
}

ngOnInit(): void {
const lang = this.languageService.getLanguage();
this.languageService.setLanguage(lang);
}

isLanguageValid(code: string): boolean {
return this.codeLanguage == code.slice(0, 2);
}

getUrl(language: string): string {
return `/${language.slice(0, 2)}/`;
}
Expand Down

0 comments on commit d7805ac

Please sign in to comment.