mirror of
https://github.com/mempool/mempool.git
synced 2025-03-13 11:36:07 +01:00
Suggested changes for Simon's PR #1113
This commit is contained in:
parent
4ca730697c
commit
efffd1a929
2 changed files with 21 additions and 17 deletions
|
@ -1,3 +1,4 @@
|
|||
import { DOCUMENT } from '@angular/common';
|
||||
import { ChangeDetectionStrategy, Component, Inject, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { languages } from 'src/app/app.constants';
|
||||
|
@ -14,6 +15,7 @@ export class LanguageSelectorComponent implements OnInit {
|
|||
languages = languages;
|
||||
|
||||
constructor(
|
||||
@Inject(DOCUMENT) private document: Document,
|
||||
private formBuilder: FormBuilder,
|
||||
private languageService: LanguageService,
|
||||
) { }
|
||||
|
@ -26,6 +28,9 @@ export class LanguageSelectorComponent implements OnInit {
|
|||
}
|
||||
|
||||
changeLanguage() {
|
||||
this.languageService.setLanguage(this.languageForm.get('language').value);
|
||||
const newLang = this.languageForm.get('language').value;
|
||||
this.languageService.setLanguage(newLang);
|
||||
const rawUrlPath = this.languageService.stripLanguageFromUrl(null);
|
||||
this.document.location.href = (newLang !== 'en' ? `/${newLang}` : '') + rawUrlPath;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import { DOCUMENT } from '@angular/common';
|
||||
import { Inject, Injectable } from '@angular/core';
|
||||
import { DOCUMENT, getLocaleId } from '@angular/common';
|
||||
import { LOCALE_ID, Inject, Injectable } from '@angular/core';
|
||||
import { languages } from 'src/app/app.constants';
|
||||
import { RelativeUrlPipe } from '../shared/pipes/relative-url/relative-url.pipe';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
|
@ -11,31 +10,31 @@ export class LanguageService {
|
|||
private languages = languages;
|
||||
constructor(
|
||||
@Inject(DOCUMENT) private document: Document,
|
||||
private relativeUrlPipe: RelativeUrlPipe,
|
||||
@Inject(LOCALE_ID) private locale: string,
|
||||
) { }
|
||||
|
||||
getLanguage(): string {
|
||||
return this.language;
|
||||
return getLocaleId(this.locale).substring(0, 2);
|
||||
}
|
||||
|
||||
getLanguageForUrl() {
|
||||
return this.language === 'en' ? '' : '/' + this.language;
|
||||
}
|
||||
|
||||
setLocalLanguage() {
|
||||
stripLanguageFromUrl(urlPath: string) {
|
||||
let rawUrlPath = urlPath ? urlPath : document.location.pathname;
|
||||
const urlLanguage = this.document.location.pathname.split('/')[1];
|
||||
if (this.languages.map((lang) => lang.code).indexOf(urlLanguage) > -1) {
|
||||
this.language = urlLanguage;
|
||||
} else {
|
||||
this.language = 'en';
|
||||
if (this.languages.map((lang) => lang.code).indexOf(urlLanguage) != -1) {
|
||||
rawUrlPath = rawUrlPath.substring(3);
|
||||
}
|
||||
return rawUrlPath;
|
||||
}
|
||||
|
||||
getLanguageForUrl(): string {
|
||||
let lang = this.getLanguage();
|
||||
return lang === 'en' ? '' : '/' + lang;
|
||||
}
|
||||
|
||||
setLanguage(language: string): void {
|
||||
this.language = language;
|
||||
try {
|
||||
document.cookie = `lang=${language}; expires=Thu, 18 Dec 2050 12:00:00 UTC; path=/`;
|
||||
} catch (e) { }
|
||||
|
||||
this.document.location.href = this.relativeUrlPipe.transform(`/${language}/`);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue