Skip to content

Commit

Permalink
refatory with isLanguageActive
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasferreiralimax committed Jun 14, 2024
1 parent d7805ac commit c700f34
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 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)" routerLinkActive="actived">
<a [href]="getUrl(language)" (click)="changeLanguage(language)" [class.actived]="isLanguageActive(language)">
<img [src]="getImageUrl(language)" [alt]="language" />
{{language}}
</a>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { LanguageService } from '../../../services/language.service';
import { Location } from '@angular/common';

@Component({
selector: 'app-language',
Expand All @@ -9,14 +10,19 @@ import { LanguageService } from '../../../services/language.service';
export class AppLanguageComponent implements OnInit {
languages = ['pt-BR', 'en-US', 'es-ES', 'fr-FR', 'ru-RU', 'zh-CN'];

constructor(private languageService: LanguageService) {
constructor(private languageService: LanguageService, private location: Location) {
}

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

isLanguageActive(language: string): boolean {
const path = this.location.path();
return path.startsWith(`/${language.slice(0, 2)}`);
}

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

0 comments on commit c700f34

Please sign in to comment.