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

Added better caching strategy and fallback to non-region-specific languages #226

Merged
merged 5 commits into from
Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions runtime/i18n/resources.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Resources service', () => {
}
};

const providers = [
const providers: any[] = [
SkyAppAssetsService,
SkyAppResourcesService,
{
Expand All @@ -44,7 +44,7 @@ describe('Resources service', () => {
provide: SkyAppAssetsService,
useValue: {
getUrl: (path) => {
if (path.indexOf('en_AU') >= 0) {
if (path.indexOf('en_AU') >= 0 || path.indexOf('es_MX') >= 0) {
return undefined;
}

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

beforeEach(injectServices());

it('should fall back to the default locale if a blank locale is specified', (done) => {
addTestResourceResponse();

currentLocale = '';

resources.getString('hi').subscribe((value) => {
expect(value).toBe('hello');
done();
});
});

it(
'should fall back to the non-region-specific locale if the specified locale does not have ' +
'corresponding resource file',
(done) => {

backend.connections.subscribe((connection) => {
expect(connection.request.url).toBe('https://example.com/locales/resources_es.json');
done();
});

currentLocale = 'es-MX';

resources.getString('hi').subscribe((value) => { });
}
);

it(
'should fall back to the default locale if the specified locale does not have a ' +
'corresponding resource file',
Expand Down
18 changes: 14 additions & 4 deletions runtime/i18n/resources.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,25 @@ export class SkyAppResourcesService {
this.resourcesObs = localeObs
.switchMap((localeInfo) => {
let obs: Observable<any>;
let resourcesUrl: string;

const resourcesUrl =
this.getUrlForLocale(localeInfo.locale) ||
this.getUrlForLocale(DEFAULT_LOCALE);
const locale = localeInfo.locale;

if (locale) {
resourcesUrl =
this.getUrlForLocale(locale) ||
// Try falling back to the non-region-specific language.
this.getUrlForLocale(locale.substr(0, 2));
}

// Finally fall back to the default locale.
resourcesUrl = resourcesUrl || this.getUrlForLocale(DEFAULT_LOCALE);

if (resourcesUrl) {
obs = this.httpObs[resourcesUrl] || this.http
.get(resourcesUrl)
.share()
.publishReplay(1)
.refCount()
.catch(() => {
// The resource file for the specified locale failed to load;
// fall back to the default locale if it differs from the specified
Expand Down