This repository was archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Added resource pipe and service for libraries #12
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
93be7ee
Added resource pipe and service for libraries
Blackbaud-SteveBrush cc37bb9
Minor adjustments
Blackbaud-SteveBrush 02594f0
Minor adjustments
Blackbaud-SteveBrush bd3604e
Modifications
Blackbaud-SteveBrush f345030
Updates to accommodate changes
Blackbaud-SteveBrush 5c9fc79
Added unit tests
Blackbaud-SteveBrush a3b510d
Removed navigator.language
Blackbaud-SteveBrush 5bfbca4
Removed spec
Blackbaud-SteveBrush File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './sample-resources.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { | ||
SkyAppLocaleInfo, | ||
SkyLibResourcesProvider | ||
} from '../public'; | ||
|
||
export class SkySampleResourcesProvider implements SkyLibResourcesProvider { | ||
public getString: (localeInfo: SkyAppLocaleInfo, name: string) => string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<table> | ||
<tr> | ||
<td>Pipe result:</td> | ||
<td>{{ 'greeting' | skyLibResources }}</td> | ||
</tr> | ||
<tr> | ||
<td>Default greeting (service):</td> | ||
<td>{{ defaultGreeting }}</td> | ||
</tr> | ||
<tr> | ||
<td>Localized greeting (service):</td> | ||
<td>{{ localizedGreeting }}</td> | ||
</tr> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
OnInit | ||
} from '@angular/core'; | ||
|
||
import { | ||
SkyLibResourcesService | ||
} from '../public'; | ||
|
||
@Component({ | ||
selector: 'sky-sample-resources', | ||
templateUrl: './sample-resources.component.html', | ||
changeDetection: ChangeDetectionStrategy.OnPush | ||
}) | ||
export class SkySampleResourcesComponent implements OnInit { | ||
public defaultGreeting: string; | ||
public localizedGreeting: string; | ||
|
||
constructor( | ||
private resourcesService: SkyLibResourcesService | ||
) { } | ||
|
||
public ngOnInit(): void { | ||
this.defaultGreeting = this.resourcesService.getDefaultString('greeting'); | ||
|
||
this.resourcesService.getString('greeting') | ||
.subscribe((localizedValue: string) => { | ||
this.localizedGreeting = localizedValue; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { | ||
NgModule | ||
} from '@angular/core'; | ||
|
||
import { | ||
SKY_LIB_RESOURCES_PROVIDERS, | ||
SkyI18nModule | ||
} from '../public'; | ||
|
||
import { | ||
SkySampleResourcesProvider | ||
} from './sample-resources-provider'; | ||
|
||
@NgModule({ | ||
exports: [ | ||
SkyI18nModule | ||
], | ||
providers: [{ | ||
provide: SKY_LIB_RESOURCES_PROVIDERS, | ||
useClass: SkySampleResourcesProvider, | ||
multi: true | ||
}] | ||
}) | ||
export class SkySampleResourcesModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<sky-sample-resources></sky-sample-resources> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { | ||
Injectable | ||
} from '@angular/core'; | ||
|
||
import { | ||
SkyAppLocaleInfo | ||
} from './locale-info'; | ||
|
||
@Injectable() | ||
export abstract class SkyLibResourcesProvider { | ||
|
||
public getString: (localeInfo: SkyAppLocaleInfo, name: string) => string; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/app/public/modules/i18n/lib-resources-providers-token.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { | ||
InjectionToken | ||
} from '@angular/core'; | ||
|
||
import { | ||
SkyLibResourcesProvider | ||
} from './lib-resources-provider'; | ||
|
||
export const SKY_LIB_RESOURCES_PROVIDERS = | ||
new InjectionToken<SkyLibResourcesProvider>('SKY_LIB_RESOURCES_PROVIDERS'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
ChangeDetectorRef, | ||
Pipe, | ||
PipeTransform | ||
} from '@angular/core'; | ||
|
||
import { | ||
Observable | ||
} from 'rxjs/Observable'; | ||
|
||
import { | ||
SkyLibResourcesService | ||
} from './lib-resources.service'; | ||
|
||
@Pipe({ | ||
name: 'skyLibResources', | ||
pure: false | ||
}) | ||
export class SkyLibResourcesPipe implements PipeTransform { | ||
private resourceCache: {[key: string]: any} = {}; | ||
|
||
constructor( | ||
private changeDetector: ChangeDetectorRef, | ||
private resourcesService: SkyLibResourcesService | ||
) { } | ||
|
||
public transform(name: string, ...args: any[]): Observable<string> { | ||
const cacheKey = name + JSON.stringify(args); | ||
|
||
if (!(cacheKey in this.resourceCache)) { | ||
this.resourcesService.getString(name, args) | ||
.subscribe((value: string) => { | ||
this.resourceCache[cacheKey] = value; | ||
this.changeDetector.markForCheck(); | ||
}); | ||
} | ||
|
||
return this.resourceCache[cacheKey]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { | ||
Inject, | ||
Injectable | ||
} from '@angular/core'; | ||
|
||
import { | ||
Observable | ||
} from 'rxjs/Observable'; | ||
|
||
import 'rxjs/add/observable/merge'; | ||
|
||
import { | ||
SkyAppFormat | ||
} from '@skyux/core'; | ||
|
||
import { | ||
SkyAppHostLocaleProvider | ||
} from './host-locale-provider'; | ||
|
||
import { | ||
SkyAppLocaleInfo | ||
} from './locale-info'; | ||
|
||
import { | ||
SkyAppLocaleProvider | ||
} from './locale-provider'; | ||
|
||
import { | ||
SKY_LIB_RESOURCES_PROVIDERS | ||
} from './lib-resources-providers-token'; | ||
|
||
import { | ||
SkyLibResourcesProvider | ||
} from './lib-resources-provider'; | ||
|
||
@Injectable() | ||
export class SkyLibResourcesService { | ||
private format = new SkyAppFormat(); | ||
|
||
constructor( | ||
@Inject(SkyAppHostLocaleProvider) private localeProvider: SkyAppLocaleProvider, | ||
@Inject(SKY_LIB_RESOURCES_PROVIDERS) private providers: SkyLibResourcesProvider[] | ||
) { } | ||
|
||
public getString(name: string, ...args: any[]): Observable<string> { | ||
return this.localeProvider | ||
.getLocaleInfo() | ||
.map((info) => this.getStringForLocale(info, name, args)); | ||
} | ||
|
||
public getDefaultString(name: string, ...args: any[]): string { | ||
return this.getStringForLocale({ locale: 'en_US' }, name, args); | ||
} | ||
|
||
private getStringForLocale( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove |
||
info: SkyAppLocaleInfo, | ||
name: string, ...args: any[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing line break |
||
): string { | ||
for (const provider of this.providers) { | ||
const s = provider.getString(info, name); | ||
if (s) { | ||
return this.format.formatText(s, args); | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
{} | ||
{ | ||
"greeting": { | ||
"message": "Hello", | ||
"_description": "A simple greeting." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"greeting": { | ||
"message": "Bonjour", | ||
"_description": "A simple greeting." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this.