Skip to content

Commit b51359e

Browse files
committed
feat(directive): read nested property
1 parent 561cba4 commit b51359e

File tree

5 files changed

+14
-14
lines changed

5 files changed

+14
-14
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ This is the recommended approach. It's DRY and efficient, as it creates one subs
157157
</ng-template>
158158
```
159159

160-
If you are using `shared` strategy (default in the next major release), you can set `context` in your structural directive to get translations from a particular nested (including deeply nested) property.
160+
If you are using `shared` strategy (default in the next major release), you can use `read` property in your structural directive to get translations of a particular nested (including deeply nested) property.
161161

162162
Given this translation object:
163163

@@ -177,13 +177,13 @@ Given this translation object:
177177
you can do
178178

179179
```html
180-
<ng-container *transloco="let t; context: 'dashboard'">
180+
<ng-container *transloco="let t; read: 'dashboard'">
181181
<h1>{{ t.title }}</h1>
182182
<p>{{ t.desc }}</p>
183183
</ng-container>
184184
```
185185

186-
without having to repeat the `dashboard` key.
186+
without having to repeat the `dashboard` key in each translation.
187187

188188
### Using the Attribute Directive
189189

projects/ngneat/transloco/src/lib/tests/transloco.directive.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('TranslocoDirective', () => {
5151
expect(host.queryHost('div')).toHaveText('Admin Lazy spanish');
5252
}
5353

54-
function testTranslationWithContext(host: SpectatorWithHost<TranslocoDirective, HostComponent>) {
54+
function testTranslationWithRead(host: SpectatorWithHost<TranslocoDirective, HostComponent>) {
5555
const service = host.get<TranslocoService>(TranslocoService);
5656
initScopeTest(host, service);
5757
expect(host.queryHost('div')).toHaveText('Title english');
@@ -196,9 +196,9 @@ describe('TranslocoDirective', () => {
196196
expect(host.queryHostAll('p')[1]).toHaveText('a.b.c value english');
197197
}));
198198

199-
it('should get translation of a nested property using context', fakeAsync(() => {
200-
host = createHost(`<section *transloco="let t; context: 'nested'"><div>{{t.title}}</div></section>`, false);
201-
testTranslationWithContext(host);
199+
it('should get translation of a nested property using read', fakeAsync(() => {
200+
host = createHost(`<section *transloco="let t; read: 'nested'"><div>{{t.title}}</div></section>`, false);
201+
testTranslationWithRead(host);
202202
}));
203203
});
204204

projects/ngneat/transloco/src/lib/transloco.directive.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class TranslocoDirective implements OnInit, OnDestroy, OnChanges {
3333
@Input('transloco') key: string;
3434
@Input('translocoParams') params: HashMap = {};
3535
@Input('translocoScope') inlineScope: string | undefined;
36-
@Input('translocoContext') inlineContext: string | undefined;
36+
@Input('translocoRead') inlineRead: string | undefined;
3737
@Input('translocoLang') inlineLang: string | undefined;
3838
@Input('translocoLoadingTpl') inlineTpl: TemplateRef<any> | undefined;
3939

@@ -104,13 +104,13 @@ export class TranslocoDirective implements OnInit, OnDestroy, OnChanges {
104104
}
105105

106106
private structuralStrategy(data: Translation) {
107-
const dataWithContext = this.inlineContext ? getValue(data, this.inlineContext) : data;
107+
const translations = this.inlineRead ? getValue(data, this.inlineRead) : data;
108108
if (this.view) {
109-
this.view.context['$implicit'] = dataWithContext;
109+
this.view.context['$implicit'] = translations;
110110
} else {
111111
this.detachLoader();
112112
this.view = this.vcr.createEmbeddedView(this.tpl, {
113-
$implicit: dataWithContext
113+
$implicit: translations
114114
});
115115
}
116116
}

src/app/scope-sharing/scope-sharing.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ <h5><u>Pipe</u></h5>
2727
<p data-cy="p-todos-page-scope">{{ 'todos.title' | transloco }}</p>
2828
<p data-cy="p-global">{{ 'home' | transloco }}</p>
2929

30-
<h5><u>Settings a context to a nested property</u></h5>
30+
<h5><u>Use read to get translations of a nested property</u></h5>
3131
<code
3232
><pre>{{ ex.nested }}</pre></code
3333
>
34-
<ng-container *transloco="let t; context: 'nested'">
34+
<ng-container *transloco="let t; read: 'nested'">
3535
<h5>{{ t.title }}</h5>
3636
<p>{{ t.desc }}</p>
3737
</ng-container>

src/app/scope-sharing/scope-sharing.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class ScopeSharingComponent implements OnInit {
1616
<p>{{ t.todos.title }}</p>
1717
<p>{{ t.home }}</p>
1818
</ng-container>`,
19-
nested: ` <ng-container *transloco="let t; context: 'nested'">
19+
nested: ` <ng-container *transloco="let t; read: 'nested'">
2020
<h5>{{ t.title }}</h5>
2121
<p>{{ t.desc }}</p>
2222
</ng-container>`

0 commit comments

Comments
 (0)