Skip to content
This repository was archived by the owner on Jun 23, 2025. It is now read-only.

Commit 56a858e

Browse files
authored
feat: google map language to default to LOCALE_ID (#1754)
the language of google maps should default to angular's LOCALE_ID, and not en-US
1 parent 57685f2 commit 56a858e

File tree

2 files changed

+46
-18
lines changed

2 files changed

+46
-18
lines changed

packages/core/services/maps-api-loader/lazy-maps-api-loader.spec.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { LOCALE_ID } from '@angular/core';
12
import { inject, TestBed } from '@angular/core/testing';
2-
33
import { DocumentRef, WindowRef } from '../../utils/browser-globals';
4-
54
import { GoogleMapsScriptProtocol, LAZY_MAPS_API_CONFIG, LazyMapsAPILoader, LazyMapsAPILoaderConfigLiteral } from './lazy-maps-api-loader';
65
import { MapsAPILoader } from './maps-api-loader';
76

@@ -32,6 +31,7 @@ describe('Service: LazyMapsAPILoader', () => {
3231
{provide: MapsAPILoader, useClass: LazyMapsAPILoader},
3332
{provide: WindowRef, useValue: windowRef},
3433
{provide: DocumentRef, useValue: documentRef},
34+
{provide: LOCALE_ID, useValue: 'en-US'},
3535
],
3636
});
3737
});
@@ -83,24 +83,51 @@ describe('Service: LazyMapsAPILoader', () => {
8383
{provide: MapsAPILoader, useClass: LazyMapsAPILoader},
8484
{provide: WindowRef, useValue: windowRef},
8585
{provide: DocumentRef, useValue: documentRef},
86+
{provide: LOCALE_ID, useValue: 'en-US'},
8687
{provide: LAZY_MAPS_API_CONFIG, useValue: lazyLoadingConf},
8788
],
8889
});
8990

90-
inject([MapsAPILoader], (loader: LazyMapsAPILoader) => {
91-
interface Script {
92-
src?: string;
93-
async?: boolean;
94-
defer?: boolean;
95-
type?: string;
96-
}
97-
const scriptElem: Script = {};
98-
(doc.createElement as jest.Mock).mockReturnValue(scriptElem);
91+
const loader: LazyMapsAPILoader = TestBed.get(MapsAPILoader);
9992

100-
loader.load();
101-
expect(doc.createElement).toHaveBeenCalled();
102-
expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js');
103-
expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem);
93+
interface Script {
94+
src?: string;
95+
async?: boolean;
96+
defer?: boolean;
97+
type?: string;
98+
}
99+
const scriptElem: Script = {};
100+
(doc.createElement as jest.Mock).mockReturnValue(scriptElem);
101+
102+
loader.load();
103+
expect(doc.createElement).toHaveBeenCalled();
104+
expect(scriptElem.src).toContain('http://maps.googleapis.com/maps/api/js');
105+
expect(doc.body.appendChild).toHaveBeenCalledWith(scriptElem);
106+
});
107+
108+
it('should load language based on locale', () => {
109+
TestBed.configureTestingModule({
110+
providers: [
111+
{provide: MapsAPILoader, useClass: LazyMapsAPILoader},
112+
{provide: WindowRef, useValue: windowRef},
113+
{provide: DocumentRef, useValue: documentRef},
114+
{provide: LOCALE_ID, useValue: 'es'},
115+
],
104116
});
117+
118+
const loader: LazyMapsAPILoader = TestBed.get(MapsAPILoader);
119+
interface Script {
120+
src?: string;
121+
async?: boolean;
122+
defer?: boolean;
123+
type?: string;
124+
}
125+
const scriptElem: Script = {};
126+
(doc.createElement as jest.Mock).mockReturnValue(scriptElem);
127+
128+
loader.load();
129+
expect(doc.createElement).toHaveBeenCalled();
130+
const url = new URL(scriptElem.src);
131+
expect(url.searchParams.get('language')).toEqual('es');
105132
});
106133
});

packages/core/services/maps-api-loader/lazy-maps-api-loader.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Inject, Injectable, InjectionToken, Optional } from '@angular/core';
1+
import { Inject, Injectable, InjectionToken, LOCALE_ID, Optional } from '@angular/core';
22

33
import { DocumentRef, WindowRef } from '../../utils/browser-globals';
44

@@ -87,7 +87,8 @@ export class LazyMapsAPILoader extends MapsAPILoader {
8787
protected readonly _SCRIPT_ID: string = 'agmGoogleMapsApiScript';
8888
protected readonly callbackName: string = `agmLazyMapsAPILoader`;
8989

90-
constructor(@Optional() @Inject(LAZY_MAPS_API_CONFIG) config: any = null, w: WindowRef, d: DocumentRef) {
90+
constructor(@Optional() @Inject(LAZY_MAPS_API_CONFIG) config: any = null, w: WindowRef, d: DocumentRef,
91+
@Inject(LOCALE_ID) private localeId: string) {
9192
super();
9293
this._config = config || {};
9394
this._windowRef = w;
@@ -161,7 +162,7 @@ export class LazyMapsAPILoader extends MapsAPILoader {
161162
channel: this._config.channel,
162163
libraries: this._config.libraries,
163164
region: this._config.region,
164-
language: this._config.language,
165+
language: this._config.language || this.localeId !== 'en-US' ? this.localeId : null,
165166
};
166167
const params: string = Object.keys(queryParams)
167168
.filter((k: string) => queryParams[k] != null)

0 commit comments

Comments
 (0)