Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1479: always translate to the last requested language #1480

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/core/lib/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ declare const window: Window;
export class TranslateService {
private loadingTranslations!: Observable<any>;
private pending: boolean = false;
private lastLangUseRequest?: string;
private _onTranslationChange: EventEmitter<TranslationChangeEvent> = new EventEmitter<TranslationChangeEvent>();
private _onLangChange: EventEmitter<LangChangeEvent> = new EventEmitter<LangChangeEvent>();
private _onDefaultLangChange: EventEmitter<DefaultLangChangeEvent> = new EventEmitter<DefaultLangChangeEvent>();
Expand Down Expand Up @@ -201,6 +202,9 @@ export class TranslateService {
* Changes the lang currently used
*/
public use(lang: string): Observable<any> {
// set the last requested language
this.lastLangUseRequest = lang

// don't change the language if the language given is already selected
if (lang === this.currentLang) {
return of(this.translations[lang]);
Expand Down Expand Up @@ -375,7 +379,8 @@ export class TranslateService {
if (this.pending) {
return this.loadingTranslations.pipe(
concatMap((res: any) => {
res = this.getParsedResult(res, key, interpolateParams);
// get the last requested language
res = this.getParsedResult(this.lastLangUseRequest ? this.translations[this.lastLangUseRequest] : res, key, interpolateParams);
return isObservable(res) ? res : of(res);
}),
);
Expand Down Expand Up @@ -464,9 +469,12 @@ export class TranslateService {
}

/**
* Changes the current lang
* Changes the current lang if it's the last language requested
*/
private changeLang(lang: string): void {
// set the language only if it's the last language requested
if (this.lastLangUseRequest !== lang) return;

this.currentLang = lang;
this.onLangChange.emit({lang: lang, translations: this.translations[lang]});

Expand Down
24 changes: 24 additions & 0 deletions packages/http-loader/tests/http-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,28 @@ describe('TranslateLoader', () => {
// mock response after the xhr request, otherwise it will be undefined
http.expectOne('/assets/i18n/en.json').flush({"TEST": "This is a test"});
});

it('should get last requested lang', (done: Function) => {
// request en, fr then en
translate.use('en');
translate.use('fr');
translate.use('en');
jest.spyOn(http, 'expectOne');

// this will request the translation from the backend because we use a static files loader for TranslateService
translate.get('TEST').subscribe((res: string) => {
expect(res).toEqual('This is a test');
done()
});


// mock response after the xhr request, otherwise it will be undefined
http.expectOne('/assets/i18n/en.json').flush({"TEST": "This is a test"});
// mock late fr response
setTimeout(()=> {
http.expectOne('/assets/i18n/fr.json').flush({"TEST": "This is a fr test"});
}, 10)

});
});