admin管理员组文章数量:1278789
We have an Angular application which has two languages. The default is German and the other one is English. We are using ngx-translate
as the translateService
.
When you refresh the browser, the application switches back to the default language.
The switchLang()
function gets called in our navigation bar:
<li class="nav-item">
<a class="nav-link" id="switchLang" (click)="switchLang()">
<span>
<i class="fa fa-globe" aria-hidden="true"></i>
<span>{{'switch-lang' | translate}}</span>
</span>
</a>
</li>
the ponent.ts:
switchLang() {
this.languageService.switchLanguage();
}
And the language service:
import { Injectable } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
@Injectable({ providedIn: 'root' })
export class LanguageService {
private language = 'de';
constructor(private translateService: TranslateService) { }
getCurrentLanguage() {
return this.language;
}
getLocale() {
if (this.language === 'en') {
return 'en_US';
} else {
return 'de_DE';
}
}
switchLanguage() {
if (this.language === 'en') {
this.language = 'de';
} else {
this.language = 'en';
}
this.translateService.use(this.language);
}
}
The translateService
is the ngx-translate.
We have an Angular application which has two languages. The default is German and the other one is English. We are using ngx-translate
as the translateService
.
When you refresh the browser, the application switches back to the default language.
The switchLang()
function gets called in our navigation bar:
<li class="nav-item">
<a class="nav-link" id="switchLang" (click)="switchLang()">
<span>
<i class="fa fa-globe" aria-hidden="true"></i>
<span>{{'switch-lang' | translate}}</span>
</span>
</a>
</li>
the ponent.ts:
switchLang() {
this.languageService.switchLanguage();
}
And the language service:
import { Injectable } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
@Injectable({ providedIn: 'root' })
export class LanguageService {
private language = 'de';
constructor(private translateService: TranslateService) { }
getCurrentLanguage() {
return this.language;
}
getLocale() {
if (this.language === 'en') {
return 'en_US';
} else {
return 'de_DE';
}
}
switchLanguage() {
if (this.language === 'en') {
this.language = 'de';
} else {
this.language = 'en';
}
this.translateService.use(this.language);
}
}
The translateService
is the ngx-translate.
- 2 Well you're saving it nowhere. When you refresh the page the whole application repiles. – Roberto Zvjerković Commented Apr 12, 2019 at 8:07
- I thought the ngx-translate has a built-in method for that. I think the localStorage is the direction – Patrik Alexits Commented Apr 12, 2019 at 8:24
-
1
i also remend you to initialize your default lang by using the browser langage.
const userLang = navigator.language || navigator.userLanguage;
– Yanis-git Commented Apr 30, 2019 at 7:51
3 Answers
Reset to default 7This is the right behavior. You can use localStorage (or other place) to store the selected language.
You can use localStorage to store the value in device memory, the following is an example
// function select language
selectLanguage(i: number) {
this.lag = this.languages[i];
this.translateService.use(this.languages[i].title.toLowerCase());
localStorage.setItem("language",this.languages[i].title.toLowerCase());
}
Angular service lives entirely in memory. If you want to add a persistence you should save current value to the localStorage on a language change or save this setting to the server
本文标签: javascriptAngular 7 ngxtranslate doesn39t remember the language after refreshStack Overflow
版权声明:本文标题:javascript - Angular 7 ngx-translate doesn't remember the language after refresh - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741240834a2363942.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论