Skip to content

Commit 0cad8b8

Browse files
alexfriesenshaharkazaz
authored andcommitted
feature(transloco): providers functions
1 parent 2044afb commit 0cad8b8

File tree

6 files changed

+160
-56
lines changed

6 files changed

+160
-56
lines changed

docs/docs/getting-started/installation.mdx

+54-10
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,21 @@ First, Transloco creates boilerplate files for the requested translations:
3333
}
3434
```
3535

36-
Next, it will create a new file, `transloco-root.module.ts` which exposes an Angular's module with a default configuration, and inject it into the `AppModule`:
36+
## NgModule Setup
37+
38+
Next, it will create a new file, `transloco-root.module.ts` which exposes an Angular module with a default configuration, and inject it into the `AppModule`:
3739

3840
```ts title="transloco-root.module.ts"
41+
import { Injectable, isDevMode, NgModule } from '@angular/core';
3942
import { HttpClient } from '@angular/common/http';
4043
import {
41-
TRANSLOCO_LOADER,
4244
Translation,
4345
TranslocoLoader,
44-
TRANSLOCO_CONFIG,
4546
translocoConfig,
46-
TranslocoModule
47+
TranslocoModule,
48+
provideTranslocoConfig,
49+
provideTranslocoLoader
4750
} from '@ngneat/transloco';
48-
import { Injectable, isDevMode, NgModule } from '@angular/core';
4951

5052
@Injectable({ providedIn: 'root' })
5153
export class TranslocoHttpLoader implements TranslocoLoader {
@@ -59,18 +61,17 @@ export class TranslocoHttpLoader implements TranslocoLoader {
5961
@NgModule({
6062
exports: [ TranslocoModule ],
6163
providers: [
62-
{
63-
provide: TRANSLOCO_CONFIG,
64-
useValue: translocoConfig({
64+
provideTranslocoConfig(
65+
translocoConfig({
6566
availableLangs: ['en', 'es'],
6667
defaultLang: 'en',
6768
// Remove this option if your application
6869
// doesn't support changing language in runtime.
6970
reRenderOnLangChange: true,
7071
prodMode: !isDevMode(),
7172
})
72-
},
73-
{ provide: TRANSLOCO_LOADER, useClass: TranslocoHttpLoader }
73+
),
74+
provideTranslocoLoader(TranslocoHttpLoader)
7475
]
7576
})
7677
export class TranslocoRootModule {}
@@ -95,6 +96,49 @@ getTranslation(langPath: string) {
9596
:::
9697
And that's it! Now we are ready to use it in our project.
9798

99+
## Standalone Setup
100+
101+
Add the `provideTransloco` function to your `main.ts` and pass in your configuration.
102+
103+
```ts title="main.ts"
104+
import {inject, Injectable, isDevMode } from '@angular/core';
105+
import { bootstrapApplication } from '@angular/platform-browser';
106+
import { HttpClient, provideHttpClient } from '@angular/common/http';
107+
import {
108+
Translation,
109+
TranslocoLoader,
110+
translocoConfig,
111+
provideTransloco,
112+
} from '@ngneat/transloco';
113+
import { AppComponent } from './app/app.component';
114+
115+
@Injectable({ providedIn: 'root' })
116+
export class TranslocoHttpLoader implements TranslocoLoader {
117+
private http = inject(HttpClient);
118+
119+
getTranslation(lang: string) {
120+
return this.http.get<Translation>(`/assets/i18n/${lang}.json`);
121+
}
122+
}
123+
124+
bootstrapApplication(AppComponent, {
125+
providers: [
126+
provideHttpClient(),
127+
provideTransloco({
128+
config: translocoConfig({
129+
availableLangs: ['en', 'es'],
130+
defaultLang: 'en',
131+
// Remove this option if your application
132+
// doesn't support changing language in runtime.
133+
reRenderOnLangChange: true,
134+
prodMode: !isDevMode(),
135+
}),
136+
loader: TranslocoHttpLoader,
137+
})
138+
]
139+
});
140+
```
141+
98142
## Angular & Transloco versions
99143

100144
Make sure you install the version corresponding to your Angular version:

libs/transloco/src/index.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
export { translate, translateObject, TranslocoService } from './lib/transloco.service';
1+
export {
2+
translate,
3+
translateObject,
4+
TranslocoService,
5+
} from './lib/transloco.service';
26
export { TranslocoDirective } from './lib/transloco.directive';
37
export { TranslocoPipe } from './lib/transloco.pipe';
4-
export { TranslocoModule, defaultProviders } from './lib/transloco.module';
8+
export { TranslocoModule } from './lib/transloco.module';
59
export { TRANSLOCO_LOADER, TranslocoLoader } from './lib/transloco.loader';
610
export {
711
TranslocoConfig,
@@ -44,3 +48,10 @@ export { getBrowserCultureLang, getBrowserLang } from './lib/browser-lang';
4448
export { getPipeValue, getLangFromScope, getScopeFromLang } from './lib/shared';
4549
export * from './lib/types';
4650
export * from './lib/helpers';
51+
export {
52+
defaultProviders,
53+
provideTransloco,
54+
provideTranslocoConfig,
55+
provideTranslocoLoader,
56+
provideTranslocoScope,
57+
} from './lib/transloco.providers';

libs/transloco/src/lib/transloco-testing.module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
import { TRANSLOCO_LOADER, TranslocoLoader } from './transloco.loader';
1010
import { HashMap, Translation } from './types';
1111
import { Observable, of } from 'rxjs';
12-
import { defaultProviders, TranslocoModule } from './transloco.module';
12+
import { TranslocoModule } from './transloco.module';
13+
import { defaultProviders } from './transloco.providers';
1314
import {
1415
TRANSLOCO_CONFIG,
1516
TranslocoConfig,

libs/transloco/src/lib/transloco.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { InjectionToken, Provider } from '@angular/core';
2-
import { AvailableLangs, HashMap } from './types';
1+
import { InjectionToken } from '@angular/core';
2+
import { AvailableLangs } from './types';
33

44
export interface TranslocoConfig {
55
defaultLang: string;
+4-41
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
11
import { NgModule } from '@angular/core';
22
import { TranslocoLoaderComponent } from './loader-component.component';
33
import { TranslocoDirective } from './transloco.directive';
4-
import {
5-
DefaultTranspiler,
6-
TRANSLOCO_TRANSPILER,
7-
} from './transloco.transpiler';
4+
import { defaultProviders } from './transloco.providers';
85
import { TranslocoPipe } from './transloco.pipe';
9-
import {
10-
DefaultHandler,
11-
TRANSLOCO_MISSING_HANDLER,
12-
} from './transloco-missing-handler';
13-
import {
14-
DefaultInterceptor,
15-
TRANSLOCO_INTERCEPTOR,
16-
} from './transloco.interceptor';
17-
import {
18-
DefaultFallbackStrategy,
19-
TRANSLOCO_FALLBACK_STRATEGY,
20-
} from './transloco-fallback-strategy';
21-
import { TRANSLOCO_CONFIG } from './transloco.config';
22-
23-
export const defaultProviders = [
24-
{
25-
provide: TRANSLOCO_TRANSPILER,
26-
useClass: DefaultTranspiler,
27-
deps: [TRANSLOCO_CONFIG],
28-
},
29-
{
30-
provide: TRANSLOCO_MISSING_HANDLER,
31-
useClass: DefaultHandler,
32-
},
33-
{
34-
provide: TRANSLOCO_INTERCEPTOR,
35-
useClass: DefaultInterceptor,
36-
},
37-
{
38-
provide: TRANSLOCO_FALLBACK_STRATEGY,
39-
useClass: DefaultFallbackStrategy,
40-
deps: [TRANSLOCO_CONFIG],
41-
},
42-
];
436

447
@NgModule({
45-
declarations: [TranslocoDirective, TranslocoPipe, TranslocoLoaderComponent],
46-
providers: [defaultProviders],
47-
exports: [TranslocoDirective, TranslocoPipe]
8+
declarations: [TranslocoDirective, TranslocoPipe, TranslocoLoaderComponent],
9+
providers: [defaultProviders],
10+
exports: [TranslocoDirective, TranslocoPipe],
4811
})
4912
export class TranslocoModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import { Provider, Type } from '@angular/core';
2+
3+
import { TRANSLOCO_LOADER, TranslocoLoader } from './transloco.loader';
4+
import { TRANSLOCO_CONFIG, TranslocoConfig } from './transloco.config';
5+
import { TRANSLOCO_SCOPE } from './transloco-scope';
6+
import { TranslocoScope } from './types';
7+
8+
import {
9+
DefaultTranspiler,
10+
TRANSLOCO_TRANSPILER,
11+
} from './transloco.transpiler';
12+
import {
13+
DefaultHandler,
14+
TRANSLOCO_MISSING_HANDLER,
15+
} from './transloco-missing-handler';
16+
import {
17+
DefaultInterceptor,
18+
TRANSLOCO_INTERCEPTOR,
19+
} from './transloco.interceptor';
20+
import {
21+
DefaultFallbackStrategy,
22+
TRANSLOCO_FALLBACK_STRATEGY,
23+
} from './transloco-fallback-strategy';
24+
25+
export const defaultProviders = [
26+
{
27+
provide: TRANSLOCO_TRANSPILER,
28+
useClass: DefaultTranspiler,
29+
deps: [TRANSLOCO_CONFIG],
30+
},
31+
{
32+
provide: TRANSLOCO_MISSING_HANDLER,
33+
useClass: DefaultHandler,
34+
},
35+
{
36+
provide: TRANSLOCO_INTERCEPTOR,
37+
useClass: DefaultInterceptor,
38+
},
39+
{
40+
provide: TRANSLOCO_FALLBACK_STRATEGY,
41+
useClass: DefaultFallbackStrategy,
42+
deps: [TRANSLOCO_CONFIG],
43+
},
44+
];
45+
46+
type TranslocoOptions = {
47+
config?: TranslocoConfig;
48+
loader?: Type<TranslocoLoader>;
49+
};
50+
51+
export function provideTransloco(options: TranslocoOptions) {
52+
const providers: Provider[] = [...defaultProviders];
53+
54+
if (options.config) {
55+
providers.push(...provideTranslocoConfig(options.config));
56+
}
57+
58+
if (options.loader) {
59+
providers.push(...provideTranslocoLoader(options.loader));
60+
}
61+
62+
return providers;
63+
}
64+
65+
export function provideTranslocoConfig(config: TranslocoConfig) {
66+
return [
67+
{
68+
provide: TRANSLOCO_CONFIG,
69+
useValue: config,
70+
},
71+
];
72+
}
73+
74+
export function provideTranslocoLoader(loader: Type<TranslocoLoader>) {
75+
return [{ provide: TRANSLOCO_LOADER, useClass: loader }];
76+
}
77+
78+
export function provideTranslocoScope(scope: TranslocoScope) {
79+
return [
80+
{
81+
provide: TRANSLOCO_SCOPE,
82+
useValue: scope,
83+
},
84+
];
85+
}

0 commit comments

Comments
 (0)