Merge pull request #1287 from atmire/language-attribute-html-tag

Language attribute on HTML tag
This commit is contained in:
Tim Donohue
2021-08-05 16:53:33 -05:00
committed by GitHub
2 changed files with 15 additions and 2 deletions

View File

@@ -21,6 +21,7 @@ describe('LocaleService test suite', () => {
let spyOnSet; let spyOnSet;
let authService; let authService;
let routeService; let routeService;
let document;
authService = jasmine.createSpyObj('AuthService', { authService = jasmine.createSpyObj('AuthService', {
isAuthenticated: jasmine.createSpy('isAuthenticated'), isAuthenticated: jasmine.createSpy('isAuthenticated'),
@@ -43,6 +44,7 @@ describe('LocaleService test suite', () => {
{ provide: CookieService, useValue: new CookieServiceMock() }, { provide: CookieService, useValue: new CookieServiceMock() },
{ provide: AuthService, userValue: authService }, { provide: AuthService, userValue: authService },
{ provide: RouteService, useValue: routeServiceStub }, { provide: RouteService, useValue: routeServiceStub },
{ provide: Document, useValue: document },
] ]
}); });
})); }));
@@ -52,7 +54,8 @@ describe('LocaleService test suite', () => {
translateService = TestBed.inject(TranslateService); translateService = TestBed.inject(TranslateService);
routeService = TestBed.inject(RouteService); routeService = TestBed.inject(RouteService);
window = new NativeWindowRef(); 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; serviceAsAny = service;
spyOnGet = spyOn(cookieService, 'get'); spyOnGet = spyOn(cookieService, 'get');
spyOnSet = spyOn(cookieService, 'set'); spyOnSet = spyOn(cookieService, 'set');
@@ -114,6 +117,12 @@ describe('LocaleService test suite', () => {
expect(translateService.use).toHaveBeenCalledWith('es'); expect(translateService.use).toHaveBeenCalledWith('es');
expect(service.saveLanguageCodeToCookie).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('', () => { describe('', () => {

View File

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