Language attribute on HTML tag

This commit is contained in:
lotte
2021-08-04 17:38:27 +02:00
parent 4292af4294
commit 43c03de0ad
2 changed files with 15 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ describe('LocaleService test suite', () => {
let spyOnSet;
let authService;
let routeService;
let document;
authService = jasmine.createSpyObj('AuthService', {
isAuthenticated: jasmine.createSpy('isAuthenticated'),
@@ -43,6 +44,7 @@ describe('LocaleService test suite', () => {
{ provide: CookieService, useValue: new CookieServiceMock() },
{ provide: AuthService, userValue: authService },
{ provide: RouteService, useValue: routeServiceStub },
{ provide: Document, useValue: document },
]
});
}));
@@ -52,7 +54,8 @@ describe('LocaleService test suite', () => {
translateService = TestBed.inject(TranslateService);
routeService = TestBed.inject(RouteService);
window = new NativeWindowRef();
service = new LocaleService(window, cookieService, translateService, authService, routeService);
document = { documentElement: { lang: 'en' } };
service = new LocaleService(window, cookieService, translateService, authService, routeService, document);
serviceAsAny = service;
spyOnGet = spyOn(cookieService, 'get');
spyOnSet = spyOn(cookieService, 'set');
@@ -114,6 +117,12 @@ describe('LocaleService test suite', () => {
expect(translateService.use).toHaveBeenCalledWith('es');
expect(service.saveLanguageCodeToCookie).toHaveBeenCalledWith('es');
});
it('should set the current language on the html tag', () => {
spyOn(service, 'getCurrentLanguageCode').and.returnValue('es');
service.setCurrentLanguageCode();
expect((service as any).document.documentElement.lang).toEqual('es');
});
});
describe('', () => {

View File

@@ -10,6 +10,7 @@ import { combineLatest, Observable, of as observableOf } from 'rxjs';
import { map, mergeMap, take } from 'rxjs/operators';
import { NativeWindowRef, NativeWindowService } from '../services/window.service';
import { RouteService } from '../services/route.service';
import { DOCUMENT } from '@angular/common';
export const LANG_COOKIE = 'dsLanguage';
@@ -38,7 +39,9 @@ export class LocaleService {
protected cookie: CookieService,
protected translate: TranslateService,
protected authService: AuthService,
protected routeService: RouteService) {
protected routeService: RouteService,
@Inject(DOCUMENT) private document: any
) {
}
/**
@@ -148,6 +151,7 @@ export class LocaleService {
}
this.translate.use(lang);
this.saveLanguageCodeToCookie(lang);
this.document.documentElement.lang = lang;
}
/**