Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Commit f223bc4

Browse files
Blackbaud-PaulCrowderBobby Earl
authored and
Bobby Earl
committed
Added better caching strategy and fallback to non-region-specific languages (#226)
* Added better caching strategy and fallback to non-region-specific languages * Added unit test * Added comment for publishReplay(1).refCount() usage
1 parent dd93e97 commit f223bc4

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

runtime/i18n/resources.service.spec.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('Resources service', () => {
3333
}
3434
};
3535

36-
const providers = [
36+
const providers: any[] = [
3737
SkyAppAssetsService,
3838
SkyAppResourcesService,
3939
{
@@ -44,7 +44,7 @@ describe('Resources service', () => {
4444
provide: SkyAppAssetsService,
4545
useValue: {
4646
getUrl: (path) => {
47-
if (path.indexOf('en_AU') >= 0) {
47+
if (path.indexOf('en_AU') >= 0 || path.indexOf('es_MX') >= 0) {
4848
return undefined;
4949
}
5050

@@ -189,6 +189,33 @@ describe('Resources service', () => {
189189

190190
beforeEach(injectServices());
191191

192+
it('should fall back to the default locale if a blank locale is specified', (done) => {
193+
addTestResourceResponse();
194+
195+
currentLocale = '';
196+
197+
resources.getString('hi').subscribe((value) => {
198+
expect(value).toBe('hello');
199+
done();
200+
});
201+
});
202+
203+
it(
204+
'should fall back to the non-region-specific locale if the specified locale does not have ' +
205+
'corresponding resource file',
206+
(done) => {
207+
208+
backend.connections.subscribe((connection) => {
209+
expect(connection.request.url).toBe('https://example.com/locales/resources_es.json');
210+
done();
211+
});
212+
213+
currentLocale = 'es-MX';
214+
215+
resources.getString('hi').subscribe((value) => { });
216+
}
217+
);
218+
192219
it(
193220
'should fall back to the default locale if the specified locale does not have a ' +
194221
'corresponding resource file',

runtime/i18n/resources.service.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,30 @@ export class SkyAppResourcesService {
6060
this.resourcesObs = localeObs
6161
.switchMap((localeInfo) => {
6262
let obs: Observable<any>;
63+
let resourcesUrl: string;
6364

64-
const resourcesUrl =
65-
this.getUrlForLocale(localeInfo.locale) ||
66-
this.getUrlForLocale(DEFAULT_LOCALE);
65+
const locale = localeInfo.locale;
66+
67+
if (locale) {
68+
resourcesUrl =
69+
this.getUrlForLocale(locale) ||
70+
// Try falling back to the non-region-specific language.
71+
this.getUrlForLocale(locale.substr(0, 2));
72+
}
73+
74+
// Finally fall back to the default locale.
75+
resourcesUrl = resourcesUrl || this.getUrlForLocale(DEFAULT_LOCALE);
6776

6877
if (resourcesUrl) {
6978
obs = this.httpObs[resourcesUrl] || this.http
7079
.get(resourcesUrl)
71-
.share()
80+
/* tslint:disable max-line-length */
81+
// publishReplay(1).refCount() will ensure future subscribers to
82+
// this observable will use a cached result.
83+
// https://stackoverflow.com/documentation/rxjs/8247/common-recipes/26490/caching-http-responses#t=201612161544428695958
84+
/* tslint:enable max-line-length */
85+
.publishReplay(1)
86+
.refCount()
7287
.catch(() => {
7388
// The resource file for the specified locale failed to load;
7489
// fall back to the default locale if it differs from the specified

0 commit comments

Comments
 (0)