diff --git a/packages/core/services/maps-api-loader/lazy-maps-api-loader.spec.ts b/packages/core/services/maps-api-loader/lazy-maps-api-loader.spec.ts index ba2d17cc3..9f0df785b 100644 --- a/packages/core/services/maps-api-loader/lazy-maps-api-loader.spec.ts +++ b/packages/core/services/maps-api-loader/lazy-maps-api-loader.spec.ts @@ -1,7 +1,6 @@ +import { LOCALE_ID } from '@angular/core'; import { inject, TestBed } from '@angular/core/testing'; - import { DocumentRef, WindowRef } from '../../utils/browser-globals'; - import { GoogleMapsScriptProtocol, LAZY_MAPS_API_CONFIG, LazyMapsAPILoader, LazyMapsAPILoaderConfigLiteral } from './lazy-maps-api-loader'; import { MapsAPILoader } from './maps-api-loader'; @@ -32,6 +31,7 @@ describe('Service: LazyMapsAPILoader', () => { {provide: MapsAPILoader, useClass: LazyMapsAPILoader}, {provide: WindowRef, useValue: windowRef}, {provide: DocumentRef, useValue: documentRef}, + {provide: LOCALE_ID, useValue: 'en-US'}, ], }); }); @@ -83,24 +83,51 @@ describe('Service: LazyMapsAPILoader', () => { {provide: MapsAPILoader, useClass: LazyMapsAPILoader}, {provide: WindowRef, useValue: windowRef}, {provide: DocumentRef, useValue: documentRef}, + {provide: LOCALE_ID, useValue: 'en-US'}, {provide: LAZY_MAPS_API_CONFIG, useValue: lazyLoadingConf}, ], }); - inject([MapsAPILoader], (loader: LazyMapsAPILoader) => { - interface Script { - src?: string; - async?: boolean; - defer?: boolean; - type?: string; - } - const scriptElem: Script = {}; - (doc.createElement as jest.Mock).mockReturnValue(scriptElem); + const loader: LazyMapsAPILoader = TestBed.get(MapsAPILoader); - loader.load(); - expect(doc.createElement).toHaveBeenCalled(); - expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js'); - expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem); + interface Script { + src?: string; + async?: boolean; + defer?: boolean; + type?: string; + } + const scriptElem: Script = {}; + (doc.createElement as jest.Mock).mockReturnValue(scriptElem); + + loader.load(); + expect(doc.createElement).toHaveBeenCalled(); + expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js'); + expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem); + }); + + it('should load language based on locale', () => { + TestBed.configureTestingModule({ + providers: [ + {provide: MapsAPILoader, useClass: LazyMapsAPILoader}, + {provide: WindowRef, useValue: windowRef}, + {provide: DocumentRef, useValue: documentRef}, + {provide: LOCALE_ID, useValue: 'es'}, + ], }); + + const loader: LazyMapsAPILoader = TestBed.get(MapsAPILoader); + interface Script { + src?: string; + async?: boolean; + defer?: boolean; + type?: string; + } + const scriptElem: Script = {}; + (doc.createElement as jest.Mock).mockReturnValue(scriptElem); + + loader.load(); + expect(doc.createElement).toHaveBeenCalled(); + const url = new URL(scriptElem.src); + expect(url.searchParams.get('language')).toEqual('es'); }); }); diff --git a/packages/core/services/maps-api-loader/lazy-maps-api-loader.ts b/packages/core/services/maps-api-loader/lazy-maps-api-loader.ts index e86fe41ff..a131b7563 100644 --- a/packages/core/services/maps-api-loader/lazy-maps-api-loader.ts +++ b/packages/core/services/maps-api-loader/lazy-maps-api-loader.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable, InjectionToken, Optional } from '@angular/core'; +import { Inject, Injectable, InjectionToken, LOCALE_ID, Optional } from '@angular/core'; import { DocumentRef, WindowRef } from '../../utils/browser-globals'; @@ -87,7 +87,8 @@ export class LazyMapsAPILoader extends MapsAPILoader { protected readonly _SCRIPT_ID: string = 'agmGoogleMapsApiScript'; protected readonly callbackName: string = `agmLazyMapsAPILoader`; - constructor(@Optional() @Inject(LAZY_MAPS_API_CONFIG) config: any = null, w: WindowRef, d: DocumentRef) { + constructor(@Optional() @Inject(LAZY_MAPS_API_CONFIG) config: any = null, w: WindowRef, d: DocumentRef, + @Inject(LOCALE_ID) private localeId: string) { super(); this._config = config || {}; this._windowRef = w; @@ -161,7 +162,7 @@ export class LazyMapsAPILoader extends MapsAPILoader { channel: this._config.channel, libraries: this._config.libraries, region: this._config.region, - language: this._config.language, + language: this._config.language || this.localeId !== 'en-US' ? this.localeId : null, }; const params: string = Object.keys(queryParams) .filter((k: string) => queryParams[k] != null)